Professional Documents
Culture Documents
COMPBBE39312rObjrBh - OOP UNIT 2 Inheritance
COMPBBE39312rObjrBh - OOP UNIT 2 Inheritance
INHERITANCE AND
POINTERS
1
INHERITANCE
2
NEED AND CONCEPT
• Inheritance is the most powerful feature of object-oriented programming.
• Reusability.
• Using the features(data and/or functions) of one class into another class.
• Inheritance is the process of creating new classes, called derived classes, from
existing or base classes.
• The existing old class is referred as Base(super) class and new class is called
as derived(Sub) class.
A
A A B
B
B
C
Single C
inheritance Multiple inheritance
5
TYPES OF INHERITANCE(CNTD…)
A A
B C
B C D
Hierarchical inheritance
D
Hybrid inheritance
6
DERIVED CLASS DEFINITION
7
INHERITANCE
#include <iostream>
O/P:
using namespace std;
class Parent
child id is: 50
{ Parent id is: 10
public:
int id_P;
};
class child : public Parent
{ public:
int id_C;
};
int main()
{
child obj;
obj.id_P=10;
obj.id_C=50;
cout<<"\nchild id is: "<<obj.id_C;
8
cout<<"\nParent id is: "<<obj.id_P;
return 0;
}
EXAMPLE
class ABC : private XYZ // private derivation
{
// members of ABC
};
Public derivation
• public members of base → public of derived
● Private members are not inheritable
10
SINGLE INHERITANCE : PUBLIC DERIVATION
class B }
{ void D :: mul()
public : }
void put_ab(); {
}; cout<<"b = "<<b<<"\n";
cout<<"c = "<<c<<"\n";
{ int main()
int c; { D d;
public: d.put_ab();
}; d.b=20;
{ a= 5; d.display(); 11
b = 10; return 0;
} }
O/P
a=5
b = 10
c = 50
a=5
b = 20
c = 100
12
PUBLIC DERIVATION
Class D
c Private section
b
Public section
put_ab() inheritable
from B
get_a()
c
Private section
b
put_ab()
inheritable
from B
get_a()
Public section
mul()
Own members of D
display()
14
SINGLE INHERITANCE : PRIVATE DERIVATION
class B }
{ void D :: mul()
public : }
void put_ab(); {
}; cout<<"b = "<<b<<"\n";
cout<<"c = "<<c<<"\n";
{ int main()
int c; { D d;
{ a= 5; d.display(); 15
b = 10; return 0;
} }
INHERITANCE AND VISIBILITY OF MEMBERS
class B : public A
{
// x is public
// y is protected
class A
// z is not accessible from B
{
};
public:
int x;
class C : protected A
protected:
{
int y;
// x is protected
private:
// y is protected
int z;
// z is not accessible from C
};
};
BASE CLASS
VISIBILITY Public Private Protected
Derivation Derivation Derivation
18
ACCESS CONTROL TO THE MEMBERS OF A CLASS
MULTI-LEVEL INHERITANCE
A – Base class
(GrandFather)
C – Derived class
( Child )
Enter value of x= 1
Enter value of y= 2
Enter value of z= 3
Product= 6
22
EXAMPLE
Implement multi- level inheritance using following Information :
Class student :
data → roll no.
functions → get_no, display_no
23
MULTIPLE INHERITANCE
• A class inheriting attributes of two or more classes is called multiple
inheritance
Syntax :
Class D : visibility B-1, visibility B-2, . . . . . .
{
………// members of D
}
24
EXAMPLE
• Base class – 1
● Having one variable
● One function get_data()
• Base class – 2
● Having second variable
● One function get_data()
25
MULTIPLE INHERITANCE
int main()
#include <iostream> {
using namespace std; C obj;
obj.id_A=10;
class A obj.id_B=20;
{ obj.id_sum=obj.id_A+obj.id_B;
public: cout<<"\nAddition of id is: "<<obj.id_sum;
int id_A; return 0;
}
};
class B
{
public:
int id_B;
};
Students
Arts Medical
Enginerring
Computer I. T. Civil
27
HYBRID INHERITANCE
Student
test sports
result
28
EXAMPLE
29
AMBIGUITY IN MULTIPLE INHERITANCE
• If the object of the derived class need to access one of the same named
member function of the base classes then it result in ambiguity as it is
not clear to the compiler which base’s class member function should be
invoked.
• The ambiguity simply means the state when the compiler confused.
30
AMBIGUITY IN MULTIPLE INHERITANCE
#include <iostream>
using namespace std;
• this throws a compile time error because the call to function show() is ambiguous 31
USING SCOPE RESOLUTION
TO RESOLVE AMBIGUITY IN MULTIPLE INHERITANCE
#include <iostream>
using namespace std;
32
FUNCTION OVERRIDING
33
#include<iostream>
using namespace std;
class BaseClass
{ public:
void Display()
{
cout<<"\n\tThis is Display() method of BaseClass";
}
void Show()
{
cout<<"\n\tThis is Show() method of BaseClass";
}
};
class DerivedClass : public BaseClass
{ public:
void Display() //overriding method - new working of base
class's display method
{ //BaseClass::Display();
cout<<"\n\tThis is Display() method of DerivedClass"; 34
}
};
VIRTUAL BASE CLASS
Grandparent
Parent 1 Parent 2
Child
36
//Virtual base class class C : virtual public A
#include <iostream> {
using namespace std; public:
class A C()
{ public: { cout << "\n Class C"<< endl;
A() }
{ cout << "\n Class A"; };
} class D : public B,public C
void fun() {
{ public:
cout << "\n Fun....."; D()
} { cout << "\n Class D"<< endl;
}; }
class B : virtual public A };
{
public: int main() O/P
B() {
Class A
{ D d1; Class B
d1.fun(); Class C 37
cout << "\n Class B"<< endl;
Class D
} return 0; Fun.....
}; }
VIRTUAL BASE CLASS EXAMPLE
Student
test sports
result
38
PROGRAM
Class student – roll_no, get_no(), display_no()
39
CONSTRUCTOR AND DESTRUCTOR IN DERIVED CLASS
• When an object of the derived class is created ,the compiler first call the base
class constructor and then the constructor of the derived class.
• This because the derived class is built up on the members of the base class.
• When the object of a derived class expires first the derived class destructor is
invoked followed by the base class destructor.
40
#include<iostream> class derivedClass: public baseClass
{ {
baseClass() }
{ ~derivedClass()
~baseClass() }
{ };
}; {
derivedClass D;
return 0;
}
O/P
I am baseClass constructor
41
I am derivedClass constructor
I am derivedClass destructor
I am baseClass destructor
#include<iostream>
using namespace std;
class baseClass
{int var1;
public:
baseClass(int x)
{
var1=x;
cout << "Parameterised constructor of baseClass and var1=" << var1<<endl;
}
};
class derivedClass: public baseClass
{int var2;
public:
derivedClass(int x,int y):baseClass(x)
{
var2=y;
cout << "Parameterised constructor of derivedClass and var2=" << var2<<endl;
}
};
int main()
{ O/P
42
derivedClass D(10,20); Parameterised constructor of baseClass and var1=10
Parameterised constructor of derivedClass and var2=20
return 0;
}
CONSTRUCTOR AND DESTRUCTOR IN DERIVED CLASS
• In inheritance,
43
NESTED CLASS
• A nested class is a class which is declared in another enclosing class.
• A nested class is a member and as such has the same access rights as any other
member.
• Like any member of its enclosing class, the nested class has access to all names
(private, protected, etc) to which the enclosing class has access.
• Nested classes can be forward-declared and later defined, either within the
same enclosing class body, or outside of it:
44
//Out-of-class definitions of the members of a nested class
#include<iostream>
using namespace std;
{ {
class B obj.get(100);
{ return 0;
int no; }
public:
void get (int s);
};
}; O/P
void A::B::get (int s)
no=100
{
no=s;
cout << "no=" << no<<endl;
}
45
//Nested class can be forward-declared and later defined
#include<iostream>
using namespace std;
int main()
class A {
A ::B obj;
{
public: obj.get(100);
return 0;
class B;
}
};
class A::B
{
int no;
public:
void get (int s) O/P
{
no=100
no=s;
cout << "no=" << no<<endl;
}
};
46