Single and Multiple Inheritance in C++
Single inheritance is the process of creating new classes from an existing base class. The existing class is known as the direct base class and the newly created class is called as a singly derived class. Single inheritance is the ability of a derived class to inherit the member functions and variables of the existing base class.
Defining the Derived Class The declaration of a singly derived class is as that same of an ordinary class. A derived class consists of the following components:
(i) the keyword class
(ii) the name of the derived class
(iii) a single colon
(iv) the type of derivation (private, public or protected)
(v) the name of the base or parent class
(vi) the remainder of the class defi nition
The derived class inherits the properties of its base classes, including its data member and member functions. The physical_fit is a derived class which has two components — private and public. In addition to its new data members such as height and weight, it may inherit the data members of the base class. The derived class contains not only the methods of its own but also of its base classes.
Example 1.
A program to read the data members of a basic class such as name, roll number and gender from the keyboard and display the contents of the class on the screen. This program does not use any inheritance concepts.
//program 1.cpp
#include <iostream>
#include <iomanip>
using namespace std;
class basic_info {
private :
char name[20];
long int rollno;
char gender ;
public :
void getdata();
void display();
}; // end of class de nition
void basic_info :: getdata()
{
cout << “ enter a name ? \n”;
cin >> name;
cout << “ roll no ? \n”;
cin >> rollno;
cout << “ gender ? \n”;
cin >> gender ;
}
void basic_info :: display()
{
cout << name << “ “;
cout << rollno << “ “;
cout << gender << “ “;
}
int main()
{
basic_info a;
cout << “enter the following information \n”;
a.getdata();
cout << “ ————————————— \n”;
cout << “ Name Rollno gender \n”;
cout << “ ————————————— \n”;
a.display();
cout << endl;
cout << “ ————————————— \n”;
return 0;
}
Output of the above program
enter the following information
enter a name?
Ravich
roll no?
20071
gender ?
M
-----------------------
Name Rollno gender
-----------------------
Ravich 20071 M
-----------------------
Example 2.
A program to read the derived class data members such as name, roll number, gender , height and weight from the keyboard and display the contents of the class on the screen. This program demonstrates a single inheritance concept which consists of a base class and a derived class.
//single inheritance
#include <iostream>
#include <iomanip>
using namespace std;
class basic_info {
private :
char name[20];
long int rollno;
char gender ;
public :
void getdata();
void display();
}; // end of class de nition
class physical_ t :public basic_info
{
private :
oat height;
oat weight;
public:
void getdata();
void display();
}; //end of class de nition
void basic_info :: getdata()
{
cout << “ enter a name ? \n”;
cin >> name;
cout << “ roll no ? \n”;
cin >> rollno;
cout << “ gender ? \n”;
cin >> gender ;
}
void basic_info :: display()
{
cout << name << “ “;
cout << rollno << “ “;
cout << gender << “ “;
}
void physical_ t :: getdata()
{
basic_info::getdata();
cout << “ height ? \n”;
cin >> height;
cout << “ weight ?\n”;
cin >> weight;
}
void physical_ t :: display()
{
basic_info::display();
cout << height << “ “;
cout << weight << “ “;
}
int main()
{
physical_ t a;
cout << “enter the following information \n”;
a.getdata();
cout << “ ———————————————————— \n”;
cout << “ Name Rollno gender Height Weight \n”;
cout << “ ———————————————————— \n”;
a.display();
cout << endl;
cout << “ ———————————————————— \n”;
return 0;
}
Output of the above program
enter the following information
enter a name?
Kandasamy
roll no?
27003
gender ?
M
height?
179
weight?
78
-------------------------------------------
Name Rollno gender Height Weight
-------------------------------------------
Kandasamy 27003 M 179 78
-------------------------------------------
Example 3.
A program to get the information of a derived class data members via name, roll number, gender , height and weight from the keyboard; again to read data for another derived class such as name, roll number, gender , course, semester and rank and display the contents of the newly created class on the screen.
// two derived classes access the same base class
//single inheritance
#include <iostream>
#include <iomanip>
using namespace std;
class basic_info {
private :
char name[20];
long int rollno;
char gender ;
public :
void getbaseinfo();
void dispbaseinfo();
}; // end of class de nition
class physical_ t :public basic_info
{
private :
oat height;
oat weight;
public:
void getphy();
void disphy();
}; //end of class de nition
class academic_ t : public basic_info
{
private :
char course[20];
char semester[10];
int rank;
public :
void getacd();
void dispacd();
}; // end of class de nition
void basic_info :: getbaseinfo()
{
cout << “ enter a name ? \n”;
cin >> name;
cout << “ roll no ? \n”;
cin >> rollno;
cout << “ gender ? \n”;
cin >> gender ;
}
void basic_info :: dispbaseinfo()
{
cout << name << “ “;
cout << rollno << “ “;
cout << gender << “ “;
}
void physical_ t :: getphy()
{
basic_info::getbaseinfo();
cout << “ height ? \n”;
cin >> height;
cout << “ weight ?\n”;
cin >> weight;
}
void physical_ t :: disphy()
{
basic_info::dispbaseinfo();
cout << height << “ “;
cout << weight << “ “;
}
void academic_ t :: getacd()
{
basic_info::getbaseinfo();
cout << “ course name ( BTech/MCA/DCA etc) ?\n”;
cin >> course;
cout << “ semester ( rst/second etc)? \n”;
cin >> semester;
cout << “ rank of the student \n”;
cin >> rank;
}
void academic_ t :: dispacd()
{
basic_info :: dispbaseinfo();
cout << course << “ “ ;
cout << semester << “ “;
cout << rank << “ “;
}
int main()
{
physical_ t p;
academic_ t a;
cout << “enter the following information for physical tness\n”;
p.getphy();
cout << “enter the following information for academic tness\n”;
a.getacd();
cout << “ Physical Fitness of the student \n”;
cout << “ ———————————————————— \n”;
cout << “ Name Rollno gender Height Weight \n”;
cout << “ ———————————————————— \n”;
p.disphy();
cout << endl;
cout << “ ———————————————————— \n”;
cout << endl;
cout << “ Academic performance of the student \n”;
cout << “ ————————————————————————— \n”;
cout << “ Name Rollno gender Course Semester Rank \n”;
cout << “ ————————————————-———————— \n”;
a.dispacd();
cout << endl;
cout << “ ————————————————————————— \n”;
return 0;
}
Output of the above program
enter the following information for physical tness
enter a name?
Velusamy.K
roll no?
27001
gender ?
M
height?
175
weight?
90
enter the following information for academic tness
enter a name?
Velusamy.K
roll no?
27001
gender ?
M
course name ( BTech/MCA/DCA etc)?
B.Tech
semester ( rst/second etc)?
I
rank of the student
2
Physical Fitness of the student
-------------------------------------------------–––
Name Rollno gender Height Weight
-------------------------------------------------–––
Velusamy.K 27001 M 175 90
-------------------------------------------------–––
Academic performance of the student
-----------------------------------------------------------–––
Name Rollno gender Course Semester Rank
-----------------------------------------------------------–––
Velusamy.K 27001 M B.Tech I 2
-----------------------------------------------------------–––

0 comments
Post a Comment