unit-4
unit-4
When one class inherits another, the members of the base class become the
members of the derived class.
A derived class can be defined by specifying its relationship with the base class
in addition to its own details.
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.
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.
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
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();
}
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.
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
};
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
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];
};
int regno;
};
void main()
student s;
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
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
}
#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.