Question: Correct the code errors in the following programs: Class X inherits from class Y. #include using namespace std; // class Y definition class Y {
Correct the code errors in the following programs:
Class X inherits from class Y.
#include
// class Y definition class Y { public: Y(); // default constructor ~Y(); // destructor private: int data; }; // end class Y
// class X definition class X ; public Y { public: // function print void print() const { cout << data; } // end function print }; // end class X
The following code should construct a Derived object.
#include
// class Base definition class Base { private: // constructor Base( int b ) { cout << b; } // end class Base constructor }; // end class Base
// class Derived definition class Derived : public Base { // constructor calls base-class constructor Derived( int a ) : Base( a ) { // empty } // end class Derived constructor }; // end class Derived
int main() { Derived d( 5 ); } // end main
The following code should create an object of type B. Class B inherits from class A.
#include
// class A definition class A { 7 public: // constructor A( int a ) { value = a; } // end class A constructor
// return value int getValue() const { return value; } // end function getValue private: int value; }; // end class A
// class B definition class B { public: // constructor B( int b ) : A( b ) { // empty } // end class B constructor }; // end class B
int main() { B object( 50 ); cout << object.getValue(); } // end main
The following code should create an object of type Y. Class Y inherits from class X.
#include
// class X definition class X { public: // constructor X() { cout << "X constructed!"; } // end class X constructor }; // end class X
// class Y definition class Y { public: // redefine inherited constructor X() { cout << "Y created, not X!"; } // end class Y constructor }; // end class Y
int main() { Y yObject(); } // end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
