Sei sulla pagina 1di 12

| 3/8 B.

Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |

CONSTRUCTORS & DESTRUCTORS IN


INHERITANCE

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

class-name(a1, a2,…an) : base-class-name(a1, a2,…an)


{
// instance variable ‘1’=arg’1’
// instance variable ‘2’=arg’2’

// instance variable ‘n’=arg’n’
}

Calling base class constructor

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 |

1. USAGE OF CONSTRUCTOR IN SINGLE INHERITANCE


(One base class constructor, One Derived class constructor)
SOURCE CODE
#include<iostream>
using namespace std;
class A // base class
{
public:
int a;
A(int i)
{
a=i;
}
};
class B:public A // derived class
{
public:
int b;
B(int i, int j):A(i)
{
b=j;
}
void disp()
{
Calling base class
cout<<"a = "<<a<<"\n"; constructor using its name
cout<<"b = "<<b<<"\n";
}
};

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 |

2. USAGE OF CONSTRUCTOR IN SINGLE INHERITANCE


(One base class constructor, One Derived class constructor)
SOURCE CODE
#include<iostream>
#include<cstring>
using namespace std;
class Person // base class
{
public:
int id;
char name[100];
Person(char n[],int k)
{
id=k;
strcpy(name,n);
}
};
class Student : public Person // derived class
{
public:
double cgpa;
char place[100];
Student(char n[],int k,double c,char loc[]):Person(n,k)
{
cgpa=c;
strcpy(place,loc);
} Calling base class
constructor using its name
void disp()
{

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 |

3. USAGE OF CONSTRUCTOR IN MULTIPLE INHERITANCE


(Two base class constructors, One Derived class constructor)
SOURCE CODE
#include<iostream>
using namespace std;
class A // base class 1
{
public:
A()
{
cout<<"Zero argument constructor of Base class 1 ...\n";
}
};
class B // base class 2
{
public:
B()
{
cout<<"Zero argument constructor of Base class 2 ...\n";
}

};
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 |

4. USAGE OF CONSTRUCTOR IN MULTILEVEL INHERITANCE


(One base class constructor, One Intermediate class, One Derived class
constructor)

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

CONSTRUCTORS & DESTRUTORS IN INHERITANCE


 Constructors are executed from top to bottom order
 Destructors are executed from bottom to top order
 Destructors are executed when an objects goes out of the scope.

10
| 3/8 B. Tech-IT [Constructors in Inheritance] 25 August 2018, 11:45:47 AM-5:17:10 PM |

4. CONSTRUCTORS & DESTRUTORS IN INHERITANCE

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

Potrebbero piacerti anche