Professional Documents
Culture Documents
POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Name of Subject:- Object Oriented Programming Using C++ MSBTE Subject Code:- 22316
Chapter 3: INHERITANCE
INHERITANCE
BASE
Class
A Direction of arrow
indicates the
direction of
inheritance
DERIVED
Class
B
A B
c
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 8
2) MULTIPLE Inheritance
• Derived class with SEVERAL BASE CLASSES is
called as multiple inheritance.
B c D
c
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 11
4) MULTLEVEL Inheritance
• A class derived from another derived class called
as multilevel inheritance.
STUDENT (roll)
TEST(sub1, sub2)
RESULT(per,sub1,sub2)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 12
5) HYBRID Inheritance
• Combination of more than one inheritance
A
B C
Result(roll)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 14
5) HYBRID Inheritance
• Combination of more than one inheritance
class student (roll)
class test: virtual public class sport: virtual public
student student
d1.b = 20;
d1.mul_ab();
d1.show_abc();
return 0;
}
Sanjivani K.B.P. Polytechnic, Kopargaon Department
19
of Compute Technology P. M. Dhanrao
Base and derived class with their functions
class D:private B
Sanjivani K.B.P. Polytechnic, Kopargaon Department
25
of Compute Technology P. M. Dhanrao
:SI .. Private derivation ERRORS
int main()
{ D d1;
'void B::get_ab()' is inaccessible
//d1.get_ab(); 'void B::show_a()' is inaccessible
//d1.show_a();
d1.mul_ab();
d1.show_abc(); 'int B::b' is inaccessible
// d1.b = 20;
d1.mul_ab();
d1.show_abc();
return 0;
} Sanjivani K.B.P. Polytechnic, Kopargaon Department
26
of Compute Technology P. M. Dhanrao
:SI .. Base class B
#include <iostream> void B::get_ab()
using namespace std;
class B
{ a=5; b=10; }
{ int B::get_a()
int a;
public: { return a; }
int b;
int get_a(); void B::show_a()
void get_ab();
void show_a();
{ cout<<"\n a ="<<a; }
};
Sanjivani K.B.P. Polytechnic, Kopargaon Department
27
of Compute Technology P. M. Dhanrao
:SI .. private derivation….. derived class D
class D:private B void D::mul_ab()
{
{ get_ab();
int c; c = b * get_a();
}
public: void D::show_abc()
void mul_ab(); { cout<<"\n a="<<get_a();
cout<<"\n b="<<b;
void show_abc(); cout<<"\n c="<<c;
}; }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
28
of Compute Technology P. M. Dhanrao
:SI .. Private derivation main() and OUTPUT
int main()
{
D d1;
//d1.get_ab(); // wont work after deriving became private of D so its object cant access
//d1.show_a(); // wont work after deriving became private member of D so its object cant access
d1.mul_ab();
d1.show_abc();
class D:protected B
Sanjivani K.B.P. Polytechnic, Kopargaon Department
34
of Compute Technology P. M. Dhanrao
Sanjivani K.B.P. Polytechnic, Kopargaon Department
35
of Compute Technology P. M. Dhanrao
PROTECTED MODE?
// B DERIVED FROM A
// C DERIVED FORM C
class B: public A
{ …. }; // B derived from A
class C: public B
{ …. }; // C derived from B