Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Code output of: Q5: What will be the output of this program after an instanc

ID: 3859769 • Letter: C

Question

C++ Code output of:

Q5: What will be the output of this program after an instance of Class5 is created? (5 points)

#include <iostream>

using namespace std;

class Class1

{

public:

    Class1()

    {

        cout << "Class1 constructor is executed." << endl;

    }

};

class Class2

{

public:

    Class2()

    {

        cout << "Class2 constructor is executed." << endl;

    }

};

class Class3

{

public:

    Class3()

    {

        cout << "Class3 constructor is executed." << endl;

    }

};

class Class4

{

public:

    Class4()

    {

        cout << "Class4 constructor is executed." << endl;

    }

};

class Class5 : public Class4, Class2, Class1, Class3

{

public:

    Class5()

    {

        cout << "Class5 constructor is executed." << endl;

    }

};

Q6: What will be the output of this program after an instance of Class5 is created? (5 points)

#include <iostream>

using namespace std;

class Class1

{

public:

    Class1()

    {

        cout << "Class1 constructor is executed." << endl;

    }

};

class Class2

{

public:

    Class2()

    {

        cout << "Class2 constructor is executed." << endl;

    }

};

class Class3

{

public:

    Class3()

    {

        cout << "Class3 constructor is executed." << endl;

    }

};

class Class4

{

public:

    Class4()

    {

        cout << "Class4 constructor is executed." << endl;

    }

};

class Class5 : public Class4, Class2, Class1, Class3

{

public:

    Class5() :

        Class3(),

        Class1(),

        Class4(),

        Class2()

    {

        cout << "Class5 constructor is executed." << endl;

    }

};

Q7: What will be the output of this program after an instance of Class5 is created and then destroyed? (5 points)

#include <iostream>

using namespace std;

class Class1

{

public:

    Class1()

    {

        cout << "Class1 constructor is executed." << endl;

    }

    ~Class1()

    {

        cout << "Class1 destructor is executed." << endl;

    }

};

class Class2

{

public:

    Class2()

    {

        cout << "Class2 constructor is executed." << endl;

    }

    ~Class2()

    {

        cout << "Class2 destructor is executed." << endl;

   }

};

class Class3

{

public:

    Class3()

    {

        cout << "Class3 constructor is executed." << endl;

    }

    ~Class3()

    {

        cout << "Class3 destructor is executed." << endl;

    }

};

class Class4

{

public:

    Class4()

    {

        cout << "Class4 constructor is executed." << endl;

    }

    ~Class4()

    {

        cout << "Class4 destructor is executed." << endl;

    }

};

class Class5 : public Class4, Class2, Class1, Class3

{

public:

    Class5() :

        Class3(),

        Class1(),

        Class4(),

        Class2()

    {

        cout << "Class5 constructor is executed." << endl;

    }

    ~Class5()

    {

        cout << "Class5 destructor is executed." << endl;

    }

};

Q11: Consider the below class definition. Mark “T” (for valid) or “F” (for invalid) next to the code lines (A, B, C, D) that instantiate the Automobile instance(s). (4 points)

class Automobile

{

private:

    string Make;

    int Year;

    string Model;

public:

    Automobile(string make, string model, int year) :

        Make (make),

        Model (model),

        Year (year)

    {

        cout << "Automobile constructor is called." << endl;

    }

    ~Automobile()

    {

        cout << "Automobile destructor is called." << endl;

    }

};

Automobile car1;

Automobile car2("Lexus", "RX450", 2017);

Automobile * pCars = new Automobile[2]

   {

        { "Lexus", "RX450", 2017 },

        { "Mercedes", "E450", 2017 },

};

Automobile * pCars = new Automobile[]

   {

        { "Lexus", "RX450", 2017 },

        { "Mercedes", "E450", 2017 },

};

Q15: Consider the following code. Write sample code lines that demonstrate calling the copy constructor and the assignment of this class. (5 points)

#include <iostream>

#include <string>

using namespace std;

class Automobile

{

private:

    string Make;

    int Year;

    string Model;

public:

    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;

    }

};

Explanation / Answer

Q5)

This is the output when the instance of class5 is created Class5 cls5;

class4 constructor is executed

class2 constructor is executed

class1 constructor is executed

class3 constructor is executed

class5 constructor is executed

__________________

Q6)

This is the output when the instance of class5 is created Class5 cls5;

class4 constructor is executed

class2 constructor is executed

class1 constructor is executed

class3 constructor is executed

class5 constructor is executed

____________________

Q7)

This is the output when the instance of class5 is created Class5 cls5;

class4 constructor is executed

class2 constructor is executed

class1 constructor is executed

class3 constructor is executed

class5 constructor is executed

class5 destructor is executed

class3 destructor is executed

class1 destructor is executed

class2 destructor is executed

This is output when the destructor is called cls5.~Class5();

class5 destructor is executed

class3 destructor is executed

class1 destructor is executed

class2 destructor is executed

__________________

Q11)

Automobile car1; ---- F (There is no default constructor in the class)

______________

Automobile car2("Lexus", "RX450", 2017); ----- T

______________

Automobile * pCars = new Automobile[2] ------ T

   {

        { "Lexus", "RX450", 2017 },

        { "Mercedes", "E450", 2017 },

};

_________________

Automobile * pCars = new Automobile[] -------------- F

   {

        { "Lexus", "RX450", 2017 },

        { "Mercedes", "E450", 2017 },

};

____________________________

Q15)

#include <iostream>

#include <string>

using namespace std;

class Automobile

{

private:

string Make;

int Year;

string Model;

public:

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;

}

};

int main()

{

//Calling the Parameterized constructor

Automobile am1("Mercedes", "X6", 2017);

//Calling the Parameterized constructor

Automobile am2("Audi", "Q5", 2012);

//Calling the Copy constructor

Automobile am3(am1);

//Calling the assignment Operator

am3=am1;

}

_____________________