SlideShare ist ein Scribd-Unternehmen logo
1 von 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();
}

Weitere ähnliche Inhalte

Ähnlich wie 12 constructors invocation and data members initialization

Ähnlich wie 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++
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
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
 

Mehr von Docent Education

Mehr von Docent Education (13)

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
 
7 class objects
7 class objects7 class objects
7 class objects
 
6 pointers functions
6 pointers functions6 pointers functions
6 pointers functions
 
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
 

Kürzlich hochgeladen

Instruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics Trade
Instruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics TradeInstruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics Trade
Instruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics TradeOptics-Trade
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeOptics-Trade
 
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Eticketing.co
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyApk Toly
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样7pn7zv3i
 
Austria vs France David Alaba Switches Position to Defender in Austria's Euro...
Austria vs France David Alaba Switches Position to Defender in Austria's Euro...Austria vs France David Alaba Switches Position to Defender in Austria's Euro...
Austria vs France David Alaba Switches Position to Defender in Austria's Euro...Eticketing.co
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxEuro Cup 2024 Tickets
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLAll American Billiards
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.SJU Quizzers
 
Technical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeTechnical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeOptics-Trade
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7dollysharma2066
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Judith Chuquipul
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeOptics-Trade
 
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesMysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Servicesnajka9823
 
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfArturo Pacheco Alvarez
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝soniya singh
 

Kürzlich hochgeladen (18)

Instruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics Trade
Instruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics TradeInstruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics Trade
Instruction Manual | ThermTec Hunt Thermal Clip-On Series | Optics Trade
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
 
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
 
Austria vs France David Alaba Switches Position to Defender in Austria's Euro...
Austria vs France David Alaba Switches Position to Defender in Austria's Euro...Austria vs France David Alaba Switches Position to Defender in Austria's Euro...
Austria vs France David Alaba Switches Position to Defender in Austria's Euro...
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FL
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.
 
Technical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeTechnical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics Trade
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
 
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Serviceyoung Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics Trade
 
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesMysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
 
FULL ENJOY Call Girls In Savitri Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  Savitri Nagar (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  Savitri Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In Savitri Nagar (Delhi) Call Us 9953056974
 
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
 

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