This code defines two classes Base and Derived where Derived is derived from Base class publicly. The Base class has two constructors, a default constructor and a parameterized constructor which takes an integer as input. The Derived class also has two constructors, a default constructor and a parameterized constructor which takes an integer as input. Additionally, the Derived class has another parameterized constructor that takes two integers as input, one for Base class constructor and one for Derived class constructor.
In the main function, three objects of Derived class are created using different constructors. d1 is created using the default constructor of Derived class, d2 is created using the parameterized constructor of Derived class which takes an integer as input, and d3 is created using the parameterized constructor of Derived class which takes two integers as input.
#include <iostream> using namespace std; class Base { public: Base() { cout<<"Non-param Base"<<endl; } Base(int x) { cout<<"Param of Base "<<x<<endl; } }; class Derived:public Base { public: Derived() { cout<<"Non-Param Derived"<<endl; } Derived(int y) { cout<<"Param of Derived "<<y<<endl; } Derived(int x,int y):Base(x) { cout<<"Param of Derived "<<y<<endl; } }; int main() { Derived d1,d2(5),d3(10,50); }To download raw file Click Here
Non-param BaseNon-Param DerivedNon-param BaseParam of Derived 5Param of Base 10Param of Derived 50
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions