0% found this document useful (0 votes)
14 views

unit-4

Uploaded by

sahayaanbu907
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

unit-4

Uploaded by

sahayaanbu907
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT 4

INHERITANCE: EXTENDING CLASSES


INTRODUCTION
C++ strongly supports the concept of reusability. The C++ classes can be
reused in several ways. Once a class has been written and tested, it can be adapted
by other programmers to suit their requirements. This is basically done by creating
new classes, reusing the properties of the existing ones. The mechanism of deriving a
new class from an old class is called inheritance. The old class is called base class and
the new class is called derived class or sub class.

When one class inherits another, the members of the base class become the
members of the derived class.

Q.No.1 DEFINING DERIVED CLASS

A derived class can be defined by specifying its relationship with the base class
in addition to its own details.

The syntax for defining a derived class is

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


{
Body of the class
}

The colon (:) indicates that the derived-class-name is derived from the base-
class-name. The visibility-mode is optional and it may be either private or public. The
default visibility-mode is private. Visibility-mode specifies whether the features of
the base class are privately derived or publicly derived.

When a base class is privately inherited by a derived class, ‘public members’ of


the base class become ‘private members’ of the derived class and therefore the
public members of the base class can only be accessed by the member functions of
the derived class. They are inaccessible to the objects of the derived class.

On the other hand, when a base class is publicly inherited by a derived class,
‘public members’ of the base class become ‘public members’ of the derived class and
therefore they are accessible to the objects of the derived class.

In both the cases, the private members are not inherited and therefore, the
private members of a base class will never become the members of its derived class.

In inheritance, some of the base class elements and members functions are
‘inherited’. We can add our own data and member functions and thus extending the
functionality of the base class.

Q.No.2 FORMS OF INHERITANCE

The various forms of inheritance are,

1) Single inheritance
2) Multiple inheritance
3) Multilevel inheritance
4) Hybrid inheritance
5) Hierarchical inheritance

1 ) Single inheritance

A derived class with only one base class is called single inheritance. In this, the
existing class is called as base class and the newly generated class is called as derived
class.

A Base class

Derived class
B
Syntax:

class base
{
Body of statements
};
class derived: visibility-mode base
{
Body of the class
};
Example program:

#include<iostream.h>
#include<conio.h>
class student
{
public:
int regno;
char name[20];
void input()
{
cout<<”Enter the regno and Name”;
cin>>regno>>name;
}
void display()
{
cout<<”Register number and Name are”;
cout<<regno<<name;
}
};
class physical : public student
{
float height, weight;
public:
void getdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void show()
{
cout<<”Height and Weight are”;
cout<<height<<weight;
}
};
void main()
{
Physical p;
p.input();
p.getdata();
p.display();
p.show();
getch();
}
The output will be displayed as shown below

Enter the regno and Name


11
Babu
Enter the height and weight
9.2
55.5
Register number and Name are
11 Babu
Height and Weight are
9.2 55.5

The class ‘physical’ is a public derivation of the base class ’student’. Therefore,
‘physical’ inherits all the public members of the ‘student’. Thus, a public member of
the base class ‘student’ is also public member of the derived class ‘physical’. The
private members of the ‘student’ cannot be inherited by ‘physical’.

2) Multiple inheritance

The process of deriving a class from more than one base class is called
multiple inheritance. It combines the features of several classes into one class.
Syntax:

class base1
{
Body of statements
};
class base2
{
Body of statements
};
class derived : visibility-mode base1, visibility-mode base2,……..
{
Body of the class
};
Where,
Visibility may be either public or private. The base classes are separated by commas.
Example:
#include<iostream.h>
#include<conio.h>
class student
{
int regno;
char name[20];
public:
void input()
{
cout<<”Enter the regno”;
cin>>regno>>name;
}
void output()
{
cout<<”Register number and Name”;
cout<<regno<<name;
}
};
class personal
{
char address[20]
long int phoneno;
public:
void getdata()
{
cout<<”Enter the address and phone number”;
cin>>address>>phoneno;
}
void setdata()
{
cout<<” Address,Phone Number”;
cout <<address<<phoneno;
}
};
class physical : public student, public personnal
{
float height, weight;
public:
void readdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void writedata()
{
cout<<” Height and Weight are”;
cout <<height<<weight;
}
};
void main()
{
physical p;
p.input();
p.getdata();
p.setdata();
p.output()
p.setdata();
p.writedata();
getch();
}

The output will be displayed as shown below


Enter the regno and Name
11
babu
Enter the address and phone number
Nagercoil
8732452128
Enter the height and weight
9.2
55.5
Register number, Name, Sex, Height and Weight are
11 Babu Nagercoil 8732452128 9.2 55.5
3) Multilevel inheritance

The process of deriving a class from another derived class is known as


multilevel inheritance.

Base
A

B Derived1

Derived2
C

In the above diagram, class A serves as a base class for B. Class B acts as a base class
for C.
Syntax:
class base
{
Body of statements
};
class derived1 : visibility-mode base
{
Body of statements
};
class derived2 : visibility-mode derived1
{
Body of the class
};
Example program:
#include<iostream.h>
#include<conio.h>
class student
{
int regno;
char name[20];
public:
void input()
{
cout<<”Enter the regno and name”;
cin>>regno>>name;
}
void output()
{
cout<<”Register number and Name”;
cout<<regno<<name;
}
};
class physical : public student
{
float height, weight;
public:
void readdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void writedata()
{
cout<<” Height and Weight are”;
cout <<height<<weight;
}
};
class marks : public physical
{
int m1,m2,m3;
public:
void getdata()
{
cout<<”Enter the 3 subject marks”;
cin>>m1>>m2>>m3;
}
void setdata()
{
cout<<” Marks are”;
cout<< m1<<m2<<m3;
}
};
void main()
{
marks m;
m.input();
m.getdata();
m.readdata();
m.setdata()
m.output();
m.setdata();
m.writedata();
getch();
}

Output:
Enter the regno and Name
11
Enter the height and weight
9.2
55.5
Enter the 3 subjects marks
50
70
85
Register number and Name, Height, Weight and Marks are
11 Babu 9.2 55.5 50 70 85

4. Hierarchical inheritance
The process of deriving classes in a hierarchical manner or the process of
deriving more than one class from a single base class is called hierarchical
inheritance.

In hierarchical inheritance, a base class contains all the information that is


common to the sub classes. A subclass is derived from this base class. This sub class
can be the base for the next lower classes and so on.
Syntax:
class base
{
Body of statements
};
class derived1 : visibility-mode base
{
Body of statements
};
class derived2 : visibility-mode base
{
Body of the class
};
class derived3 : visibility-mode base
{
Body of the class
};

Example program:
#include<iostream.h>
#include<conio.h>
class student
{
int regno;
char name[20];
public:
void input()
{
cout<<”Enter the regno and name”;
cin>>regno>>name;
}
void output()
{
cout<<”Register number and Name”;
cout<<regno<<name;
}

};
class physical : public student
{
float height, weight;
public:
void getdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void setdata()
{
cout<< “Height, Weight are”;
cout <<height<<weight;
}
};
class marks : public student
{
int m1,m2,m3;
public:
void readdata()
{
cout<<”Enter the 3 subject marks”;
cin>>m1>>m2>>m3;
}
void writedata()
{
cout<<” Three subjects Marks are”;
cout<<m1<<m2<<m3;
}
};
class address : public student
{
char place[20],dist[10];
public:
void getaddress()
{
cout<<”Enter the place and district”;
cin>>place>>dist;
}
void setaddress()
{
cout<<”Place and District are”;
cout<<place<<dist;
}
};
void main()
{
physical p;
marks m;
address a;
p.input();
p.getdata();
m.readdata();
a.getaddress();
p.output();
m.setdata();
a.writedata();
a.setaddress();
getch();
}
Output:
Enter the regno and Name
11
Enter the height and weight
9.2
55.5
Enter the 3 subjects marks
50
70
85
Enter the place and district
Nagercoil
kanyakumari
Register number and Name, Height, Weight are
11 Babu 9.2 55.5
Three subjects Marks are
50 70 85
Place and District are
Nagercoil
kanyakumari

6) Hybrid Inheritance
Derivation of a class involving more than one form of inheritance is
called hybrid inheritance. It is a combination of multilevel and multiple
inheritance.

B C

D
Syntax:

class base1
{
Body of statements
};
class derived1 : visibility-mode base1
{
Body of statements
};
class base2
{
Body of statements
};

class derived2 : visibility-mode derived1, visibility-mode base2


{
Body of the class
};

Example Program:
#include<iostream.h>
#include<conio.h>
class student
{
int regno;
char name[20];
public:
void input()
{
cout<<”Enter the regno and name”;
cin>>regno>>name;
}
void output()
{
cout<<”Register number and Name”;
cout<<regno<<name;
}

};
class physical : public student
{
float height, weight;
public:
void getdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void setdata()
{
cout<< “Height, Weight are”;
cout <<height<<weight;
}
};
class marks
{
int m1,m2,m3;
public:
void readdata()
{
cout<<”Enter the 3 subject marks”;
cin>>m1>>m2>>m3;
}
void writedata()
{
cout<<” Three subjects Marks are”;
cout<<m1<<m2<<m3;
}
};
class address : public student
{
char place[20],dist[10];
public:
void getaddress()
{
cout<<”Enter the place and district”;
cin>>place>>dist;
}
void putaddress()
{
cout<< “ Place and district are”;
cout<<place<<dist;
}
};
void main()
{
address a;
a.input();
a.getdata();
a.readdata();
a.getaddress();
a.output();
a.setdata();
a.writedata();
a.putaddress();
getch();
}

Output:
Enter the regno and Name
11
Enter the height and weight
9.2
55.5
Enter the 3 subjects marks
50
70
85
Enter the place and district
Nagercoil
Kanyakumari
Register number and Name, Height, Weight ,3 subjects marks , Place and district
are
11 Babu 9.2 55.5 50 70 85 Nagercoil Kanyakumari

VIRTUAL BASE CLASS


If a class is derived from two derived classes, which are derived from same
base class then two copies of the members of primary base class are copied into
resultant derived class. This problem can occur when we combine more than one
types of inheritance.

In
In the above diagram, all the public and protected members of the ‘grandparent’ are
inherited into ‘child’ twice, first via ‘parent1’ and again via ‘parent2’. This means,
‘child’ would have duplicate sets of the members inherited from ‘grandparent’. This
introduces ambiguity and should be avoided.

This problem can be avoided by making the common base class as virtual base
class while inheriting the base classes.

Syntax:

class base
{
Body of statements
};
class derived1 : virtual visibility-mode base
{
Body of statements
}
class derived2 : virtual visibility-mode base
{
Body of the class
}
class derived: visibility-mode derived1, visibility-mode derived2
{
Body of the class
}

#include<iostream.h>
#include<conio.h>
class student
{
int regno;
char name[20];
public:
void input()
{
cout<<”Enter the regno and name”;
cin>>regno>>name;
}
void output()
{
cout<<”Register number and Name”;
cout<<regno<<name;
}
};
class physical : virtual public student
{
float height, weight;
public:
void getdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void setdata()
{
cout<< “Height, Weight are”;
cout <<height<<weight;
}
};
class marks :virtual public student
{
int m1,m2,m3;
public:
void readdata()
{
cout<<”Enter the 3 subject marks”;
cin>>m1>>m2>>m3;
}
void writedata()
{
cout<<” Three subjects Marks are”;
cout<<m1<<m2<<m3;
}

};
class address : public physical, public marks
{
char place[20],dist[10];
public:
void getaddress()
{
cout<<”Enter the place and district”;
cin>>place>>dist;
}
void display()
{
cout<< “Place and district are”;
cout<<place<<dist;
}
};
void main()
{
address a;
a.input();
a.getdata();
a.readdata();
a.getaddress();
a.output();
a.setdata();
a.writedata();
a.display();
getch();
}
Enter the regno and Name
11
Enter the height and weight
9.2
55.5
Enter the 3 subjects marks
50
70
85
Enter the place and district
Nagercoil
Kanyakumari
Register number and Name, Height, Weight ,3 subjects marks , Place and district
are
11 Babu 9.2 55.5 50 70 85 Nagercoil Kanyakumari

ABSTARCT CLASS
An abstract class is one that is not used to create object. An abstract class is
designed only to act as a base class and it can be inherited by other classes.

Example:

class college

char collegename[10];

};

class student: public college

int regno;

};

void main()

student s;

In the above example, no object is created for the class ‘college’.


But ‘college’ class is used only to derive a class ‘student’. So, class ‘college’ is called
abstract class.

Program:

#include<iostream.h>
#include<conio.h>
class student
{
public:
int regno;
char name[20];
void input()
{
cout<<”Enter the regno and Name”;
cin>>regno>>name;
}
void display()
{
cout<<”Register number and Name are”;
cout<<regno<<name;
}
};
class derived : public student
{
float height, weight;
public:
void getdata()
{
cout<<”Enter the height and weight”;
cin>>height>>weight;
}
void show()
{
cout<<”Height and Weight are”;
cout<<height<<weight;
}
};
void main()
{
derived d;
d.input();
d.getdata();
d.display();
d.show();
getch();
}

Output:
Enter the regno and Name
11
Babu
Enter the height and weight
9.2
55.5
Register number and Name are
11 Babu
Height and Weight are
9.2 55.5

CONSTRUCTORS IN DERIVED CLASS


While applying inheritance, we usually create objects using the derived class. So,
it is not possible to initialize the data members in the base class using constructors.
But the derived class can pass the arguments to the base class constructor. When
both the derived and base classes contain constructors, the base class constructor is
executed first and then the constructor in the derived class is executed.

The constructor of the derived class receives the entire list of values s its
arguments and passes them on to the base constructors in the order in which they
are declared in the derived class. The base constructors are called and executed
before executing the statements in the body of the derived constructor.

Syntax:

Derived-constructor(arglist1,arglist2,…….arglistN,arglistD :
base1(arglist1),
base2(arglist2),
…….
………
……..
baseN(arglistN)
{
Body of derived class
}

Where, base1,base2,baseN --->base class constructors

Arglist1,arglist2,arglist3……arglistN -----> the list of arguments to be passed


for base class

arglistD -------> the list of arguments to initialize derived class

The header line of derived-constructor function contains two parts separated by


colon(:). The first part provides the declaration of the arguments that are passed to
the derived-constructor and the second part lists the function calls to the base
constructors.
Example

#include<iostream.h>
#include<conio.h>
class base1
{
int x;
public:
base1(int x1)
{
x=x1;
}
void display1()
{
cout<<”x=”<<x;
}
};
class base2
{
int y;
public:
base2(int y1)
{
y=y1;
}
void display2()
{
cout<<”y=”<<y;
}
};
class derived : public base1, public base2
{
int z;
public:
derived(int x1, int y1, int z1) : base1(x1), base2(y1)
{
z=z1;
}
void display3()
{
cout<<”z=”<<z;
}
};
void main()
{
derived d(10,20,30);
d.display1();
d.display2();
d.display3();
getch();
}
Output:
x=10
y=20
z=30
MEMBER CLASSES : NESTING OF CLASSES
Inheritance is the mechanism of deriving certain properties of one class into
another. C++ supports another way of inheriting properties of one class into another.
That is, a class can contain objects of other classes as its members.
Example:
class one
{

};

class two
{

};
class three
{
one o; // ’o’ is an object of one class
two t; // ‘t’ is an object of two class
int a; //data member
};
All objects of ‘three’ class will contain the objects ‘o’ and ‘t’. This kind of relationship
is called nesting or containership.

MAKING A PRIVATE MEMBER INHERITABLE


C++ provides a third visibility modifier, protected, which serve a limited
purpose in inheritance. A member declared as protected is accessible by the member
functions within its class and any class immediately derived from it. It cannot be
accessed by the functions outside these two classes.
E4
class example
{
private : // visible to member functions within its class
…………
……………
protected : //visible to member functions of its own and derived class
………………….
………………….
public : // visible to all functions in the program
…………..
…………………..
};
When a protected member is inherited in public mode, it becomes protected in the
derived class too and therefore is accessible by the member functions o the derived
class. It is also ready or further inheritance. Protected member, inherited in private
mode becomes private in the derived class. Although it is available to the member
functions of the derived class, it is not available for further inheritance.
The following figure shows the pictorial representation for the two levels of
derivation.
The below table summarizes the visibility of inherited members.

You might also like