SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
Inheritance – II UNIT - 5
1 | P a g e A n a n d a K u m a r H N
Constructors, Destructors and Inheritance:
“Constructors are called in the order of
derivation and destructor are called in the
reverse order of derivation”
/*constructor and destructor in “simple
inheritance” when BASE class object is
created*/
#include<iostream>
using namespace std;
class base
{
public:
base( )
{
cout<<"base class constructor"<<endl;
}
~base( )
{
cout<<"base class destructor"<<endl;
}
};
class derived: public base
{
public:
derived( )
{
cout<<"derived class constructor”<<endl;
}
~derived( )
{
cout<<"derived class destructor”<<endl;
}
};
int main( )
{
base b1; //object of base class is created
}
OUTPUT:
base class constructor
base class destructor
/*constructor and destructor in “simple
inheritance” when DERIVED class object is
created*/
#include<iostream>
using namespace std;
class base
{
public:
base( )
{
cout<<"base class constructor"<<endl;
}
~base( )
{
cout<<"base class destructor"<<endl;
}
};
class derived: public base
{
public:
derived( )
{
cout<<"derived class constructor"<<endl;
}
~derived( )
{
cout<<"derived class destructor"<<endl;
}
};
int main( )
{
derived d1; //object of derived class is
created
}
OUTPUT:
Inheritance – II UNIT - 5
2 | P a g e A n a n d a K u m a r H N
/*constructor and destructor in “simple
inheritance” when BOTH BASE and
DERIVED classes object are created*/
#include<iostream>
using namespace std;
class base
{
public:
base( )
{
cout<<"base class constructor"<<endl;
}
~base( )
{
cout<<"base class destructor"<<endl;
}
};
class derived: public base
{
public:
derived( )
{
cout<<"derived class constructor"<<endl;
}
~derived( )
{
cout<<"derived class destructor"<<endl;
}
};
int main( )
{
base b1;
derived d1;
}
OUTPUT:
/*constructor and destructor in “Multilevel
inheritance” */
#include<iostream>
using namespace std;
class base
{
public:
base( )
{
cout<<"base class constructor"<<endl;
}
~base( )
{
cout<<"base class destructor"<<endl;
}
};
class derived1: public base
{
public:
derived1( )
{
cout<<"derived1 class constructor"<<endl;
}
~derived1( )
{
cout<<"derived1 class destructor"<<endl;
}
};
class derived2: public derived1
{
public:
derived2( )
{
cout<<"derived2 class constructor"<<endl;
}
~derived2( )
{
cout<<"derived2 class destructor"<<endl;
}
};
int main( )
{
derived2 d2;
}
OUTPUT:
Inheritance – II UNIT - 5
2 | P a g e A n a n d a K u m a r H N
/*constructor and destructor in “simple
inheritance” when BOTH BASE and
DERIVED classes object are created*/
#include<iostream>
using namespace std;
class base
{
public:
base( )
{
cout<<"base class constructor"<<endl;
}
~base( )
{
cout<<"base class destructor"<<endl;
}
};
class derived: public base
{
public:
derived( )
{
cout<<"derived class constructor"<<endl;
}
~derived( )
{
cout<<"derived class destructor"<<endl;
}
};
int main( )
{
base b1;
derived d1;
}
OUTPUT:
/*constructor and destructor in “Multilevel
inheritance” */
#include<iostream>
using namespace std;
class base
{
public:
base( )
{
cout<<"base class constructor"<<endl;
}
~base( )
{
cout<<"base class destructor"<<endl;
}
};
class derived1: public base
{
public:
derived1( )
{
cout<<"derived1 class constructor"<<endl;
}
~derived1( )
{
cout<<"derived1 class destructor"<<endl;
}
};
class derived2: public derived1
{
public:
derived2( )
{
cout<<"derived2 class constructor"<<endl;
}
~derived2( )
{
cout<<"derived2 class destructor"<<endl;
}
};
int main( )
{
derived2 d2;
}
OUTPUT:
Inheritance – II UNIT - 5
4 | P a g e A n a n d a K u m a r H N
/*two parameters for constructor*/
#include<iostream>
using namespace std;
class base
{
public:
int i,j;
base(int x,int y) //two parameters
{
cout<<"base class constructor"<<endl;
i=x;
j=y;
}
};
class derived:public base
{
public:
int m,n;
derived(int p,int q,int r,int s):base(r,s)
{
cout<<"derived class constructor"<<endl;
m=p;
n=q;
}
void show()
{
cout<<i<<endl<<j<<endl;
cout<<m<<endl<<n<<endl;
}
};
int main()
{
derived d1(111,20,33,99);
d1.show();
}
OUTPUT:
base class constructor
derived class constructor
33
99
111
20
/*Passing parameter to base class
constructor in MULTIPLE inheritance*/
#include<iostream>
using namespace std;
class base1
{
public:
int i;
base1(int x)
{
cout<<"base1 class constructor"<<endl;
i=x;
}
};
class base2
{
public:
int j;
base2(int y)
{
cout<<"base2 class constructor"<<endl;
j=y;
}
};
class derived:public base1,public base2
{
int k;
public:
derived(int p,int q,int r):base1(q),base2(r)
{
cout<<"derived class constructor"<<endl;
k=p;
}
void show()
{
cout<<i<<endl;
cout<<j<<endl;
cout<<k<<endl;
}
};
int main()
{
derived d1(111,20,33);
d1.show();
}
OUTPUT:
Base1 class constructor
Base2 class constructor
derived class constructor
20
33
111
Inheritance – II UNIT - 5
5 | P a g e A n a n d a K u m a r H N
Granting access:
#include<iostream>
using namespace std;
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase()
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived: private base
{
private:
int m,n;
public:
base::initbase; //initbase is public again
base::showbase; //showbase is public again
void initderived(int u,int v)
{
m=u;
n=v;
}
void showderived()
{
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main()
{
derived d1;
d1.initbase(77,99);
d1.showbase();
d1.initderived(33,44);
d1.showderived();
}
/* ambiguous in diamond (or) multiple
inheritance*/
#include<iostream>
using namespace std;
class A
{
public:
int i;
};
class B : public A
{
public:
int j;
};
class C: public A
{
public:
int k;
};
class D: public B, public C
{
public:
int sum;
};
int main()
{
D ob;
ob.i = 10; //ambiguous since two copies of
i is inherited ERROR
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k; //ERROR
cout<<"Value of i is : "<<ob.i; //ERROR
cout<<"Value of j is : ”"<< ob.j;
cout << "Value of k is :”"<< ob.k;
cout << "“Sum is : ”"<< ob.sum;
return 0;
}
Inheritance – II UNIT - 5
6 | P a g e A n a n d a K u m a r H N
/* Solving Multiple Inheritance ambiguity
by using : : operator */
#include<iostream>
using namespace std;
class A
{
public:
int i;
};
class B : public A
{
public:
int j;
};
class C: public A
{
public:
int k;
};
class D: public B, public C
{
public:
int sum;
};
int main()
{
D ob;
ob.B::i = 10; //unambiguous
ob.C::i=100; //unambiguous
ob.j = 20;
ob.k = 30;
ob.sum = ob.B::i + ob.j + ob.k; //unambiguous
cout<<"Value of i is in B: "<<ob.B::i<<endl;
cout<<"Value of i is in C: "<<ob.C::i<<endl;
cout<<"Value of j is : "<< ob.j<<endl;
cout << "Value of k is :"<< ob.k<<endl;
cout << "Sum is : "<< ob.sum<<endl;
return 0;
}
OUTPUT:
Virtual base classes / Inheritance:
Example 1:
#include<iostream>
using namespace std;
class A
{
public:
int i;
};
class B : public virtual A
{
public:
int j;
};
class C: public virtual A
{
public:
int k;
};
class D: public B, public C
{
public:
int sum;
};
int main()
{
D ob;
ob.i = 10;//unambiguous since one copy of i is
inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.B::i + ob.j + ob.k;
cout<<"Value of i is in B: "<<ob.B::i<<endl;
cout<<"Value of i is in C: "<<ob.C::i<<endl;
cout<<"Value of i in A : "<<ob.A::i<<endl;
cout<<"Value of j is : "<< ob.j<<endl;
cout << "Value of k is :"<< ob.k<<endl;
cout << "Sum is : "<< ob.sum<<endl;
return 0;
}
OUTPUT:
Inheritance – II UNIT - 5
7 | P a g e A n a n d a K u m a r H N
Example 2: Virtual Base Inheritance
#include<iostream>
using namespace std;
class stuinfo
{
public:
int usn;
char name[20];
void readinfo()
{
cout<<"enter student usn and namen";
cin>>usn>>name;
}
};
class theoryscore : public virtual stuinfo
{
public:
int t1,t2,t3;
void readtheoryscore()
{
cout<<"enter student score in t1,t2 and t3n";
cin>>t1>>t2>>t3;
}
};
class labscore: public virtual stuinfo
{
public:
int L1,L2;
void readlabscore()
{
cout<<"enter student score in L1 and L2n";
cin>>L1>>L2;
}
};
class result: public theoryscore, public labscore
{
public:
void show()
{
cout<<"Student details are:n";
cout<<"usn:"<<usn<<endl; //one copy of usn;
cout<<"name:"<<name<<endl; //one copy of name;
cout<<"marks1:"<<t1<<endl;
cout<<"marks2:"<<t2<<endl;
cout<<"marks3:"<<t3<<endl;
cout<<"lab1:"<<L1<<endl;
cout<<"lab2:"<<L2<<endl;
}
void avgresult()
cout<<"average marks in theory is:"<<
(t1+t2+t3)/3.0<<endl;
cout<<"average marks in LAB is:"<<
(L1+L2)/2.0<<endl;
}
};
int main()
{
result obj;
obj.readinfo();// one copy of readinfo() function
obj.readtheoryscore();
obj.readlabscore();
obj.show();
obj.avgresult();
}

Mais conteúdo relacionado

Mais procurados

Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
Fabien Potencier
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Fabien Potencier
 
implementing of properties
implementing of propertiesimplementing of properties
implementing of properties
prakashpatel86
 

Mais procurados (20)

Lecture4
Lecture4Lecture4
Lecture4
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Combatendo code smells em Java
Combatendo code smells em Java Combatendo code smells em Java
Combatendo code smells em Java
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usage
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Presentation1
Presentation1Presentation1
Presentation1
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in Laravel
 
Construindo APIs de forma produtiva com Spring Boot, Spring Data e Spring MVC
Construindo APIs de forma produtiva com Spring Boot, Spring Data e Spring MVCConstruindo APIs de forma produtiva com Spring Boot, Spring Data e Spring MVC
Construindo APIs de forma produtiva com Spring Boot, Spring Data e Spring MVC
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
Top Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in DrupalTop Ten Reasons to Use EntityFieldQuery in Drupal
Top Ten Reasons to Use EntityFieldQuery in Drupal
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event Sourcing
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web Developers
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
 
Lecture19
Lecture19Lecture19
Lecture19
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
implementing of properties
implementing of propertiesimplementing of properties
implementing of properties
 

Semelhante a C++ prgms 5th unit (inheritance ii)

INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 

Semelhante a C++ prgms 5th unit (inheritance ii) (20)

Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
Lab3
Lab3Lab3
Lab3
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
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
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Inheritance
InheritanceInheritance
Inheritance
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
Opp compile
Opp compileOpp compile
Opp compile
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 

Mais de Ananda Kumar HN (9)

DAA 18CS42 VTU CSE
DAA 18CS42 VTU CSEDAA 18CS42 VTU CSE
DAA 18CS42 VTU CSE
 
CGV 18CS62 VTU CSE
CGV 18CS62 VTU CSECGV 18CS62 VTU CSE
CGV 18CS62 VTU CSE
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questions
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineers
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADA
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

C++ prgms 5th unit (inheritance ii)

  • 1. Inheritance – II UNIT - 5 1 | P a g e A n a n d a K u m a r H N Constructors, Destructors and Inheritance: “Constructors are called in the order of derivation and destructor are called in the reverse order of derivation” /*constructor and destructor in “simple inheritance” when BASE class object is created*/ #include<iostream> using namespace std; class base { public: base( ) { cout<<"base class constructor"<<endl; } ~base( ) { cout<<"base class destructor"<<endl; } }; class derived: public base { public: derived( ) { cout<<"derived class constructor”<<endl; } ~derived( ) { cout<<"derived class destructor”<<endl; } }; int main( ) { base b1; //object of base class is created } OUTPUT: base class constructor base class destructor /*constructor and destructor in “simple inheritance” when DERIVED class object is created*/ #include<iostream> using namespace std; class base { public: base( ) { cout<<"base class constructor"<<endl; } ~base( ) { cout<<"base class destructor"<<endl; } }; class derived: public base { public: derived( ) { cout<<"derived class constructor"<<endl; } ~derived( ) { cout<<"derived class destructor"<<endl; } }; int main( ) { derived d1; //object of derived class is created } OUTPUT:
  • 2. Inheritance – II UNIT - 5 2 | P a g e A n a n d a K u m a r H N /*constructor and destructor in “simple inheritance” when BOTH BASE and DERIVED classes object are created*/ #include<iostream> using namespace std; class base { public: base( ) { cout<<"base class constructor"<<endl; } ~base( ) { cout<<"base class destructor"<<endl; } }; class derived: public base { public: derived( ) { cout<<"derived class constructor"<<endl; } ~derived( ) { cout<<"derived class destructor"<<endl; } }; int main( ) { base b1; derived d1; } OUTPUT: /*constructor and destructor in “Multilevel inheritance” */ #include<iostream> using namespace std; class base { public: base( ) { cout<<"base class constructor"<<endl; } ~base( ) { cout<<"base class destructor"<<endl; } }; class derived1: public base { public: derived1( ) { cout<<"derived1 class constructor"<<endl; } ~derived1( ) { cout<<"derived1 class destructor"<<endl; } }; class derived2: public derived1 { public: derived2( ) { cout<<"derived2 class constructor"<<endl; } ~derived2( ) { cout<<"derived2 class destructor"<<endl; } }; int main( ) { derived2 d2; } OUTPUT:
  • 3. Inheritance – II UNIT - 5 2 | P a g e A n a n d a K u m a r H N /*constructor and destructor in “simple inheritance” when BOTH BASE and DERIVED classes object are created*/ #include<iostream> using namespace std; class base { public: base( ) { cout<<"base class constructor"<<endl; } ~base( ) { cout<<"base class destructor"<<endl; } }; class derived: public base { public: derived( ) { cout<<"derived class constructor"<<endl; } ~derived( ) { cout<<"derived class destructor"<<endl; } }; int main( ) { base b1; derived d1; } OUTPUT: /*constructor and destructor in “Multilevel inheritance” */ #include<iostream> using namespace std; class base { public: base( ) { cout<<"base class constructor"<<endl; } ~base( ) { cout<<"base class destructor"<<endl; } }; class derived1: public base { public: derived1( ) { cout<<"derived1 class constructor"<<endl; } ~derived1( ) { cout<<"derived1 class destructor"<<endl; } }; class derived2: public derived1 { public: derived2( ) { cout<<"derived2 class constructor"<<endl; } ~derived2( ) { cout<<"derived2 class destructor"<<endl; } }; int main( ) { derived2 d2; } OUTPUT:
  • 4. Inheritance – II UNIT - 5 4 | P a g e A n a n d a K u m a r H N /*two parameters for constructor*/ #include<iostream> using namespace std; class base { public: int i,j; base(int x,int y) //two parameters { cout<<"base class constructor"<<endl; i=x; j=y; } }; class derived:public base { public: int m,n; derived(int p,int q,int r,int s):base(r,s) { cout<<"derived class constructor"<<endl; m=p; n=q; } void show() { cout<<i<<endl<<j<<endl; cout<<m<<endl<<n<<endl; } }; int main() { derived d1(111,20,33,99); d1.show(); } OUTPUT: base class constructor derived class constructor 33 99 111 20 /*Passing parameter to base class constructor in MULTIPLE inheritance*/ #include<iostream> using namespace std; class base1 { public: int i; base1(int x) { cout<<"base1 class constructor"<<endl; i=x; } }; class base2 { public: int j; base2(int y) { cout<<"base2 class constructor"<<endl; j=y; } }; class derived:public base1,public base2 { int k; public: derived(int p,int q,int r):base1(q),base2(r) { cout<<"derived class constructor"<<endl; k=p; } void show() { cout<<i<<endl; cout<<j<<endl; cout<<k<<endl; } }; int main() { derived d1(111,20,33); d1.show(); } OUTPUT: Base1 class constructor Base2 class constructor derived class constructor 20 33 111
  • 5. Inheritance – II UNIT - 5 5 | P a g e A n a n d a K u m a r H N Granting access: #include<iostream> using namespace std; class base { private: int a,b; public: void initbase(int x,int y) { a=x; b=y; } void showbase() { cout<<"a="<<a<<" b="<<b<<endl; } }; class derived: private base { private: int m,n; public: base::initbase; //initbase is public again base::showbase; //showbase is public again void initderived(int u,int v) { m=u; n=v; } void showderived() { cout<<"m="<<m<<" n="<<n<<endl; } }; int main() { derived d1; d1.initbase(77,99); d1.showbase(); d1.initderived(33,44); d1.showderived(); } /* ambiguous in diamond (or) multiple inheritance*/ #include<iostream> using namespace std; class A { public: int i; }; class B : public A { public: int j; }; class C: public A { public: int k; }; class D: public B, public C { public: int sum; }; int main() { D ob; ob.i = 10; //ambiguous since two copies of i is inherited ERROR ob.j = 20; ob.k = 30; ob.sum = ob.i + ob.j + ob.k; //ERROR cout<<"Value of i is : "<<ob.i; //ERROR cout<<"Value of j is : ”"<< ob.j; cout << "Value of k is :”"<< ob.k; cout << "“Sum is : ”"<< ob.sum; return 0; }
  • 6. Inheritance – II UNIT - 5 6 | P a g e A n a n d a K u m a r H N /* Solving Multiple Inheritance ambiguity by using : : operator */ #include<iostream> using namespace std; class A { public: int i; }; class B : public A { public: int j; }; class C: public A { public: int k; }; class D: public B, public C { public: int sum; }; int main() { D ob; ob.B::i = 10; //unambiguous ob.C::i=100; //unambiguous ob.j = 20; ob.k = 30; ob.sum = ob.B::i + ob.j + ob.k; //unambiguous cout<<"Value of i is in B: "<<ob.B::i<<endl; cout<<"Value of i is in C: "<<ob.C::i<<endl; cout<<"Value of j is : "<< ob.j<<endl; cout << "Value of k is :"<< ob.k<<endl; cout << "Sum is : "<< ob.sum<<endl; return 0; } OUTPUT: Virtual base classes / Inheritance: Example 1: #include<iostream> using namespace std; class A { public: int i; }; class B : public virtual A { public: int j; }; class C: public virtual A { public: int k; }; class D: public B, public C { public: int sum; }; int main() { D ob; ob.i = 10;//unambiguous since one copy of i is inherited. ob.j = 20; ob.k = 30; ob.sum = ob.B::i + ob.j + ob.k; cout<<"Value of i is in B: "<<ob.B::i<<endl; cout<<"Value of i is in C: "<<ob.C::i<<endl; cout<<"Value of i in A : "<<ob.A::i<<endl; cout<<"Value of j is : "<< ob.j<<endl; cout << "Value of k is :"<< ob.k<<endl; cout << "Sum is : "<< ob.sum<<endl; return 0; } OUTPUT:
  • 7. Inheritance – II UNIT - 5 7 | P a g e A n a n d a K u m a r H N Example 2: Virtual Base Inheritance #include<iostream> using namespace std; class stuinfo { public: int usn; char name[20]; void readinfo() { cout<<"enter student usn and namen"; cin>>usn>>name; } }; class theoryscore : public virtual stuinfo { public: int t1,t2,t3; void readtheoryscore() { cout<<"enter student score in t1,t2 and t3n"; cin>>t1>>t2>>t3; } }; class labscore: public virtual stuinfo { public: int L1,L2; void readlabscore() { cout<<"enter student score in L1 and L2n"; cin>>L1>>L2; } }; class result: public theoryscore, public labscore { public: void show() { cout<<"Student details are:n"; cout<<"usn:"<<usn<<endl; //one copy of usn; cout<<"name:"<<name<<endl; //one copy of name; cout<<"marks1:"<<t1<<endl; cout<<"marks2:"<<t2<<endl; cout<<"marks3:"<<t3<<endl; cout<<"lab1:"<<L1<<endl; cout<<"lab2:"<<L2<<endl; } void avgresult() cout<<"average marks in theory is:"<< (t1+t2+t3)/3.0<<endl; cout<<"average marks in LAB is:"<< (L1+L2)/2.0<<endl; } }; int main() { result obj; obj.readinfo();// one copy of readinfo() function obj.readtheoryscore(); obj.readlabscore(); obj.show(); obj.avgresult(); }