Multiple Inheritance in C++
In the previous section, we have seen how a derived class could inherit more enhancements and additional features from the base class. In the original implementation of C++, a derived class could inherit from only one base class. Even with this restriction, the object-oriented paradigm is a flexible and powerful programming tool. The latest version of the C++ compiler implements the multiple inheritance. In this section, how a class can be derived from more than one base class is explained.
Multiple inheritance is the process of creating a new class from more than one base classes. The syntax for multiple inheritance is similar to that of single inheritance. For example, the following program segment shows how a multiple inheritance is defi ned:
class baseA {
-------
-------
};
class baseB {
-------
-------
};
class C : public baseA,public baseB
{
-------
-------
};
The class C is derived from both classes baseA and baseB. Multiple inheritance is a derived class declared to inherit properties of two or more parent classes (base classes). Multiple inheritance can combine the behaviour of multiple base classes in a single derived class. Multiple inheritance has many advantages over the single inheritance such as rich semantics and the ability to directly express complex structures. In C++, derived classes must be declared during the compilation of all possible combinations of derivations and the program can choose the appropriate class at run time and create object for the application.
Example 6.
A program to illustrate how a multiple inheritance can be declared and defi ned in a program. This program consists of two base classes and one derived class. The base class basic_info contains the data members: name, roll number and sex. Another base class academic_fi t contains the data members: course, semester and rank. The derived class fi nancial_assit contains the data member amount besides the data members of the base classes. The derived class has been declared as public inheritance. The member functions are used to get information of the derived class from the keyboard and display the contents of class objects on the screen.
//multiple inheritance
#include <iostream>
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 academic_ t {
private :
char course[20];
char semester[10];
int rank;
public :
void getdata();
void display();
}; // end of class de nition
class nancial_assit : private basic_info, private academic_ t
{
private :
oat amount ;
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 academic_ t :: getdata()
{
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 :: display()
{
cout << course << “ “ ;
cout << semester << “ “;
cout << rank << “ “;
}
void nancial_assit :: getdata()
{
basic_info:: getdata();
academic_ t::getdata();
cout << “ amount in rupees ?\n”;
cin >> amount;
}
void nancial_assit :: display()
{
basic_info:: display();
academic_ t::display();
cout << amount << “ “;
}
int main()
{
nancial_assit f;
cout << “enter the following information for nancial assistance\n”;
f.getdata();
cout << endl;
cout << “ Academic Performance for Financial Assistance \n”;
cout << “ ———————————————————————————— \n”;
cout << “ Name Rollno Sex Course Semester Rank Amount \n”;
cout << “ ———————————————————————————— \n”;
f.display();
cout << endl;
cout << “ ———————————————————————————— \n”;
return 0;
}
Output of the above program
enter the following information for nancial assistance
enter a name?
AjitKumar
roll no?
20071
sex?
M
course name (BTech/MCA/DCA etc)?
MCA
semester ( rst/second etc)?
First
rank of the student
3
amount in rupees?
10000
Academic Performance for Financial Assistance
----------------------------------------------------------------
Name Rollno Sex Course Semester Rank Amount
----------------------------------------------------------------
AjitKumar 20071 M MCA First 3 10000
----------------------------------------------------------------
Example 7.
A program consists of two base classes and one derived class. The base class basic_info contains the data members: name, roll number and sex. Another base class academic_fi t contains the data members: course, semester and rank. The derived class fi nancial_assit contains the data member amount besides the data members of the base classes. The derived class has been declared as an array of class objects. The member functions are used to get information of the derived class from the keyboard and display the contents of the array of class objects on the screen.
//multiple inheritance using array of objects
#include <iostream>
using namespace std;
const int MAX = 100;
class basic_info {
private :
char name[20];
long int rollno;
char sex;
public :
void getdata();
void display();
}; // end of class de nition
class academic_ t {
private :
char course[20];
char semester[10];
int rank;
public :
void getdata();
void display();
}; // end of class de nition
class nancial_assit : private basic_info, private academic_ t
{
private :
oat amount ;
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 academic_ t :: getdata()
{
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 :: display()
{
cout << course << “ “ ;
cout << semester << “ “;
cout << rank << “ “;
}
void nancial_assit :: getdata()
{
basic_info:: getdata();
academic_ t::getdata();
cout << “ amount in rupees ?\n”;
cin >> amount;
}
void nancial_assit :: display()
{
basic_info:: display();
academic_ t::display();
cout << amount << “ “;
}
int main()
{
nancial_assit f[MAX];
int n;
cout << “ How many students ?\n”;
cin >> n;
cout << “enter the following information for nancial assistance\n”;
for (int i = 0; i <= n-1; ++i) {
cout << “ Record No : “ << i+1 << endl;
f[i].getdata();
cout << endl;
cout << endl;
cout << “ Academic Performance for Financial Assistance \n”;
cout << “ ———————————————————————————— \n”;
cout << “ Name Rollno Sex Course Semester Rank Amount \n”;
cout << “ ———————————————————————————— \n”;
for (int i = 0; i <= n-1; ++i) {
f[i].display();
cout << endl;
}
cout << “ ———————————————————————————— \n”;
return 0;
}
Output of the above program
How many students?
3
enter the following information for nancial assistance
Record No: 1
enter a name?
Fateema
roll no?
20071
sex?
F
course name ( BTech/MCA/DCA etc)?
DCA
semester ( rst/second etc)?
Second
rank of the student
1
amount in rupees?
20000
Record No: 2
enter a name?
ArulRaj
roll no?
20072
sex?
M
course name ( BTech/MCA/DCA etc)?
DCA
semester ( rst/second etc)?
First
rank of the student
3
amount in rupees?
15000
Record No: 3
enter a name?
Sampath
roll no?
20075
sex?
M
course name ( BTech/MCA/DCA etc)?
DCA
semester ( rst/second etc)?
First
rank of the student
2
amount in rupees?
22000
Academic Performance for Financial Assistance
--------------------------------------------------------
Name Rollno Sex Course Semester Rank Amount
--------------------------------------------------------
Fateema 20071 F DCA Second 1 20000
ArulRaj 20072 M DCA First 3 15000
Sampath 20075 M DCA First 2 22000
--------------------------------------------------------
Example 8.
A program to demonstrate how ambiguity is avoided in multiple inheritance using scope resolution operator.
using namespace std;
class baseA {
public:
int a;
};
class baseB {
public:
int a;
};
class baseC {
public :
int a;
};
class derivedD : public baseA,public baseB,public baseC
{
public :
int a;
};
int main()
{
derivedD objd;
objd.a = 10;
objd.baseA::a = 20;
objd.baseB::a = 30;
objd.baseC::a = 40;
cout << “ value of a in the derived class = ” << objd.a << endl;
cout << “ value of a in baseA = ” << objd.baseA::a << endl;
cout << “ value of a in baseB = ” << objd.baseB::a << endl;
cout << “ value of a in baseC = ” << objd.baseC::a << endl;
cout << endl;
return 0;
}
Output of the above program
value of a in the derived class = 10
value of a in baseA = 20
value of a in baseB = 30
value of a in baseC = 40
CONTAINER CLASSES
When a class is declared and defined as a member of another class, it is known as a nested class. In this section, how a container class can be declared and defined in a program is explained. C++ allows to declare an object of a class as a member of another class. When an object of a class is declared as a member of another class, it is called as a container class. Some of the examples for container classes are arrays, linked lists, stacks and queues. The general syntax for the declaration of container class is,
class user_de ned_name_1 {
-------
-------
};
class user_de ned_name_2 {
-------
-------
};
class user_de ned_name_n {
-------
-------
};
class derived_class :public/protected/private
{
user_de ned_name_1 obj1; //object of the class one
user_de ned_name_2 obj2; //object of the class two
-------
-------
user_de ned_name_n objn; //object of the class n
};
For example, the following program segment illustrates how to declare the container class.
class basic_info {
private :
char name[20];
public :
void getdata();
}; // end of class de nition
class academic_ t {
private :
int rank;
public :
void getdata();
}; // end of class de nition
class nancial_assit
{
private :
basic_info bdata; //object of class basic_info
academic_ t acd; // object of class academic_ t
oat amount ;
public :
void getdata();
void display();
}; // end of class de nition
int main()
{
nancial_assit objf;
-------
-------
return 0;
}
Example 9.
A program to demonstrate how a container class is declared and defi ned in a program. The program consists of two base classes and one derived class. The base class basic_info contains the data members: name, roll number and sex. Another base class academic_fi t contains the data members: course, semester and rank. The derived class fi nancial_assit contains the data member amount, besides the data members of the base classes. The objects of these two base classes are defi ned as members of the derived class. The member functions are used to get information on the derived class from the keyboard and display the contents of the class objects on the screen.
//demonstration of container class
#include <iostream>
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 academic_ t {
private :
char course[20];
char semester[10];
int rank;
public :
void getdata();
void display();
}; // end of class de nition
class nancial_assit
{
private :
basic_info bdata; //object of class basic_info
academic_ t acd; // object of class academic_ t
oat amount ;
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 academic_ t :: getdata()
{
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 :: display()
{
cout << course << “ ” ;
cout << semester << “ ”;
cout << rank << “ ”;
}
void nancial_assit :: getdata()
{
bdata.getdata();
acd.getdata();
cout << “ amount in rupees ?\n”;
cin >> amount;
}
void nancial_assit :: display()
{
bdata.display();
acd.display();
cout << amount << “ ”;
}
int main()
{
nancial_assit f;
cout << “enter the following information \n”;
f.getdata();
cout << endl;
cout << “ Academic Performance for Financial Assistance \n”;
cout << “ -------------------------------------------------------- \n”;
cout << “ Name Rollno Sex Course Semester Rank Amount \n”;
cout << “ -------------------------------------------------------- \n”;
f.display();
cout << endl;
cout << “ -------------------------------------------------------- \n”;
return 0;
}
Output of the above program
enter the following information
enter a name?
Suseekaran
roll no?
20071
sex?
M
course name ( BTech/MCA/DCA etc)?
B.Tech(CSE)
semester ( rst/second etc)?
First
rank of the student
1
amount in rupees?
30000
Academic Performance for Financial Assistance
--------------------------------------------------------------------
Name Rollno Sex Course Semester Rank Amount
--------------------------------------------------------------------
Suseekaran 20071 M B.Tech(CSE) First 1 30000
--------------------------------------------------------------------
MEMBER ACCESS CONTROL
It is well known that the C++ class is more than an enhanced version of the C structure. The class sets up an environment in which the software designer not only can create objects that contain their own methods in the form of member functions, but also can exercise almost total control over the access of class implementations. In this section, access control and its mechanism to access the individual members of a class as well as the derived class are explained. It has already been stated that the access mechanism of the individual members of a class is based on the use of the keywords public, private and protected.
Accessing the Public Data
The public members of a class can be accessed by the following:
- Member functions of the class
- Nonmember functions of the class
- Member function of a friend class
- Member function of a derived class if it has been derived publicly.
Example 10.
A program to illustrate how a data member of a class is accessed by the member function.
#include <iostream>
using namespace std;
class sample {
public:
int a;
void setdata();
void display();
};
void sample :: setdata()
{
a = 10;
}
void sample :: display()
{
cout << “ ++a = ” << ++a << endl;
}
int main()
{
sample obj;
obj.setdata();
obj.display();
return 0;
}
Output of the above program
++a = 11
Example 11.
A program to show how a public data member is accessed by a nonmember function of the class.
#include <iostream>
using namespace std;
class sample {
public:
int a;
void setdata();
};
void sample :: setdata()
{
a = 10;
}
int main()
{
sample obj;
obj.setdata();
cout << “++a = ” << ++(obj.a) << endl;
return 0;
}
Output of the above program
++a = 11
Example 12.
A program to demonstrate how a member function of a derived class can access the public data of a base class in which a derived class has been derived publicly.
//accessing public data by derived class
#include <iostream>
using namespace std;
class base {
public :
int value;
inline void getdata()
{
cout << “ enter a number ” << endl;
cin >> value;
}
}; // end of class de nition
class derived : public base {
public :
void display()
{
++value;
}
};// end of class de nition
int main()
{
derived obj;
obj.getdata();
obj.display();
cout << “ value in derived class = ” << obj.value;
return 0;
}
Output of the above program
enter a number
10
value in derived class = 11
Example 13.
A program to demonstrate how the private data member cannot be accessed by the public member function of the derived class, even though the derived class has been inherited publicly.
//demonstration of private member
#include <iostream>
using namespace std;
class baseA{
private :
int value;
public :
baseA ()
{
cout << “ enter a number : ”;
cin >> value;
}
}; // end of base declaration
class derivedB: public baseA {
public :
void display ()
{
++value;
cout << “ value in derivedB = ” << value << endl;
}
}; // end of derived class de nition
int main()
{
derivedB objB;
objB.display();
return 0;
}
Compile time error
void baseA :: value is a private category and inaccessible.
Example 14.
A program to demonstrate how the protected data member of a base class is accessed by the public member function of the derived class if it has been derived privately.
#include <iostream>
using namespace std;
class baseA{
protected:
int value;
public:
baseA ()
{
cout << “ enter a number : ”;
cin >> value;
}
}; // end of base declaration
class derivedB: private baseA {
public:
void display ()
{
++value;
cout << “ content of value = ” << value << endl;
}
}; // end of derived class de nition
int main()
{
derivedB obj1;
obj1.display();
return 0;
}
Output of the above program
enter a number : 100
content of value = 101
Example 15.
A program to demonstrate how the protected data member of a base class is accessed by the public member function of the derived class irrespective of whether the derived class has a direct base or indirect base and has been derived publicly.
//demonstration of accessing a protected data
#include <iostream>
using namespace std;
class baseA{
protected :
int value;
public :
baseA ()
{
cout << “ enter a number : ”;
cin >> value;
}
}; // end of base declaration
class derivedB: public baseA {
public :
void display ()
{
++value;
cout << “ value in derivedB = ” << value << endl;
}
}; // end of derived class de nition
class derivedC: public derivedB {
public :
void display ()
{
++value;
cout << “ value in derivedC = ” << value << endl;
}
}; // end of derived class de nition
class derivedD: public derivedC {
public :
void display ()
{
++value;
cout << “ value in derivedD = ” << value << endl;
}
}; // end of derived class de nition
int main()
{
derivedD objD;
objD.display();
return 0;
}
Output of the above program
enter a number: 200
value in derivedD = 201

0 comments
Post a Comment