SlideShare a Scribd company logo
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();
}

More Related Content

PDF
C++ prgms 4th unit Inheritance
PPTX
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
PDF
C++ prgms 3rd unit
PDF
Design Patterns
PDF
Unittests für Dummies
PDF
Pim Elshoff "Technically DDD"
PDF
CQRS and Event Sourcing in a Symfony application
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
C++ prgms 4th unit Inheritance
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
C++ prgms 3rd unit
Design Patterns
Unittests für Dummies
Pim Elshoff "Technically DDD"
CQRS and Event Sourcing in a Symfony application
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6

What's hot (20)

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

Similar to C++ prgms 5th unit (inheritance ii) (20)

PPTX
OOP unit II inheritance.pptx object oriented programming
PDF
Inheritance
PPTX
Introduction to inheritance and different types of inheritance
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPTX
Inheritance.pptx
PPTX
inheritance in OOPM
PPTX
Inheritance
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPTX
Aryan's pres. entation.pptx
PPTX
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
PPT
Inheritance
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
[OOP - Lec 20,21] Inheritance
PPT
Inheritance in C++
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPTX
INHERITANCE.pptx
PPT
PPT
10.Inheritance.ppt for oops programinggg
OOP unit II inheritance.pptx object oriented programming
Inheritance
Introduction to inheritance and different types of inheritance
Chapter 6 and inheritance OOP C++ tu ioe
Inheritance.pptx
inheritance in OOPM
Inheritance
MODULE2_INHERITANCE_SESSION1.ppt computer
Aryan's pres. entation.pptx
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Inheritance
lecture-2021inheritance-160705095417.pdf
[OOP - Lec 20,21] Inheritance
Inheritance in C++
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
INHERITANCE.pptx
10.Inheritance.ppt for oops programinggg
Ad

More from Ananda Kumar HN (9)

PPTX
DAA 18CS42 VTU CSE
PPTX
CGV 18CS62 VTU CSE
PDF
Python for beginners
PDF
VTU Network lab programs
PDF
VTU CN-1important questions
PDF
Oop with c++ notes unit 01 introduction
PDF
C++ prgms io file unit 7
PDF
Microsoft office in kannada for begineers
PPTX
Microsoft office in KANNADA
DAA 18CS42 VTU CSE
CGV 18CS62 VTU CSE
Python for beginners
VTU Network lab programs
VTU CN-1important questions
Oop with c++ notes unit 01 introduction
C++ prgms io file unit 7
Microsoft office in kannada for begineers
Microsoft office in KANNADA

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Lesson notes of climatology university.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Computing-Curriculum for Schools in Ghana
Lesson notes of climatology university.
Renaissance Architecture: A Journey from Faith to Humanism
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPH.pptx obstetrics and gynecology in nursing
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
master seminar digital applications in india
Cell Structure & Organelles in detailed.

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