Single and Multiple Inheritance
It is well known that the C++ is a tool for object-oriented programming that supports most of the OOP features such as data hiding, data encapsulation, inheritance, polymorphism, virtual functions, etc. Data hiding and encapsulation are important features of the object-oriented programming and how these concepts are implemented using classes. In some OOP languages such as Simula, the base and derived classes are called as super and subclasses respectively. The derived class inherits all capabilities of the base class. It can also add some more features to this class. The base class is unchanged by its process.
The main advantages of the inheritance are:
- reusability of the code,
- to increase the reliability of the code, and
- to add some enhancements to the base class.
Once the base class is written and debugged, it need not be changed again when there are circumstances to add or modify the member of the class.
Single Inheritance
Example 1.
A program to read the data members of a basic class such as name, roll number and sex from the keyboard and display the contents of the class on the screen. This program does not use any inheritance concepts.
#include <iostream>
#include <iomanip>
using namespace std;
class basic_info {
private :
char name[20];
long int rollno;
char sex;
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 << “ sex ? \n”;
cin >> sex;
}
void basic_info :: display()
{
cout << name << “ “;
cout << rollno << “ “;
cout << sex << “ “;
}
int main()
{
basic_info a;
cout << “enter the following information \n”;
a.getdata();
cout << “ ————————————— \n”;
cout << “ Name Rollno Sex \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
sex?
M
-----------------------
Name Rollno Sex
-----------------------
Ravich 20071 M
-----------------------
Example 2.
A program to read the derived class data members such as name, roll number, sex, 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 sex;
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 << “ sex ? \n”;
cin >> sex;
}
void basic_info :: display()
{
cout << name << “ “;
cout << rollno << “ “;
cout << sex << “ “;
}
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 Sex 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
sex?
M
height?
179
weight?
78
-------------------------------------------
Name Rollno Sex 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, sex, height and weight from the keyboard; again to read data for another derived class such as name, roll number, sex, 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 sex;
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 << “ sex ? \n”;
cin >> sex;
}
void basic_info :: dispbaseinfo()
{
cout << name << “ “;
cout << rollno << “ “;
cout << sex << “ “;
}
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 Sex 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 Sex 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
sex?
M
height?
175
weight?
90
enter the following information for academic tness
enter a name?
Velusamy.K
roll no?
27001
sex?
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 Sex Height Weight
-------------------------------------------------–––
Velusamy.K 27001 M 175 90
-------------------------------------------------–––
Academic performance of the student
-----------------------------------------------------------–––
Name Rollno Sex Course Semester Rank
-----------------------------------------------------------–––
Velusamy.K 27001 M B.Tech I 2
-----------------------------------------------------------–––
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos
0 comments
Post a Comment