Answers for "parametrized constructor in inheritance"

0

parametrized constructor in inheritance

#include <iostream>
using namespace std;
//order of execution of constructors in inheritance
class Base
{
   int x;
   public:
   // default constructor
   Base()
   {
      cout << "Base class default constructor\n"; } //parameterized constructor Base(int x) { this->x = x;
      cout<<"Base class parameterized constructor\n";
   }
};
class Derived : public Base
{
   int y;
   public:
   // default constructor
   Derived()
   {
      cout << "Derived class default constructor\n";
   }
   // parameterized constructor
   Derived(int i):Base(i)
   {
      cout << "Derived class parameterized constructor\n";
   }
};
int main()
{
   Base b; //construct base class object
   Derived d1; //construct derived class object with default constructor
   Derived d2(10); //construct derived class object with parameterized constructor
}
Posted by: Guest on April-27-2022

Code answers related to "parametrized constructor in inheritance"

Browse Popular Code Answers by Language