Exercise 6:

Create a directory ex6 and store in this directory all the files corresponding to this exercise.

Question 1: Destructors. Order of construction / destruction.

In this exercise, you will check your understanding of the cleanup of C++ objects by their destructors. Please create the files "Engine.h", "Car.h" and "test.cpp". In "Engine.h" type and complete the following code:
// Engine.h
#include <string>
#include <iostream>

#ifndef ENGINE_H
#define ENGINE_H


class Engine {
public:
 Engine(const std::string& n, int n_cyl, int w) 
      // complete the code for the ctor 
      // using an initialization list
 Engine(const Engine& e) 
      // complete the code for the ctor 
      // using an initialization list

 // write the code for the destructor here.
 // The destructor does nothing except printing: ~Engine().

 Engine& operator= (const Engine& e) {
   // complete the code for the assignment operator
 }
 std::string get_name() const { return name; }
 int get_num_cylinders() const { return num_cylinders; }
 int get_weight() const { return weight; }
 void set_name(const std::string& s) { name = s; }
 void set_num_cylinders(int n) { num_cylinders = n; }
 void set_weight(int w) { weight = w; }
 void print() const {
   // complete the code that prints the engine's name
   // its number of cylinders and its weight
 }
private:
 std::string name;
 int num_cylinders;
 int weight;
};

#endif // ENGINE_H
The class Car has an object of type Engine. In "Car.h" type and complete the following code:
// Car.h
#include <string>
#include <iostream>
#include "Engine.h"

#ifndef CAR_H
#define CAR_H

class Car {
public:
 Car(const Engine& e, std::string n) 
           // complete the code for the ctor 
           // using an init list and empty body
 Car(const Car& c) 
           // complete the code for the copy ctor 
           // using an init list and empty body

 // write the code for the dtor here

 Car& operator= (const Car& c) {
   // complete the code for the assignment operator here
 }

 std::string get_name() const { return name; }
 void set_name(const std::string& n) { name = n; }
 
 void print() const {
  // print information of the car: its name and the characteristics
  // of its engine
 } 

private:
 Engine* engine; // the car's engine
 std::string name; // name of the car
};

#endif // CAR_H
Finally to test your classes, type and complete in "test.cpp" the following code:
// test.cpp

// Include the needed headers here

int main()
{
 Car* c = new Car(Engine("V100_2", 16, 2000), "Toyota");
 c->print();
 delete c;
}

Question 2: Inheritance.

In this question, you will check your understanding of the basic principles of inheritance. Create the following files: "Person.h", "Student.h", "Student.cpp" and "test_inheritance.cpp". In the file "Person.h" type the following code (there is nothing to complete here):
#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person {
public:
 Person(const std::string& n) : name(n) {}
 Person(const Person& p) : name(p.name) {}
 ~Person() {}
 Person& operator= (const Person& p) {
  name = p.name;
  return *this;
 }
 void set_name(const std::string& s) {
  name = s;
 }
 std::string get_name() const { return name; }

private:
 std::string name;
};

#endif // PERSON_H
Write in "Student.h" the definition for a class Student that inherits (public inheritance) from Person. A Student is a specialized version of Person with the following additional fields (to be declared private): Student has also the following additional methods (with public access): Add these methods in the header "Student.h" and add their implementation in "Student.cpp". Student has only one constructor: The code for the constructor can go in "Student.h". The destructor has an empty body (same as ~Person()) and will go with the constructor in "Student.h". Finally to test your code, you can type in the file "test_inheritance.cpp" the following code:
// test_inheritance.cpp
#include <iostream>
#include "Person.h"
#include "Student.h"

int main() {
  Student* s = new Student("Tanaka", 2, 12345);
  std::cout << "Student's name: " << s->get_name() << std::endl;
  std::cout << "Student's id: " << s->get_student_id() << std::endl;
  delete s;
}