Download presentation
Presentation is loading. Please wait.
Published byJeffery Stokes Modified over 9 years ago
1
Constructor in Inheritance
2
2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class constructor is automatically called class A { protected: int a; public: A(){a=10;} }; class B:public A { public: void show() {cout<<a;} }; void main() { B obj; obj.show(); }
3
3 Constructor in Inheritance If base class contains parameterized constructor then the derived class must have constructor ; Through the derived class constructor we can call base class constructor. In main() we are creating object of only derived class, then derived class constructor will call base class constructor The derived class constructor function definition contains two part derived class constructor(arglist): initialization section { constructor body; } colon separates the constructor declaration of the derived class from the base class constructor.
4
4 Constructor in Inheritance class A { protected: int a; public: A(int x){a=x;} }; class B:public A { int b; public: B(int x,int y):A(x) {b=y;} void show() {cout<<a<<" "<<b;} }; void main() { B obj(1,2); obj.show(); }
5
5 Constructor in Inheritance In multiple inheritance derived(int x,int y,int z):A(x),B(y) { c=z; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.