Tuesday, September 6, 2011

Multiple inheritance in C++ Programming Language


In multiple inheritance in C++ Programs, a derived class inherits a number of classes which are not linked with one another.

Here is an Inheritance in C++ with example


#include< iostream.h >
#include< stdio.h >
#include< conio.h >
class str
{
private:
char name [40],address [40];
public:
void getData1 ()
{
fflush(stdin);
puts("Enter the name: -");
gets(name);
fflush (stdin);
puts("Enter the address: -");
gets(address);
}
void display1 ()
{
cout<< name<< endl;
cout<< "Address: -"<< address<< endl;
}
};
class num
{
private:
int age;
long tel_no;
public:
void getData2 ()
{
cout<< "Enter the age: -";
cin >>age;
cout<< "Enter the Telephone Number: -";
cin >>tel_no;
}
void display2 ()
{
cout<< "Age: -"<< age<< endl;
cout<< "Telephone Number: -"<< tel_no<< endl;
}
};
class final:public str,public num
{
private:
char gender [10];
public:
void getData3 ()
{
cout<< "Enter the gender(Mr./Miss.): -";
cin >>gender;
}
void display3 ()
{
cout<< gender<< " ";
}
};
void main ()
{
clrscr ();
final ob1,ob2;
cout<< "For the 1st object:-"<< endl;
ob1.getData1 ();
ob1.getData2 ();
ob1.getData3 ();
cout<< "For the 2nd object:-"<< endl;
ob2.getData1 ();
ob2.getData2 ();
ob2.getData3 ();
clrscr ();
cout<< "1st object values are: -"<< endl;
ob1.display3 ();
ob1.display1 ();
ob1.display2 ();
cout<< endl<< "Press any key to continue.........";
getch ();
clrscr ();
cout<< endl<< "2nd object values are: -"<< endl;
ob2.display3 ();
ob2.display1 ();
ob2.display2 ();
getch ();
}

In the above C++ program on inheritance, the class 'str' contains two private data members - char name [40],address [40]; and two public function members void getData1 () and void display1 ().

There is another class in the above program 'num'. This class also contains two private data members int age;long tel_no; and two public function members - void getData2 () and void display2 ().

The third class is 'final' which inherits both the above classes. This is an example of multiple inheritance where a derived class can have a number of different classes.

As the data members are declared private in str and num, they can not be accessed in class final. So these members are accessed through the functions of the class itself. To access the data members from the derived class the following modification is needed.

After modification Inheritance in C++ with example


#include< iostream.h >
#include< stdio.h >
#include< conio.h >
class str
{
protected:
char name [40],address [40];
public:
};
class num
{
protected:
int age;
long tel_no;
};
class final:public str,public num
{
private:
char gender [10];
public:
void getData ()
{
fflush(stdin);
cout<< "Enter the gender(Mr./Miss.): -";
cin >>gender;
puts("Enter the name: -");
gets(name);
fflush (stdin);
puts("Enter the address: -");
gets(address);
cout<< "Enter the age: -";
cin >>age;
cout<< "Enter the Telephone Number: -";
cin >>tel_no;
}
void display ()
{
cout<< gender<< " ";
cout<< name<< endl;
cout<< "Address: -"<< address<< endl;
cout<< "Age: -"<< age<< endl;
cout<< "Telephone Number: -"<< tel_no<< endl;
}
};
void main ()
{
clrscr ();
final ob1,ob2;
cout<< "For the 1st object:-"<< endl;
ob1.getData ();
cout<< "For the 2nd object:-"<< endl;
ob2.getData ();
clrscr ();
cout<< "1st object values are: -"<< endl;
ob1.display ();
cout<< endl<< "Press any key to continue.........";
getch ();
clrscr ();
cout<< endl<< "2nd object values are: -"<< endl;
ob2.display ();
getch ();
}

Again modifications are done on the above C++ program. The data members are declared private in the base classes and all the three classes have their own void getData () and void display () functions. The derived class final inherits the two classes by using the statement class final:private str,private num . So the members of  str and num are not accessible outside the respective class. So, to enter values we will call the getData () function of the derived class final and from within the function the functions of the base classes will be called.

#include< iostream.h >
#include< stdio.h >
#include< conio.h >
class str
{
private:
char name [40],address [40];
public:
void getData ()
{
fflush(stdin);
puts("Enter the name: -");
gets(name);
fflush (stdin);
puts("Enter the address: -");
gets(address);
}
void display ()
{
cout<< name<< endl;
cout<< "Address: -"<< address<< endl;
}
};
class num
{
private:
int age;
long tel_no;
public:
void getData ()
{
cout<< "Enter the age: -";
cin >>age;
cout<< "Enter the Telephone Number: -";
cin >>tel_no;
}
void display ()
{
cout<< "Age: -"<< age<< endl;
cout<< "Telephone Number: -"<< tel_no<< endl;
}
};
class final:private str,private num
{
private:
char gender [10];
public:
void getData ()
{
cout<< "Enter the gender(Mr./Miss.): -";
cin >>gender;
str::getData ();
num::getData ();
}
void display ()
{
cout<< gender<< " ";
str::display ();
num::display ();
}
};
void main ()
{
clrscr ();
final ob1,ob2;
cout<< "For the 1st object:-"<< endl;
ob1.getData ();
cout<< "For the 2nd object:-"<< endl;
ob2.getData ();
clrscr ();
cout<< "1st object values are: -"<< endl;
ob1.display ();
cout<< endl<< "Press any key to continue.........";
getch ();
clrscr ();
cout<< endl<< "2nd object values are: -"<< endl;
ob2.display ();
getch ();
}

#include< iostream.h >
#include< conio.h >
class A
{
public:
void show()
{
cout<< "\nInside class A.";
}
};
class B
{
public:
void show()
{
cout<< "\nInside class B.";
}
};
class C:public A,public B
{
};
void main()
{
C ob;
ob.show()//error generated in this statement
ob.A::show();
ob.B::show();
getch();
}

In the above C++ program both the classes A and B have show () function. Object of derived class  C can access both but not directly.

When we inherit a base class privately , the derived class has all the data and functions of the base class, but the functionality is hidden, means the members of the base class can be accessed in the derived class but not outside it. When we inherits privately all the public and protected members of the base class becomes private members of the derived class. To demonstrate this feature the same program is modified again.

Another program on Inheritance in C++ with example


#include< iostream.h >
#include< stdio.h >
#include< conio.h >
class str
{
private:
char name [40],address [40];
public:
void getData ()
{
fflush(stdin);
puts("Enter the name: -");
gets(name);
fflush (stdin);
puts("Enter the address: -");
gets(address);
}
void display ()
{
cout<< name<< endl;
cout<< "Address: -"<< address<< endl;
}
};
class num
{
protected:
/* modification is done here. Data members are declared as protected*/
int age;
long tel_no;
public:
void getData ()
{
cout<< "Enter the Telephone Number: -";
cin >>tel_no;
}
void display ()
{
cout<< "Age: -"<< age<< endl;
cout<< "Telephone Number: -"<< tel_no<< endl;
}
};
class final:private str,private num
{
private:
char gender [10];
public:
void getData ()
{
cout<< "Enter the gender(Mr./Miss.): -";
cin >>gender;
cout<< "Enter the age: -";
cin >>num::age;
/* now the protected member of the base class num is accessed hare. As num is derived privately , the derived class can not access it’s protected member directly*/
str::getData ();
num::getData ();
}
void display ()
{
cout<< gender<< " ";
str::display ();
num::display ();
}
};
void main ()
{
clrscr ();
final ob1,ob2;
cout<< "For the 1st object:-"<< endl;
ob1.getData ();
cout<< "For the 2nd object:-"<< endl;
ob2.getData ();
clrscr ();
cout<< "1st object values are: -"<< endl;
ob1.display ();
cout<< endl<< "Press any key to continue.........";
getch ();
clrscr ();
cout<< endl<< "2nd object values are: -"<< endl;
ob2.display ();
getch ();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner