C++ Programming question: Consider the below code. In this code, there are 4 cla
ID: 3866779 • Letter: C
Question
C++ Programming question:
Consider the below code. In this code, there are 4 classes (Automobile, Truck, Sedan, and CrossOverVehicle). The Automobile class is the based class of the Truck class and the Sedan class. The CrossOverVehicle inherits from both the Truck class and the Sedan class. In the main() method, the stack variable (crossOver1) is instantiated. Explain the reason why all three outputs (1), (2), and (3) are the same.
#include <iostream>
#include <string>
using namespace std;
// CLASS: Automobile
class Automobile
{
private:
string Make;
int Year;
string Model;
public:
string GetMake() { return Make; }
int GetYear() { return Year; }
string GetModel() { return Model; }
void SetMake(string value) { Make = value; }
void SetYear(int value) { Year = value; }
void SetModel(string value) { Model = value; }
public:
Automobile()
{
Make = "";
Model = "";
Year = 0;
cout << "Automobile default constructor is called." << endl;
}
Automobile(string make, string model, int year) :
Make(make),
Model(model),
Year(year)
{
cout << "Automobile constructor is called." << endl;
}
Automobile(const Automobile & src)
{
Make = src.Make;
Model = src.Model;
Year = src.Year;
cout << "Automobile copy-constructor is called." << endl;
}
public:
~Automobile()
{
cout << "Automobile destructor is called." << endl;
}
public:
const Automobile & operator=(const Automobile & src)
{
Make = src.Make;
Model = src.Model;
Year = src.Year;
cout << "Automobile assignment operator (=) is called." << endl;
return *this;
}
};
// CLASS: Truck
class Truck : virtual public Automobile
{
public:
Truck() :
Automobile()
{
cout << "Truck default constructor is called." << endl;
}
Truck(string make, string model, int year) :
Automobile(make, model + " (Truck)", year)
{
cout << "Truck constructor is called." << endl;
}
Truck(const Truck & src)
{
cout << "Truck copy-constructor is called." << endl;
*this = src;
}
public:
~Truck()
{
cout << "Truck destructor is called." << endl;
}
public:
const Truck & operator=(const Truck & src)
{
Automobile::operator=(src);
cout << "Truck assignment operator (=) is called." << endl;
return *this;
}
};
// CLASS: Sedan
class Sedan : virtual public Automobile
{
public:
Sedan() :
Automobile()
{
cout << "Sedan default constructor is called." << endl;
}
Sedan(string make, string model, int year) :
Automobile(make, model + " (Sedan)", year)
{
cout << "Sedan constructor is called." << endl;
}
Sedan(const Sedan & src)
{
cout << "Sedan copy-constructor is called." << endl;
*this = src;
}
public:
~Sedan()
{
cout << "Sedan destructor is called." << endl;
}
public:
const Sedan & operator=(const Sedan & src)
{
Automobile::operator=(src);
cout << "Sedan assignment operator (=) is called." << endl;
return *this;
}
};
// CLASS: CrossOverVehicle
class CrossOverVehicle : public Truck, public Sedan
{
public:
CrossOverVehicle() :
Truck(),
Sedan()
{
cout << "CrossOverVehicle default constructor is called." << endl;
}
CrossOverVehicle(string make, string model, int year) :
Automobile(make, model + " (CrossOverVehicle)", year)
{
cout << "CrossOverVehicle constructor is called." << endl;
}
CrossOverVehicle(const CrossOverVehicle & src)
{
cout << "CrossOverVehicle copy-constructor is called." << endl;
*this = src;
}
public:
~CrossOverVehicle()
{
cout << "CrossOverVehicle destructor is called." << endl;
}
public:
const CrossOverVehicle & operator=(const CrossOverVehicle & src)
{
Truck::operator=(src);
Sedan::operator=(src);
cout << "CrossOverVehicle assignment operator (=) is called." << endl;
return *this;
}
};
int main()
{
CrossOverVehicle crossOver1("Mercedes", "X6", 2017);
// OUTPUT #1
cout << endl;
cout << "----------------------------" << endl;
cout << "Truck Make: " << ((Truck *)&crossOver1)->GetMake() << endl;
cout << "Truck Model: " << ((Truck *)&crossOver1)->GetModel() << endl;
cout << "Truck Year: " << ((Truck *)&crossOver1)->GetYear() << endl << endl;
// OUTPUT #2
cout << "----------------------------" << endl;
cout << "Sedan Make: " << ((Sedan *)&crossOver1)->GetMake() << endl;
cout << "Sedan Model: " << ((Sedan *)&crossOver1)->GetModel() << endl;
cout << "Sedan Year: " << ((Sedan *)&crossOver1)->GetYear() << endl << endl;
// OUTPUT #3
cout << "----------------------------" << endl;
cout << "CrossOverVehicle Make: " << crossOver1.GetMake() << endl;
cout << "CrossOverVehicle Model: " << crossOver1.GetModel() << endl;
cout << "CrossOverVehicle Year: " << crossOver1.GetYear() << endl << endl;
return 0;
}
Explanation / Answer
Here if we see the hierarchy Automobile is superclass of all the classes here and from inside the constructor of every class constructor Automobile class constructor is called which in turn calls the Make(), Model(),Year() method (which make use of its field members). Also all the three times same input has been provided using direct reference to CrossOverVehicle class and pointers to this class which all end up calling constructor of automobile class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.