You are on page 1of 48

SANJIVANI K. B. P.

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

Department:- COMPUTER TECHNOLOGY Class:- SYCM-B

Name of Subject:- Object Oriented Programming Using C++ MSBTE Subject Code:- 22316
Chapter 3: INHERITANCE

3.1 Introduction to Inheritance,


defining a derived class,
visibility modes and effects.

3.2 Types of Inheritance:


Single, Multilevel, Multiple,
Hierarchical and Hybrid.

3.3 Virtual Base Class,


Abstract Class, Constructors
in derived class.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 2


UNIT –III OUTCOMES
• 3a. Explain given type of INHERITANCE based on
its characteristics.
• 3b. Implement given type of Inheritance in C++
program.
• 3c. Write C++ program using virtual base class.
• 3d. Use Constructor in the given derived class.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 3


3.1 INHERITANCE REFERENCE page
• 3.1 Introduction to Inheritance-176
defining a derived class-177
visibility modes and effects-
• 3.2 TYPES- 179-200
• 3.3 Virtual Base Class- 203
Abstract Class-207
Constructors in derived class- 207
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 4
3.1.1
INTRODUCTION
INHERITANCE

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 5


Types of

INHERITANCE

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 6


TYPES…. 1) SINGLE INHERITANCE
• Derived class with only one base class.

BASE
Class
A Direction of arrow
indicates the
direction of
inheritance

DERIVED
Class
B

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 7


2) MULTIPLE Inheritance
• Derived class with SEVERAL BASE CLASSES is
called as multiple inheritance.

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.

Student (roll) Test(sub1,sub2)


sports
result(roll , sub1,sub2, per, hobby) (hobby)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 9
3) HIERARCHICAL Inheritance
• One class inherited by more than one classes.
A

B c D

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 10


4) MULTLEVEL Inheritance
• A class derived from another derived class called
as multilevel inheritance.
A
B

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

Sanjivani K.B.P. Polytechnic, Kopargaon


D Department of Compute Technology P. M. Dhanrao 13
5) HYBRID Inheritance
• Combination of more than one inheritance
Student (roll)

class test:public student Sport(hobby)

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

class Result:public test, public sport


Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 15
PROGRAM
SINGLE INHERITANCE

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 16


BASE class B with its functions

class B void B::get_ab()


{ { a=5; b=10;
Private:
}
int a;
public:
int B::get_a()
int b; { return a;
void get_ab(); }
int get_a(); void B::show_a()
void show_a(); { cout<<"\n a ="<<a;
};
}
Sanjivani K.B.P. Polytechnic, Kopargaon Department
17
of Compute Technology P. M. Dhanrao
Derived class D with its functions
class D : public B void D::mul_ab()
{
int c; { c = b * get_a();
public: }
void mul_ab();
void D::show_abc()
void show_abc();
public: { cout<<"\n a="<<get_a();
int b; cout<<"\n b="<<b;
void get_ab();
int get_a(); cout<<"\n c="<<c;
void show_a(); }
};
Sanjivani K.B.P. Polytechnic, Kopargaon Department
18
of Compute Technology P. M. Dhanrao
main() function and OUTPUT
int main()
{
D d1;
d1.get_ab();
d1.show_a();
d1.mul_ab();
d1.show_abc();

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 B class D:public B


{ void D::mul_ab()
void B::get_ab() {
int a; {c = b * get_a(); }
{ a=5; b=10; } int c;
public: void D::show_abc()
int B::get_a() public:
int b; { cout<<“a="<<get_a()
{ return a; } void mul_ab();
void get_ab(); cout<<"\n b="<<b;
int get_a(); void B::show_a() void
cout<<"\n c="<<c;
{ cout<<a; } show_abc();
void show_a(); }
}; };
Sanjivani K.B.P. Polytechnic, Kopargaon Department
20
of Compute Technology P. M. Dhanrao
SI: ONLY FUNCTIONS with main function
: D1 ACCESSING ALL FUNCTIONS
int main()
void B::get_ab() void D::mul_ab() {
{ a=5; b=10; } {c = b * get_a(); } D d1;
d1.get_ab();
int B::get_a() void D::show_abc() d1.show_a();
{ return a; } { cout<<“a="<<get_a(); d1.mul_ab();
d1.show_abc();
void B::show_a() cout<<"\n b="<<b;
{ cout<<a; } cout<<"\n c="<<c; d1.b = 20;
d1.mul_ab();
} d1.show_abc();
return 0;
}
Sanjivani K.B.P. Polytechnic, Kopargaon Department
21
of Compute Technology P. M. Dhanrao
:SI…d1 accessing b => d1.b = 20;
int main()
{
D d1;
d1.get_ab();
d1.show_a();
d1.b = 20;  As b is the public member of class B is also inherited as a
public member in D
And accessible with the help of object of class D as well
=>d1 object of class D, have access to all the public members of class B
d1.mul_ab();
d1.show_abc();
return 0;
}
Sanjivani K.B.P. Polytechnic, Kopargaon Department
22
of Compute Technology P. M. Dhanrao
d1 accessing private member var a of class
B through function get_a()
void get_a() int main()
{
{ return a; }
D d1;
d1.get_ab();
void mul_ab() d1.show_a();
d1.mul_ab();
{c = b * get_a(); }
d1.show_abc();

void D:: d1.b = 20;


{ cout<<“a=show_abc()"<<get_a(); d1.mul_ab();
d1.show_abc();
cout<<"\n b="<<b <<“\n c=”<<c; return 0;
} }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
23
of Compute Technology P. M. Dhanrao
class D:public B
1) private data a is not inherited
2) data b of B inherited public
getab(), geta() show_a()
inherited from B because public
remains in pulic section in D
3) class D’s own private data c
4) D’s functions mul(), display()
Sanjivani K.B.P. Polytechnic, Kopargaon Department
24
of Compute Technology P. M. Dhanrao
Lets derive class D from B In private mode…

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

// d1.b = 20; // b of B become private in D as D derived in private mode… so d1 cant access b


d1.mul_ab();
d1.show_abc();
return 0;
}

Sanjivani K.B.P. Polytechnic, Kopargaon Department


29
of Compute Technology P. M. Dhanrao
:SI .. Private derivation ONLY FUNCTIONS…

void B::get_ab() void D::mul_ab()


int main()
{
{ a=5; b=10; } get_ab();
{
D d1;
int B::get_a() c = b * get_a(); //d1.get_ab();
//d1.show_a();
{ return a; } }
d1.mul_ab();
void D::show_abc()
void d1.show_abc();
{ cout<<"\n
B::show_a() a="<<get_a(); // d1.b = 20;
{ cout<<"\n a cout<<"\n b="<<b; d1.mul_ab();
d1.show_abc();
="<<a; } cout<<"\n c="<<c; return 0;
} }

Sanjivani K.B.P. Polytechnic, Kopargaon Department


30
of Compute Technology P. M. Dhanrao
:SI .. Private derivation WHOLE PROGRAM
#include <iostream> void B::get_ab() void D::mul_ab() int main()
using namespace { a=5; b=10; }
std; int B::get_a()
{ {
D d1;
class B { return a; } get_ab();
//d1.get_ab();
{ void B::show_a() c = b * get_a(); //d1.show_a();
int a; { cout<<"\n a ="<<a; }
} d1.mul_ab();
public: d1.show_abc();
class D:private B void D::show_abc()
int b;
int get_a();
{ { cout<<"\n // d1.b = 20;
void get_ab();
int c; a="<<get_a(); d1.mul_ab();
public:
void show_a(); cout<<"\n b="<<b; d1.show_abc();
void mul_ab(); return 0;
void show_abc(); cout<<"\n c="<<c;
}
}; }; }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
31
of Compute Technology P. M. Dhanrao
class D:private B
1) private data a is not inherited
2) data b of B inherited it is public
and becomes private in D
3) getab(), geta() show_a() of B
inherited from B because public
and becomes private in D
3) c=> class D’s own private data c
4) D’s functions mul(), display()
Sanjivani K.B.P. Polytechnic, Kopargaon Department
32
of Compute Technology P. M. Dhanrao
class D:public B class D:private B

Sanjivani K.B.P. Polytechnic, Kopargaon Department


33
of Compute Technology P. M. Dhanrao
Lets derive class D from B In protected mode

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?

1) Member declared as Protected are accessible


by the member functions within its class
2) and to any class immediately derived from it.
3) It cannot be accessed by the functions outside
these two classes.

Sanjivani K.B.P. Polytechnic, Kopargaon Department


36
of Compute Technology P. M. Dhanrao
:SI .. Base class B with protected members
#include <iostream> void B::get_ab()
using namespace std;
class B
{ a=5; b=10; }
{ int B::get_a()
private: int a;
protected: int b; { return a; }
public:
int get_a(); void B::show_a()
void get_ab();
void show_a();
{ cout<<"\n a ="<<a; }
};
Sanjivani K.B.P. Polytechnic, Kopargaon Department
37
of Compute Technology P. M. Dhanrao
Base and derived class with their functions

class B class D:protected


{ B void D::mul_ab()
void B::get_ab()
private: int a; { {c = b * get_a(); }
{ a=5; b=10; }
protected: int b int c; void D::show_abc()
int B::get_a() public:
public: { cout<<“a="<<get_a()
{ return a; } void mul_ab();
void get_ab(); cout<<"\n b="<<b;
int get_a(); void B::show_a() void show_abc();
cout<<"\n c="<<c;
void show_a(); { cout<<a; } }; }
};
Sanjivani K.B.P. Polytechnic, Kopargaon Department
38
of Compute Technology P. M. Dhanrao
d1 of D accessing protected members of B

void get_a() int main()


{
{ return a; }
D d1;
d1.get_ab();
void mul_ab() d1.show_a();
d1.mul_ab();
{c = b * get_a(); }
d1.show_abc();

void D:: d1.b = 20;


{ cout<<“a=show_abc()"<<get_a(); d1.mul_ab();
d1.show_abc();
cout<<"\n b="<<b <<“\n c=”<<c; return 0;
} }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
39
of Compute Technology P. M. Dhanrao
Sanjivani K.B.P. Polytechnic, Kopargaon Department
40
of Compute Technology P. M. Dhanrao
Sanjivani K.B.P. Polytechnic, Kopargaon Department
41
of Compute Technology P. M. Dhanrao
class D:public B
class B {
{ not inherited
private: int a;
protected: int b;
protected: int b;
public: int c; public: int c;
} }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
42
of Compute Technology P. M. Dhanrao
class D:private B
class B {
{ not inherited
private: int a;
protected: int b;
private: int b;
public: int c; private: int c;
} }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
43
of Compute Technology P. M. Dhanrao
class D:protected B
class B {
{ not inherited
private: int a;
protected: int b;
protected: int b;
public: int c; protected: int c;
} }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
44
of Compute Technology P. M. Dhanrao
WHO CAN ACCESS THESE DATA
1) Member function: of a derived class.
2) Friend class function: A function that is friend of a class.
=> can access private data as well.
3) Friend function- Member function of a class that is a
friend of the class.
Class B=> ITS FRIEND and MEMBER function can access
private and protected data.
Class D=> Member function of derived class can access only
protected data of base class but not private data.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 45
WHO CAN ACCESS THESE DATA

Sanjivani K.B.P. Polytechnic, Kopargaon Department


46
of Compute Technology P. M. Dhanrao
MULITLEVEL INHERITANCE
// BASE CLASS A

// B DERIVED FROM A

// C DERIVED FORM C

Sanjivani K.B.P. Polytechnic, Kopargaon Department


47
of Compute Technology P. M. Dhanrao
MULITLEVEL INHERITANCE….SYNTAX
class A
{ ….. }; // BASE CLASS

class B: public A
{ …. }; // B derived from A

class C: public B
{ …. }; // C derived from B

Sanjivani K.B.P. Polytechnic, Kopargaon Department


48
of Compute Technology P. M. Dhanrao

You might also like