11 constructors in derived classes

17
06/22/22 1 VIT - SCSE 1. The derived class need not have a constructor as long as the base class has a no-argument constructor. 2. However, if the base class has constructors with arguments (one or more), then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. 3. When an object of a derived class is created, the constructor of the base class is executed first and later the constructor of the derived class. Constructors in Derived Classes

description

constructors in derived classes

Transcript of 11 constructors in derived classes

Page 1: 11 constructors in derived classes

04/10/231 VIT - SCSE

1. The derived class need not have a constructor as long as the base class has a no-argument constructor.

2. However, if the base class has constructors with arguments (one or more), then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor.

3. When an object of a derived class is created, the constructor of the base class is executed first and later the constructor of the derived class.

Constructors in Derived Classes

Page 2: 11 constructors in derived classes

04/10/232 VIT - SCSE

1. No constructors in the base class and derived class2. Constructor only in the base class class A{public:A(){cout<<”No-argument constructor of the base class B is executed”;}};class B:public A{

public:};void main(){B ob; //accesses base constructor}

Constructors in Derived Classes

Page 3: 11 constructors in derived classes

04/10/233 VIT - SCSE

3. Constructor only in the derived class class A{public:};class B:public A{

public:B(){cout<<”No-argument constructor of the base class B is executed”;}

};void main(){B ob; //accesses derived constructor}

Page 4: 11 constructors in derived classes

04/10/234 VIT - SCSE

4. Constructors in both base and derived classesclass A{public:A(){cout<<”No-argument constructor of the base class A executed first”;}};class B:public A{

public:B(){cout<<”No-argument constructor of the base class B is executed next”;}

};void main(){B ob; //accesses both constructor}

Page 5: 11 constructors in derived classes

04/10/235 VIT - SCSE

class A{public:A(){cout<<”No argument constructor of base class A”;}A(int a){cout<<”One argument constructor of the base class A”;}};

class B:public A{public:B(int a){cout<<”One argument constructor of the derived class B”;}};void main(){B ob(3);}

5. Multiple constructors in base class and a single constructor in derived class

Page 6: 11 constructors in derived classes

04/10/236 VIT - SCSE

Output:

No argument constructor of base class AOne argument constructor of the derived class B

Page 7: 11 constructors in derived classes

04/10/237 VIT - SCSE

class A{public:A(int a){cout<<”One argument constructor of the base class A”;} };class B:public A{public:B(int a){cout<<”One argument constructor of the derived class B”;} };

void main(){B ob(3);} Output: Error

6. Constructor in base and derived classes without default constructor

Page 8: 11 constructors in derived classes

04/10/238 VIT - SCSE

class A{public:A(int a){cout<<”One argument constructor of the base class A”;}};class B:public A{public:B(int a):A(a){cout<<”One argument constructor of the derived class B”;}};

void main(){B ob(3);} Output: One argument constructor of the base class AOne argument constructor of the derived class B

7. Explicit invocation in the absence of default constructor

Page 9: 11 constructors in derived classes

04/10/239 VIT - SCSE

class A1{public:A1(){cout<<”No-argument constructor of the base class A1”;}};class A2{public:A2(){cout<<”No argument constructor of the base class A2”;}};

class B:public A2,public A1{public:B(){cout<<”No argument constructor of the derived class B”;}};void main(){B ob;}

8. Constructor in a multiple inherited class with default invocation

Page 10: 11 constructors in derived classes

04/10/2310 VIT - SCSE

Output: No argument constructor of the base class A2No-argument constructor of the base class A1No argument constructor of the derived class B

Page 11: 11 constructors in derived classes

04/10/2311 VIT - SCSE

class A1{public:A1(){cout<<”No-argument constructor of the base class A1”;}};class A2{public:A2(){cout<<”No argument constructor of the base class A2”;}};

class B:public A2,public A1{public:B():A2(),A1(){cout<<”No argument constructor of the derived class B”;}};void main(){B ob;}

9. Constructor in a multiple inherited class with explicit invocation

Page 12: 11 constructors in derived classes

04/10/2312 VIT - SCSE

Output: No argument constructor of the base class A2No-argument constructor of the base class A1No argument constructor of the derived class B

Page 13: 11 constructors in derived classes

04/10/2313 VIT - SCSE

class A{public:A1(){cout<<”No-argument constructor of the base class A”;}};class B:public A{public:B(){cout<<”No argument constructor of the base class B”;}};

class C:public B{public:C(){cout<<”No argument constructor of the derived class C”;}};void main(){C ob;} 

10. Constructor in multilevel inheritance

Page 14: 11 constructors in derived classes

04/10/2314 VIT - SCSE

Output: No argument constructor of the base class ANo-argument constructor of the base class BNo argument constructor of the derived class C

Page 15: 11 constructors in derived classes

04/10/2315 VIT - SCSE

Destructors in derived classes 1.Unlike constructors, destructors in the class hierarchy (parent and child class) are invoked in the reverse order of the constructor invocation.2.The destructor of that class whose constructor was executed last, while building object of the derived class, will be executed first whenever the object goes out of scope.

Page 16: 11 constructors in derived classes

04/10/2316 VIT - SCSE

class B1{public:B1(){cout<<”No argument constructor of the base class B1”;}~B1(){cout<<”Destructor in the base class B1”;}};class B2{public:B2(){cout<<”No argument constructor of the base class B2”;}

~B2(){cout<<”Destructor in the base class B2”;}};class D:public B1,public B2{public:D(){cout<<”No argument constructor of the derived class D”;}

Page 17: 11 constructors in derived classes

04/10/2317 VIT - SCSE

~D(){cout<<”Destructor in the derived class D”;}};void main(){D ob;}

Output: No argument constructor of the base class B1No argument constructor of the base class B2No argument constructor of the derived class DDestructor in the derived class DDestructor in the base class B2Destructor in the base class B1