In the case of multiple inheritance ,we have seen that ambiguity occurs when we use same name for different functions in two or more base classes.
Constructors are used to initialized data members in an object and also for allocating memory for the objects. In the case of inheritance, the objects of the derived class are usually used instead of the base class objects. Hence objects of the derived class are initialized using constructors and if there is a need , the base class objects are also initialized using derived class constructors
when a base class and a derived class both have constructors ans destructor functions, the constructor functions are executed in the order of derivation. That is , the base class constructor is executed before the constructor in the derived class.
The destructor functions are executed in the reverse order. That is, the derived class destructor is executed before the base class destructor. Further arguments can be passed from the derived class constructor to the base class constructor.the syntax for passing arguments from derived class constructor is:
derived(arg-list):base(arg-list)
{
//body of the derived class constructor
}
where, derived is the name of the derived class, base is the name of the base class, and the argument lists in the two constructors can be same or different. when they are different, we must pass to the derived class constructor all arguments need by both the derived class and the base class. Then the derived class simply passes to the base class all those arguments required by it.
In the case of multiple inheritance, the constructors are executed i tneh order based classes are specified. Destructors are expected in the reverse order. The general syntax for derived class constructor in the case of multiple inheritance is:
derived (arg-list): base 21(arg-list),base2(arg-list),...
{
// body of the derived class constructor
}
where, derived is the name of the derived class and base1,base2,.... are the names of the base classes. the argument list in the derived class constructor consists of all arguments needed by the derived class and all the base classes.
// This program illustrates order of execution of constructors and destructors in the base class and the //derived class. Both the constructors use the same arguments.
#include<iostream>
using namespace std;
class base
{
int i;
public:
base(int n)
{
cout<<"Constructing base class\n";
i=n;
}
~base()
{
cout<<"Destructing base clas\n";
}
void show()
{
cout<<"i="<<i<<"\n";
}
};
class derived:public base
{
int j;
public:
derived(int n):base (n)
{
cout<<"Constructing derived class\n";
j=n;
}
~derived()
{
cout<<"Destructing derived class \n";
}
void showj()
{
cout<<"j="<<j<<"\n";
}
};
void main()
{
derived ob(10);
ob.showj();
ob.showj();
}
Result:
constructing base class
constructing derived class
i=10
j=10
destructing derived class
destructing base class
C++ provides a no. of ways to
establish access to class members. One such access control mechanism is the
derived class are declared. A derived class can be declared with one of the access
control specifiers private, public or protected and thus there are 3 types of
derivations. The access specifier determines how the derived class inherits the
elements of the base class.
Public
Derivation:
When the access specifier for the
inherited base class is public, all public member of the base class become
public member of the derived class. All protected members of the base class become
protected members of the derived class. All private members of the base class
remain private to it and are inaccessible by the derived class.
Eg:
class base A
{
private:
public:
int
a; //member data
};
class derivedB:
public basea
{
private:
public:
};
void main()
{
derivedB
test;
cin>>test.A;
cout<<test.A;
}
Private Derivation:
If the access specifier is
private, all public members of the base class becomes private member of the
derived class and they are accessible by the member function of the derived
class. All protected members of the base class also becomes private members of
the derived class. All private members of the base class are remain to it and
are inaccessible by the derived class.
Eg:
class baseA
{
private:
public:
int
a;
};
class derived:
private baseA
{
private:
public:
void
read()
{
cin>>a;
}
void
display()
{
cout<<a;
}
};
void main()
{
derived sample;
sample.read();
sample.disp();
}
Protected Derivation:
A base class can also be
inherited with the access specifier protected by a derived class then the
public and protected members of the base class become protected member of the
derived class. All private members of the base class remains private to it and
are inaccessible by the derived class.
Eg:
class baseA
{
private:
protected:
int a;
public:
};
class derived:
public baseA
{
private:
public:
void read()
{
cin>>a;
}
void disp()
{
cout<<a;
}
};
void main()
{
derivedB sample;
sample.read();
sample.disp();
}
BASE CLASS VISIBILITY
|
DERIVED CLASS VISIBILITY
|
||
PUBLIC DERIVATION
|
PRIVATE DERIVATION
|
PROTECTED DERIVATION
|
|
PUBLIC
|
Public
|
Private
|
Protected
|
PRIVATE
|
Not inherited
|
Not inherited
|
Not inherited
|
PROTECTED
|
Protected
|
Private
|
Protected
|
Ambiguity means an unclear situation. Two base
classes can have functions with same name while the class derived from both the
base classes has no function with same name. How do objects of the derived
class access the correct base class function?
Now
the program has derived class medical with two base classes, namely student and
academic. We see both the base classes have functions with same name, which
certainly lead to a problem when a class derives features from both. The
derived class will have two different functions with the same name. The
ambiguity occurs only when we use the name of the function. To call a function,
the name of the function alone is insufficient, since the compiler is meant. To
overcome this problem the functions are invoked long with name of class and
scope resolution operator as follows:
student::output()
academic::output()
The
compiler distinguishes the above two member functions by identifying them with
the class to which they belong. So scope resolution operator plays an important
role to point the objects and map the respective functions.
Other Recommended posts
Inheritance is classified according to the levels of inheritance as
Simple or single, multiple and hierarchical inheritance are referred to as single inheritance. Multilevel and hybrid inheritance are generally considered to be multilevel inheritance. Multilevel inheritance can be further classified as inheritance with multiple paths or no multiple paths.
- Single Level Inheritance
- Multilevel Inheritance
Simple or single, multiple and hierarchical inheritance are referred to as single inheritance. Multilevel and hybrid inheritance are generally considered to be multilevel inheritance. Multilevel inheritance can be further classified as inheritance with multiple paths or no multiple paths.
Single Level Inheritance |
Other Recommended posts
This is a single inheritance connected in parallel, having two or more base classes and a common derived class. It has the tree structure. Here a class can inherit the properties of two or more classes. The general format for declaring and defining a derived class from multiple base classes is:
class derived : visibility base1, visibility base2,visibility base3,.....{ } ;
where, derived is the derived class name and base1,base2,base3,... are base class names. Visibility is the access specifier used for the derivation.Generally it is public or private. No an object of C ,derived from the base classes A and B will have the feature of both A and B.
OUTPUT:
Enter Student Details:
Name:Rahul
Roll:303
Age:18
Rank:2
Blood Group:O+
Details of a Student:
Name:Rahul
Roll:303
Age:18
Rank:2
Blood Group:O+
class derived : visibility base1, visibility base2,visibility base3,.....{ } ;
where, derived is the derived class name and base1,base2,base3,... are base class names. Visibility is the access specifier used for the derivation.Generally it is public or private. No an object of C ,derived from the base classes A and B will have the feature of both A and B.
Illustrate the multiple inheritance with public derivation
#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void input()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void output()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class academic
{
private:
int rank;
public:
void input()
{
cout<<"Rank:";
cin>>rank;
}
void output()
{
cout<<"Rank:"<<rank;
}
};
class medical:public student,public academic
{
private:
char bloodgp[4];
public:
void cinput()
{
student::input(); // input()invoked along with class name
academic::input();
cout<<"Blood Group:";
cin>>bloodgp;
}
void coutput()
{
student::output(); // output()invoked along with class name
academic::output();
cout<<endl<<"Blood Group:"<<bloodgp<<endl;
}
};
void main()
{
medical m;
cout<<"Enter student Details:"<<"\n";
m.cinput();
cout<<"\n Details of a student:"<<"\n";
m.coutput();
}
OUTPUT:
Enter Student Details:
Name:Rahul
Roll:303
Age:18
Rank:2
Blood Group:O+
Details of a Student:
Name:Rahul
Roll:303
Age:18
Rank:2
Blood Group:O+
Other Recommended posts
This is single inheritance connected in parallel keeping the base class common. Here classes B and C have no connection; they only inherit from the common base class A.if an object of class B only is created in the main() function, then there will be no trace of the features of class C and vice versa.
Illustrates Hierarchical inheritance and public derivation
#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class student:public stud
{
private:
int rank;
public:
void inputdata()
{
cout<<"Rank:";
cin>>rank;
}
void outputdata()
{
cout<<"Rank:"<<rank;
}
};
class medical:public students
{
private:
char bloodgp[4];
public:
void cinputdata()
{
inputdata();
cout<<"Blood Group:";
cin>>bloodgp;
}
void coutputdata()
{
outputdata();
cout<<endl<<"Blood Group:"<<bloodgp<<endl;
}
};
void main()
{
academic a;
cot<<"Enter the values of the object of academic:"<<"\n";
a.getstudent();
a.cinputdata();
cout<<"Contents of the object of academic:"<<"\n";
a.showstudent();
a.coutputdata();
//Objects are accessed using same method name
medical b;
cout<<"Enter the values of the object of edical:"<<"\n";
b.getstudent();
b.cinputdata();
cout<<"Contents of the object of medical:"<<"\n";
b.showstudent();
b.coutputdata();
}
Output:
Enter the values of the object of academic:
Name :Sai
Roll:101
Age:28
Rank:4
Content of the object of academic:
Name :Sai
Roll:101
Age:28
Rank:4
Enter the values of the object of academic:
Name :Sai ram
Roll:202
Age:18
Blood Group: O+
Content of the object of academic:
Name :Sai ram
Roll:202
Age:18
Blood Group: O+
A chain of single inheritance is referred to as multilevel inheritance.
In this type of inheritance, class B plays a dual role and act both as a derived class as well as a base class. When the class B is inherited from class A, B is acting as a derived class. When the class C is derived from the class B, B is acting as a base class for the derived class C. The class C holds the characteristics of both the classes,namely class A and class B, which in turn directs the user to reuse the definitions made in those two classes.
if (class B is inherited from A under the public access specifier) then
class C will contain the public member of both class A and B
else
class C will contain only the public member of B
the above statement clearly mentions the position of C, when the inheritance of B from A is public or not . In any type of derivation, the protected members of the class A will be visible to the public members of classes B only and class C can never access them.
Illustrate the program for multiple inheritance with public
#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class student:public stud
{
private:
int rank;
public:
void inputdata()
{
cout<<"Rank:";
cin>>rank;
}
void modifydata()
{
char bool;
cout<<endl<<"Do you want to modify?(y/N):";
cin>>bool;
if(bool=='Y')
{
cout<<"Age:";
cin>>age;
cout<<"Rank:";
cin>>rank;
}
}
void outputdata()
{
cout<<"Rank:"<<rank;
}
};
class medical : public students
{
private:
char bloodgp[4];
public:
void cinputdata()
{
inputdata();
cout<<"Blood Group:";
cin>>bloodgp;
}
void coutputdata()
{
outputdata();
cout<<endl<<"Blood Group:"<<bloodgp<<endl;
}
};
void main()
{
medical m;
m.cinputdata();
m.coutputdata();
m.modifydata();
m.coutputdata();
}
In this type of inheritance, class B plays a dual role and act both as a derived class as well as a base class. When the class B is inherited from class A, B is acting as a derived class. When the class C is derived from the class B, B is acting as a base class for the derived class C. The class C holds the characteristics of both the classes,namely class A and class B, which in turn directs the user to reuse the definitions made in those two classes.
if (class B is inherited from A under the public access specifier) then
class C will contain the public member of both class A and B
else
class C will contain only the public member of B
the above statement clearly mentions the position of C, when the inheritance of B from A is public or not . In any type of derivation, the protected members of the class A will be visible to the public members of classes B only and class C can never access them.
Illustrate the program for multiple inheritance with public
#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class student:public stud
{
private:
int rank;
public:
void inputdata()
{
cout<<"Rank:";
cin>>rank;
}
void modifydata()
{
char bool;
cout<<endl<<"Do you want to modify?(y/N):";
cin>>bool;
if(bool=='Y')
{
cout<<"Age:";
cin>>age;
cout<<"Rank:";
cin>>rank;
}
}
void outputdata()
{
cout<<"Rank:"<<rank;
}
};
class medical : public students
{
private:
char bloodgp[4];
public:
void cinputdata()
{
inputdata();
cout<<"Blood Group:";
cin>>bloodgp;
}
void coutputdata()
{
outputdata();
cout<<endl<<"Blood Group:"<<bloodgp<<endl;
}
};
void main()
{
medical m;
m.cinputdata();
m.coutputdata();
m.modifydata();
m.coutputdata();
}
OUT PUT:
Name:cnu
Roll:10
Age:15
Rank:8
Blood Group:B+
Name:cnu
Roll:10
Age:15
Rank:8
Blood Group:B+
Do you want to modify?(Y/N) Y
Age:19
Rank:3
Name:cnu
Roll:10
Age:19
Rank:3
Subscribe to:
Posts (Atom)