SlideShare a Scribd company logo
1 of 11
09/04/131 VIT - SCSE
• In multiple inheritance, the constructors of base classes
are invoked first, in order in which they appear in the
declaration of the derived class.
• In multilevel inheritance, they are executed in the order of
inheritance.
• Data member initialization is represented by
• Datamembername(value)
• The value can be arguments of a constructor, expression
or other data members.
Constructors Invocation and Data Members Initialization
09/04/132 VIT - SCSE
class B
{
protected:
int x,y;
public:
B(int a,int b):x(a),y(b) //x=a, y=b
{
}
};
class D:public B
{
private:
int a,b;
public:
D(int p,int q,int r):a(p),B(p,q),b(r)
{
}
void output()
{
cout<<”x=”<<x<<endl;
cout<<”y=”<<y<<endl;
cout<<”a=”<<a<<endl;
cout<<”b=”<<b<<endl;
}
};
void main()
{
D ob(5,10,15);
ob.output();
}
09/04/133 VIT - SCSE
The following examples illustrate the initialization of data
members with different formats:
B(int a,int b):x(a),y(a+b)
B(int a,int b):x(a),y(x+b)
B(int a,int b):y(a),x(y+b)
09/04/134 VIT - SCSE
Overloaded member functions
If the same member (data/function) exists in both the base
class and the derived class, the member in the derived class
will be executed.
Abstract class
Class with no object
Use for inheritance only
Can derive classes from an abstract class
09/04/135 VIT - SCSE
class baseA
{
protected:
int x;
…..
….
};
class derivedB:public baseA
{
protected:
…..
….
};
class derivedD:public
baseA
{
protected:
…..
….
};
class abc:public
derivedB,public
derived
{
……
……
};
Virtual Base Class
 Multipath inheritance
09/04/136 VIT - SCSE
• The data member x is inherited twice in the derived class
abc, once through the derived class derivedB and again
through derivedD.
• This is wasteful.
• The above multiple repetition of the data member can be
corrected by changing the derived class derivedB and
derivedD into virtual base classes.
• Any base class which is declared using the keyword 
virtual is called a virtual base class.
09/04/137 VIT - SCSE
class baseA
{
protected:
int x;
…..
….
};
class derivedB:public virtual baseA
{
protected:
…..
….
};
class derivedD:public virtual
baseA
{
protected:
…..
….
};
class abc:public derivedB,public
derivedD
{
……
……
};
09/04/138 VIT - SCSE
By making derivedB and derivedD into virtual base classes
for abc, a copy of the data member x is available only once.
09/04/139 VIT - SCSE
class A
{
protected:
int x;
public:
void getdata();
void display();
};
class B:public virtual A
{
protected:
float y;
public:
void getdata();
void display();
};
class C:public virtual
A
{
protected:
char name[20];
public:
void getdata();
void display();
};
class D:public
B,public C
{
public:
void getdata();
void display();
};
09/04/1310 VIT - SCSE
void A:getdata()
{
cout<<”Enter an integer”;
cin>>x;
}
void A:display()
{
cout<<”Integer:”<<x<,endl;
}
void B:getdata()
{
A::getdata();
cout<<”Enter a floating point value”;
cin>>y;
}
void B:display()
{
A::display();
cout<<”Real Number:”<<y<<endl;
}
void C:getdata()
{
A::getdata();
cout<<”Enter a string”;
cin>>name;
}
void C:display()
{
A::display();
cout<<”String:”<<name<
<endl;
}
void D:getdata()
{
B::getdata();
C::getdata();
}
09/04/1311 VIT - SCSE
void D:display()
{
B::display();
C::display();
}
void main()
{
D ob;
ob.getdata();
ob.display();
}

More Related Content

Similar to 12 constructors invocation and data members initialization

Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
Deepak Singh
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 

Similar to 12 constructors invocation and data members initialization (20)

Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Inheritance
InheritanceInheritance
Inheritance
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
10 inheritance
10 inheritance10 inheritance
10 inheritance
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 

More from Docent Education (11)

17 files and streams
17 files and streams17 files and streams
17 files and streams
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
13 exception handling
13 exception handling13 exception handling
13 exception handling
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
5 array
5 array5 array
5 array
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented Programming
 
2 Intro c++
2 Intro c++2 Intro c++
2 Intro c++
 
unit-1-intro
 unit-1-intro unit-1-intro
unit-1-intro
 

Recently uploaded

Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
baharayali
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

Netherlands Players expected to miss UEFA Euro 2024 due to injury.docx
Netherlands Players expected to miss UEFA Euro 2024 due to injury.docxNetherlands Players expected to miss UEFA Euro 2024 due to injury.docx
Netherlands Players expected to miss UEFA Euro 2024 due to injury.docx
 
JORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdf
JORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdfJORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdf
JORNADA 6 LIGA MURO 2024TUXTEPECOAXACA.pdf
 
2k Shots ≽ 9205541914 ≼ Call Girls In Sheikh Sarai (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Sheikh Sarai (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Sheikh Sarai (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Sheikh Sarai (Delhi)
 
European Football Icons that Missed Opportunities at UEFA Euro 2024.docx
European Football Icons that Missed Opportunities at UEFA Euro 2024.docxEuropean Football Icons that Missed Opportunities at UEFA Euro 2024.docx
European Football Icons that Missed Opportunities at UEFA Euro 2024.docx
 
Unveiling the Mystery of Main Bazar Chart
Unveiling the Mystery of Main Bazar ChartUnveiling the Mystery of Main Bazar Chart
Unveiling the Mystery of Main Bazar Chart
 
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
 
Albania Vs Spain South American coaches lead Albania to Euro 2024 spot.docx
Albania Vs Spain South American coaches lead Albania to Euro 2024 spot.docxAlbania Vs Spain South American coaches lead Albania to Euro 2024 spot.docx
Albania Vs Spain South American coaches lead Albania to Euro 2024 spot.docx
 
Sector 62, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 62, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docxSlovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
Slovenia Vs Serbia UEFA Euro 2024 Fixture Guide Every Fixture Detailed.docx
 
Technical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics Trade
Technical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics TradeTechnical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics Trade
Technical Data | Sig Sauer Easy6 BDX 1-6x24 | Optics Trade
 
Slovenia Vs Serbia Eurovision odds Slovenia have top.docx
Slovenia Vs Serbia Eurovision odds Slovenia have top.docxSlovenia Vs Serbia Eurovision odds Slovenia have top.docx
Slovenia Vs Serbia Eurovision odds Slovenia have top.docx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Hire 💕 8617697112 Kasauli Call Girls Service Call Girls Agency
Hire 💕 8617697112 Kasauli Call Girls Service Call Girls AgencyHire 💕 8617697112 Kasauli Call Girls Service Call Girls Agency
Hire 💕 8617697112 Kasauli Call Girls Service Call Girls Agency
 
Spain Vs Italy Spain to be banned from participating in Euro 2024.docx
Spain Vs Italy Spain to be banned from participating in Euro 2024.docxSpain Vs Italy Spain to be banned from participating in Euro 2024.docx
Spain Vs Italy Spain to be banned from participating in Euro 2024.docx
 
Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...
Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...
Italy vs Albania Italy Euro 2024 squad Luciano Spalletti's full team ahead of...
 
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdfJORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
 
Ramban Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts In...
Ramban  Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts In...Ramban  Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts In...
Ramban Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts In...
 
Who Is Emmanuel Katto Uganda? His Career, personal life etc.
Who Is Emmanuel Katto Uganda? His Career, personal life etc.Who Is Emmanuel Katto Uganda? His Career, personal life etc.
Who Is Emmanuel Katto Uganda? His Career, personal life etc.
 
Personal Brand Exploration - By Bradley Dennis
Personal Brand Exploration - By Bradley DennisPersonal Brand Exploration - By Bradley Dennis
Personal Brand Exploration - By Bradley Dennis
 
Austria vs France Austria Euro 2024 squad Ralf Rangnick's full team ahead of ...
Austria vs France Austria Euro 2024 squad Ralf Rangnick's full team ahead of ...Austria vs France Austria Euro 2024 squad Ralf Rangnick's full team ahead of ...
Austria vs France Austria Euro 2024 squad Ralf Rangnick's full team ahead of ...
 

12 constructors invocation and data members initialization

  • 1. 09/04/131 VIT - SCSE • In multiple inheritance, the constructors of base classes are invoked first, in order in which they appear in the declaration of the derived class. • In multilevel inheritance, they are executed in the order of inheritance. • Data member initialization is represented by • Datamembername(value) • The value can be arguments of a constructor, expression or other data members. Constructors Invocation and Data Members Initialization
  • 2. 09/04/132 VIT - SCSE class B { protected: int x,y; public: B(int a,int b):x(a),y(b) //x=a, y=b { } }; class D:public B { private: int a,b; public: D(int p,int q,int r):a(p),B(p,q),b(r) { } void output() { cout<<”x=”<<x<<endl; cout<<”y=”<<y<<endl; cout<<”a=”<<a<<endl; cout<<”b=”<<b<<endl; } }; void main() { D ob(5,10,15); ob.output(); }
  • 3. 09/04/133 VIT - SCSE The following examples illustrate the initialization of data members with different formats: B(int a,int b):x(a),y(a+b) B(int a,int b):x(a),y(x+b) B(int a,int b):y(a),x(y+b)
  • 4. 09/04/134 VIT - SCSE Overloaded member functions If the same member (data/function) exists in both the base class and the derived class, the member in the derived class will be executed. Abstract class Class with no object Use for inheritance only Can derive classes from an abstract class
  • 5. 09/04/135 VIT - SCSE class baseA { protected: int x; ….. …. }; class derivedB:public baseA { protected: ….. …. }; class derivedD:public baseA { protected: ….. …. }; class abc:public derivedB,public derived { …… …… }; Virtual Base Class  Multipath inheritance
  • 6. 09/04/136 VIT - SCSE • The data member x is inherited twice in the derived class abc, once through the derived class derivedB and again through derivedD. • This is wasteful. • The above multiple repetition of the data member can be corrected by changing the derived class derivedB and derivedD into virtual base classes. • Any base class which is declared using the keyword  virtual is called a virtual base class.
  • 7. 09/04/137 VIT - SCSE class baseA { protected: int x; ….. …. }; class derivedB:public virtual baseA { protected: ….. …. }; class derivedD:public virtual baseA { protected: ….. …. }; class abc:public derivedB,public derivedD { …… …… };
  • 8. 09/04/138 VIT - SCSE By making derivedB and derivedD into virtual base classes for abc, a copy of the data member x is available only once.
  • 9. 09/04/139 VIT - SCSE class A { protected: int x; public: void getdata(); void display(); }; class B:public virtual A { protected: float y; public: void getdata(); void display(); }; class C:public virtual A { protected: char name[20]; public: void getdata(); void display(); }; class D:public B,public C { public: void getdata(); void display(); };
  • 10. 09/04/1310 VIT - SCSE void A:getdata() { cout<<”Enter an integer”; cin>>x; } void A:display() { cout<<”Integer:”<<x<,endl; } void B:getdata() { A::getdata(); cout<<”Enter a floating point value”; cin>>y; } void B:display() { A::display(); cout<<”Real Number:”<<y<<endl; } void C:getdata() { A::getdata(); cout<<”Enter a string”; cin>>name; } void C:display() { A::display(); cout<<”String:”<<name< <endl; } void D:getdata() { B::getdata(); C::getdata(); }
  • 11. 09/04/1311 VIT - SCSE void D:display() { B::display(); C::display(); } void main() { D ob; ob.getdata(); ob.display(); }