Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

UNIT II

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.

• An important result of reusability is the ease of distributing class libraries.


• A programmer can use a class created by another person or company and
without modifying it, derive other classes from it that are suited to particular
situations.
3
4
TYPES OF INHERITANCE

A
A A B

B
B
C

Single C
inheritance Multiple inheritance

Multi level inheritance

5
TYPES OF INHERITANCE(CNTD…)

A A

B C
B C D

Hierarchical inheritance
D

Hybrid inheritance

6
DERIVED CLASS DEFINITION

• General form can written as :

class derived-class-name : visibility-mode base-class-name


{
……//
……// Members of derived class
……//
};

Colon indicates derivation, by default it is private

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

class ABC : public XYZ // public derivation


{
// members of ABC
};

class ABC : XYZ // private derivation by default


{
// members of ABC
}; 9
PRIVATE AND PUBLIC DERIVATION
Private derivation
• Public members of base → private of derived
● So public members of base(now private of derived) can be accessed by
member functions of derived
● Private members are not inheritable

Public derivation
• public members of base → public of derived
● Private members are not inheritable

10
SINGLE INHERITANCE : PUBLIC DERIVATION

#include <iostream> int B :: get_a()

using namespace std; { return a;

class B }

{ void D :: mul()

int a; // private, not inheritable { c = b * get_a();

public : }

int b; void D :: display()

void put_ab(); {

int get_a(); cout<<"a = "<<get_a()<<"\n";

}; cout<<"b = "<<b<<"\n";
cout<<"c = "<<c<<"\n";

class D : public B // public derivation }

{ int main()

int c; { D d;

public: d.put_ab();

void mul(); d.mul();

void display(); d.display();

}; d.b=20;

void B :: put_ab() d.mul();

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

mul() display() Own members of D 13


PRIVATE DERIVATION
Class D

c
Private section
b

put_ab()
inheritable
from B
get_a()

Public section
mul()
Own members of D
display()
14
SINGLE INHERITANCE : PRIVATE DERIVATION

#include <iostream> int B :: get_a()

using namespace std; { return a;

class B }

{ void D :: mul()

int a; // private, not inheritable { c = b * get_a();

public : }

int b; void D :: display()

void put_ab(); {

int get_a(); cout<<"a = "<<get_a()<<"\n";

}; cout<<"b = "<<b<<"\n";
cout<<"c = "<<c<<"\n";

class D : private B // privatederivation }

{ int main()

int c; { D d;

public: d.put_ab(); //won't work

void mul(); d.mul();

void display(); d.display();

}; d.b=20; //won't work

void B :: put_ab() d.mul();

{ 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
};
};

class D : private A // 'private' is default for classes


{
// x is private
// y is private
// z is not accessible from D
};
16
EFFECT OF INHERITANCE ON VISIBILITY OF MEMBERS
VISIBILITY OF INHERITED MEMBERS

DERIVED CLASS VISIBILITY

BASE CLASS
VISIBILITY Public Private Protected
Derivation Derivation Derivation

Private Not inherited Not inherited Not inherited

Protected Protected Private Protected

Public Public Private Protected

18
ACCESS CONTROL TO THE MEMBERS OF A CLASS
MULTI-LEVEL INHERITANCE

A – Base class
(GrandFather)

B – Intermediate base class


(Father)

C – Derived class
( Child )

Class A {……..}; // Base class A


Class B : public A{……}; // B derived from A
Class C : public B{……}; // C derived from B 20
MULTI-LEVEL INHERITANCE
class derive2 : public derive1 // derived from
#include <iostream> {
using namespace std; private:
class base //single base class int z;
{ public:
public: void indata()
int x; {
void getdata() cout << "\nEnter value of z= ";
{ cin >> z;
cout << "Enter value of x= "; }
cin >> x; void product()
} {
}; cout << "\nProduct= " << x * y * z;
class derive1 : public base // derived class from base class }
{ };
public: int main()
int y; {
void readdata() derive2 a; //object of derived class
{ a.getdata();
cout << "\nEnter value of y= "; a.readdata();
cin >> y; a.indata();
} a.product(); 21
}; return 0;
}
O/P

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

Class test inherits from student


data → sub1, sub2
functions→ get_marks, display_marks

Class result inherits from test


data→ total
functions→ display_total

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
}

The base classes are separated by comma

24
EXAMPLE
• Base class – 1
● Having one variable
● One function get_data()
• Base class – 2
● Having second variable
● One function get_data()

• Derived class inheriting from these base classes and performing


multiplication of these two values and displaying results

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

class C : public A,public B


{ public:
int id_sum; O/P
};
Addition of id is: 30
26
HIERARCHICAL INHERITANCE

Students

Arts Medical

Enginerring

Computer I. T. Civil
27
HYBRID INHERITANCE

Student

test sports

result

28
EXAMPLE

Student – roll no. – get and display()

Test inherits student – sub1, sub2 – get and display()

Sports – score – get_score() and display_score()

Result inherits test, sports – total – display()

29
AMBIGUITY IN MULTIPLE INHERITANCE

• In multiple inheritance, there may be possibility that a class may inherit


member functions with same name from two or more base classes and
the derived class may not have functions with same name as those of
its base classes.

• 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;

class A class C:public A, public B


{ {
public: };
void show()
{ int main()
cout << "A" << endl; {
} C obj;
}; obj.show();
}
class B
{
public:
void show() {
cout << "B" << endl;
}
};

• 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;

class A class C:public A, public B


{ {
public: };
void show()
{ int main()
cout << "A" << endl; {
} C obj;
}; obj.A::show();
}
class B
{
public:
void show() {
cout << "B" << endl;
}
};

32
FUNCTION OVERRIDING

• It is the redefinition of base class function in its derived class with


same signature

● i.e return type and parameters.

• It can only be done in derived class.

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

• In above case all the public as well as protected members of grandparent


are inherited ‘twice’ , so child would have duplicate copies.

• Can be avoided by making common base class(grandparent in this case) as


virtual base class
35
EXAMPLE VBC
Class A { // grandparent
};

Class B1 : virtual public A // Parent 1


{
};

Class B2 : virtual public A // parent 2


{
}
Class C : public B1, public B2 // child
{ // only one copy of A will be inherited
}

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

Class test : virtual public student


-- sub1, sub2, get_marks(), display_marks()

Class sports : public virtual student


-- score, get_score(), display_score()

Class result : public test, public sports


-- total, display_total()

39
CONSTRUCTOR AND DESTRUCTOR IN DERIVED CLASS

• Constructors are not inherited. However, when an object of a derived class is


being created, its base class must be constructed first.

• 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

using namespace std; {


public:

class baseClass derivedClass()

{ {

public: cout << "I am derivedClass constructor" << endl;

baseClass() }

{ ~derivedClass()

cout << "I am baseClass constructor" << endl; {

} cout <<" I am derivedClass destructor" << endl;

~baseClass() }

{ };

cout << "I am baseClass destructor" << endl;


} int main()

}; {
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,

1. order of constructors calling is from child class to parent class.

2. order of constructor's execution is from parent class to child class.

3. order of destructors calling is from child class to parent class.

4. order of destructors execution is from child class to parent class.

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.

• A declaration of a class or struct or union may appear within another class.

• 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.

• Out-of-class definitions of the members of a nested class appear in the


namespace of the enclosing class.

• 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 A int main()

{ {

public: A ::B obj;

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

You might also like