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

P11.1 The class derivedCL inherits base class baseCL , using public inheritance.

ID: 3830045 • Letter: P

Question

P11.1 The class derivedCL inherits base class baseCL, using public inheritance. Both classes have a version of the function f(). for simplicity, all functions are implemented inline (i.e., code for all member functions have been provided). The interfaces of two classes are provided below:

class baseCL

{

public:

   baseCL(int x = 1) : baseX(x), baseY(4)

   {}

   int f()

   { return baseX + baseY;}

private:

   int baseX;

protected:

   int baseY;

};

class derivedCL : public baseCL

{

public:

   derivedCL() : x(10), y(11), baseCL(5)

   {}

   int f()

   {

       y = baseCL :: baseY * 2;

       x = 3;

       return x * y;

   }

private:

   int x, y;

};

Assume the given declarations for objects base1Obj, base2Obj, and derivedObj in the main function with base1Obj initialized to 2.

Predict the output for the following statements. If a statement is not valid, indicates that as the output.

       cout << base1Obj.f() << endl;            // output = ___________________________

       cout << base2Obj.f() << endl;            // output = ___________________________

       cout << derivedObj.f() << endl;          // output = ___________________________

       cout << derivedObj.baseCL::f() << endl; // output = ___________________________

       base2Obj = derivedObj;

       cout << base2Obj.f() << endl << endl;    // output = ___________________________

cout << base2Obj.derivedCL.f() << endl; // output = ___________________________

1 ) Implement all member functions outside the scope of the class then write the main function to test all member functions. Display the output and compare the results with those obtained in part (a).

2) Remove “protected” from baseCL class and run the program again. Does the program compile? If not, what is the error message?

3) What happens when “public” inheritance (in the header of the derived class) in replaced by “private” or “protected” inheritance?

Explanation / Answer

1.) Outputs of the program

     1.1 ) cout << base1Obj.f() << endl -> The output of this code of line is 5 as this object of baseCL class will initialise the variable x with value 1 and variable y with 4 and adds both of them and prints the output to the console.

1.2 ) cout << base2Obj.f() << endl -> The output of this code of line is 5 as this object of baseCL class will initialise the variable x with value 1 and variable y with 4 and adds both of them and prints the output to the console.

   1.3 ) cout << derivedObj.f() << endl -> The output of ths code is 24 as it will first fetch the value of variable y of base class and multiply it with 2 and assign it to the variable y of derived class, initialise variable x of the derived class with value 3, and then it will multiply both the values x and y variables of derived class and return the value.

1.4 ) cout << derivedObj.baseCL::f() << endl -> The output of this code is 9 as it will initialise the variable x of base class with value 5 and variable y of base class with value 4 , and it will call the f() method of base class to add x and y of base class and return the resultant value.

1.5 ) cout << base2Obj.f() << endl << endl -> The output of this code is 9 as it will initialise the variable x of base class with value 5 and variable y of base class with value 4 , and it will call the f() method of base class to add x and y of base class and return the resultant value.

1.6 ) cout << base2Obj.derivedCL.f() << endl -> This line of code will run into compile error as it will try to access the derivedCL method of base2Obj object of baseCL class which does not exist or in other words, it has not been defined.

2.) Removing protected keyword from baseCL class will run the program into compile time error as it will try to access the variable y of baseCL class which is private and according to the OOPS concept, private variables or methods of the class cn not be accessed outside the definition of the class. Accessing the private variables of the class will run the program into compile time error of accessing the private variables which are inaccessible.

3.) If the public keyword in derivedCL class definition is replaced with either private or protected, then the program will run into compile time error as it will try to access the baseCL class methods and variables through the object of derivedCL class which has been inherited by it in either private scope or protected scope. So, inheriting the baseCL class by derivedCL class in either private scope or protected scope will run the program into compile time error.