Constructor in Inheritance PDF
Constructor in Inheritance PDF
Constructor in Inheritance PDF
CONSTRUCTOR IN INHERITANCE
Constructors are used to initialize member variables of the object and
destructors are used to destroy the object.
Constructors are not inherited in derived / sub class. But a derived
class constructor can call base class constructor using the signatures
(arguments).
A derived class constructor is used to initialize both the derived class
and base class instance variables.
It is important to note that, constructors are executed from top to
bottom and destructors are executed from bottom to top order.
Syntax
1
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
Default Constructor
If a base class contains default constructor, then no need to call base
class constructor in the definition of derived class constructor
Parameterized Constructor
If a base class contains any parameterized constructors, then we
should call base class constructor in the definition of derived class
constructor
IMPORTANT POINTS
The compiler automatically calls constructors and destructors (implicit
calling)
Whenever the default constructor of the derived class is called, the
default constructor of the base class is called automatically
To call (access) the parameterized constructor of base class inside the
parameterized constructor of derived class, we have to define
(mention) it explicitly.
The parameters in the derived call must match the order and type of
parameters defined in the base class constructor.
2
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
3
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
int main()
{
cout<<"---------------------------------------------\n";
cout<<"\tBase & Derived Constructors\n";
cout<<"---------------------------------------------\n";
B obj(25,75);
obj.disp();
cout<<"---------------------------------------------\n";
return 0;
}
OUTPUT
4
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
5
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
cout<<"Name\t: "<<name<<"\n";
cout<<"Id\t: "<<id<<"\n";
cout<<"CGPA\t: "<<cgpa<<"\n";
cout<<"Place\t: "<<place<<"\n";
}
};
int main()
{
cout<<"---------------------------------------------\n";
cout<<"\tDerived Class Constructor\n";
cout<<"---------------------------------------------\n";
Student ss("Vijay",1110,825,"Chennai");
ss.disp();
cout<<"---------------------------------------------\n";
return 0;
} Calling base class and derived
class constructors using derived
class object
OUTPUT
6
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
};
class C: public B, public A // derived class
{
public:
C():B(),A()
7
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
{
cout<<"Zero argument constructor of derived class ...\n";
}
};
int main()
{
cout<<"---------------------------------------------------------\n";
cout<<"\tConstructors in Multiple Inheritance\n";
cout<<"---------------------------------------------------------\n";
C obj;
cout<<"---------------------------------------------------------\n";
return 0;
} Calling base class and derived
class constructors using derived
class object
OUTPUT
8
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
SOURCE CODE
#include<iostream>
using namespace std;
class A // base class 1
{
public:
A()
{
cout<<"Zero argument constructor of Base class ...\n";
}
};
class B: public A // intermediate class
{
public:
B()
{
cout<<"Zero argument constructor of Intermediate class ...\n";
}
};
class C: public B // derived class
{
public:
C():B()
{
cout<<"Zero argument constructor of derived class ...\n";
9
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
}
};
int main()
{
cout<<"---------------------------------------------------------\n";
cout<<"\tConstructors in Multilevel Inheritance\n";
cout<<"---------------------------------------------------------\n";
C obj;
cout<<"---------------------------------------------------------\n";
return 0;
Calling base class and derived
}
class constructors using derived
class object
OUTPUT
10
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
SOURCE CODE
#include<iostream>
using namespace std;
class A // base class
{
public:
A()
{
cout<<"Base class constructor is calling ...\n";
}
~A()
{
cout<<"Base class destructor is calling ...\n";
}
};
class B:public A // derived class
{
public:
B()
{
cout<<"Derived class constructor is calling ...\n";
}
~B()
11
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |
{
cout<<"Derived class destructor is calling ...\n";
}
};
int main()
{
cout<<"---------------------------------------------------------\n";
cout<<"\tConstructors & Destructors in Inheritance\n";
cout<<"---------------------------------------------------------\n";
B obj;
return 0;
}
OUTPUT
12