Beruflich Dokumente
Kultur Dokumente
An object of one class is an object of another class, as well. For example, in geometry, a rectangle is a quadrilateral.
Thus, in C++ class rectangle can be said to inherit from class Quadrilateral. In this context, class Quadrilateral is a
base class, and class Rectangle is a derived class. A rectangle is a specific type of quadrilateral, but it is incorrect to
claim that a quadrilateral is a rectangle the quadrilateral could be a parallelogram or some other shape.
Every derived class object is an object of its base class, and one base class can have many derived classes, the set
of objects represented by a base class typically is larger than the set of objects represented by any of its derived
classes. For example, the base class vehicle represent all vehicles, including cars, trucks, boats, airplanes, bicycles
and so on. By contrast, derived class Car represents a smaller, more specific subset of all vehicles.
Protected Members
Using protected access offers an intermediate level of protection between public and private access. A base classs
protected members can be accessed with the body of that base class, by members and friends of that base class, and
by members and friends of any classes derived from that base class.
Derived class member functions can refer to public and protected members of the base class simply by using the
member names. When a derived class member function redefines a base class member function, the base class
member can be accessed from the derived class by preceding the base class member name with the base-class name
and the scope resolution operator(::).
When we declared base class data members as protected, so derived classes can modify the data directly. Inheriting
protected data members slightly increases performance, because we can directly access the members without
incurring the overhead of calls to get and display member functions.
Using protected data members creates two serious problems. First, the derived class object does not have to use a member
function to get the value of the base classs protected data member.
Therefore, a derived-class object easily can assign an invalid value to the protected data member, thus leaving the object in
an inconsistent state. The second problem with using protected data members is that derived class member functions are
more likely to be written so that they depend on the base class implementation. In practice, derived classes should
depend only on the base class services and not on the base class implementation. With protected data members in the
base class, if the base class implementation changes, we may need to modify all derived classes of that base class.
Each public member in the base class is public in the derived class.
Each protected member in the base class is protected in the derived class.
Each private member in the base class remains private in the base class and hence it is visible only in the base
class (not inherited).
www.edutechlearners.com
Page 1
Private Inheritance
In a private derivation
Each public member in the base class is private in the derived class.
Each protected member in the base class is private in the derived class.
Each private member in the base class remains private in the base class and hence it is visible only in the base
class (not inherited).
Protected Inheritance
In a protected inheritance
Each public member in the base class is protected in the derived class.
Each protected member in the base class is protected in the derived class.
Each private member in the base class remains private in the base class and hence it is visible only in the base
class (not inherited).
Multiple Inheritance
Multiple inheritance is the process of creating a new class from more than one base classes. Multiple inheritance is a
derived class declared to inherit properties of two or more base classes. Multiple inheritance can combine the behavior of
multiple base classes in a single derived class. Multiple inheritance has many advantages over 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. ( in short, in multiple inheritance, a derived class has multiple base classes.).
www.edutechlearners.com
Page 2
Suppose A is the base class for the class B. Any pointer to A type (base class) can be assigned the address of an
object of the class B (Derived class). In addition to the address of an object of its own class, i.e., pointers of objects
of base class are type compatible with pointers to objects of derived class.
A a, *ap;
ap = &a;
The pointer ap is made to point to a, an object A type. This is quite obvious. All the public members of the class can
be accessed through ap with the help -> operator. If display() is a member function of the class, ap -> display(); is a
valid statement.
B b;
The C++ language permits the pointer ap to be able to point to b also using the assignment ap = &b;. Even though
this is possible the public members of the class B can not be accessed through ap. Suppose the class B also has the
member function by name display(), even the ap -> display(); refers to the function in the base class A itself.
Selection of the public members through the pointer ap depends on the type of the pointer rather than the content of
the pointer.
Program: Casting of Base Class Pointers to Derived Class Pointer (without virtual functions)
#include<iostream.h>
class alpha
{
protected: int a;
public:
alpha ( int a = 0 )
{
a = i;
}
void display ()
{
cout << a = << a << \n;
}
};
www.edutechlearners.com
Page 3
www.edutechlearners.com
Page 4
Program: Casting of Base Class Pointers to Derived Class Pointer (with virtual functions)
#include<iostream.h>
class alpha
{
protected: int a;
public:
alpha ( int a = 0 )
{
a = i;
}
virtual void display ( )
{
cout << a = << a << \n;
}
};
class beta : public alpha
{
private : int b;
public:
beta (int j = 0)
{
b = j;
}
};
void display( )
{
cout << b = << b << \n;
}
};
int main( )
{
alpha a1(10), *ap;
beta b1(20);
ap = &a1;
ap -> display();
ap = &b1;
ap -> display();
return 0;
}
input output:
a = 10;
b = 20;
www.edutechlearners.com
Page 5
Explanation:In Program, alpha is the base class and beta is its derived class. The member function display() in alpha is made
virtual and the same named function is defined even within the derived class beta. The pointer to base type (alpha)
ap is made to point to a1. An object of alpha type, ap -> display(); is then invoked to display the member data of a1.
The display() is from the base class alpha. The same pointer is assigned the address of the object b1. The display() is
from the derived class beta. This has been made possible because the same named function in the base class alpha
has been made virtual.
As can be observed from Program, the member function selected for execution depends on the content of the pointer
rather than the type of the pointer. The function calls are resolved during the runtime of the program and hence the
phenomenon is called runtime polymorphism or late binding or dynamic binding.
Private Inheritance
When class B is derived from the class A publicly (i.e., with the line class B : public A in the definition of B), we
know that the protected and the public members of the class A are accessible in the class B and also an object of
derived class B can access all the public members of the base class A outside the class.
Consider the following class definition:
class B : private A
{
};
Here the class B is said to be privately derived from the class A. All the protected and public members of the base class A
are accessible in the derived class B. But an object of the class B can not access even the public members of the base class
A also. The protected and public members of the base class will become the private members of the derived class. The
functionality of the base class A is hidden in the derived class B. But there is a way out for making only some public
member functions of the base class accessible to objects of derived class, i.e., by using the base class name followed by the
:: followed by the function name in the public section of the derived class.
www.edutechlearners.com
Page 6
www.edutechlearners.com
Page 7
www.edutechlearners.com
Page 8
j = 20; k = 30;
}
void display( )
{
cout << within display() of beta \n; cout << j = << j;
cout << k = << k;
}
};
class gamma : public beta
{
public:
void show ( )
{
cout << within show of gamma \n;
cout<< j = << j;
cout << k = << k;
}
}
int main( )
{
beta b;
b.display();
gamma g;
g.show();
cout<< j = << j ; //error
cout << k = << k;
}
The class alpha has a protected member j and public member k of int type. The class beta is publicly derived from
the class alpha. As a result, both j and k of alpha class remains same members of beta class. Within the beta class,
the member function set() assigns values to both j and k and the member function display() displays the values of
both j and k.
www.edutechlearners.com
Page 9
The class gamma is derived from beta. Within gamma the members j and k can still be accessed. The member function
show() of gamma display both the values. But direct access through object of class gamma is possible only for k not for j;
private
Protected
Not
inheritable
Private
Not inheritable
protected
Not
inheritable
protected
Access
Specifier
Class
Accessible
From own
Class Outside
Accessible
Public
YES
Accessible
From
Derived
Class
YES
Private
YES
NO
NO
Protected
YES
YES
NO
Private
Protected
From others
YES
www.edutechlearners.com
Page 10
www.edutechlearners.com
Page 11
The class beta is derived from the class alpha and it is also has a constructor and destructor.
The statement cout<< constructor in beta class \n; in the body the constructor is to display the string
constructor in beta class \n on the screen while the constructor of the beta class gets executed. The statement cout
<<destructor in beta class
\n; in the body the destructor is to display the string destructor in beta class \n on the screen while the destructor
of the beta class gets executed.
In the main(), b is declared to be an object of beta type. During the process of its creation, the constructor in the base
class (alpha) is executed and then the constructor of the derived class (beta) gets executed. This is evident from the
order of display of the strings constructor in alpha class and constructor in beta class in the output of the
program. When the object goes out of scope, the destructor in the derived class beta gets executed and it is followed
by the execution of the destructor in the base class alpha. This is evident from the order of display of the strings
destructor in alpha class and destructor in beta class.
Overloaded Constructors in Single Level Inheritance
In the previous program, both base class and the derived class had only default constructor. We may have overloaded
constructors in both the classes. If the base class contains only default constructor and derived class contains overloaded
constructors, during the creation of all objects of the derived class with of without arguments (initial values), the default
constructor in the base class gets executed and then the corresponding constructor in the derived class get executed. What
if the constructors in the base class are overloaded? How can we select the required constructor of the base class for
execution? Program enlightens on this.
www.edutechlearners.com
Page 12
beta (int m)
{
j = m;
}
beta( int m, int n) : alpha(m)
{
j = m;
}
void display()
{
cout << i = << i ; cout << j = << j;
}
};
int main()
{
clrscr()
beta b1, b2(30), b3(40,50);
cout<< members data of b1; b1.display();
cout << members data of b2; b2.display();
cout << members data of b3; b3.display();
return 0;
}
input output:
members data of b1 10 20
members data of b2 10 30
members data of b3 40 50
Explanation
In program, the class alpha is defined with a protected member data I, a constructor without arguments and a constructor
with one argument. The class beta is derived from the class alpha and it has its own private member data j of int type, a
constructor without any arguments, a constructor with one argument and another constructor with two arguments.
In the main(), b1 is declared to be an object of beta type without any initial value. During its creation, the default
constructor in the base class (alpha) gets executed and then the default constructor in the derived class (beta) gets
executed. As a result of which, the member data I of alpha gets the value 10 and the member data j of beta gets the
value 20. B2 is declared to be an object of beta type with one initial value 30. During its creation, the default
constructor in the base class (alpha) gets executed and then the constructor with one argument in the derived class
(beta) gets executed. As a result of which, the member data I of alpha gets the value 10 and the member data j of
beta gets the value 30. B3 is declared to be an object of beta type two arguments 40 and 50. Note the definition of the
constructor with two arguments. In the header of the constructor, the constructor with one argument of alpha class is
invoked with the first formal parameter of the beta class constructor as the actual argument for the alpha class
constructor. During its creation, the constructor with one argument in the base class (alpha) gets executed and then
www.edutechlearners.com
Page 13
the constructor in the derived class (beta) gets executed. As a result of which, the member data I of alpha gets the
value 40 and the member data j of beta gets the value 50. The member data of all the three objects are then displayed.
Constructor,
Program:
Destructors
in
Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class alpha
{
public:
alpha ( )
{
cout << constructor in alpha class \n;
}
~ ~ alpha( )
{
cout<< destructor in alpha class;
}
};
class beta
{
public:
beta()
{
cout << constructor in beta class \n;
}
~beta()
{
cout << destructor in beta class \n;
}
};
class gamma : public alpha, public beta
{
public:
{
gamma( )
{
cout << constructor in gamma class \n;
}
~gamma( )
{
cout<< destructor in gamma class \n;
}
};
www.edutechlearners.com
Page 14
int main( )
{
clrscr( );
gamma g;
return 0;
}
input output:
constructor in alpha class
constructor in beta class
constructor in gamma class
destructor in gamma class
destructor in beta class
destructor in alpha class
Explanation
In Program, the class alpha is defined with default constructor and destructor. The class beta is also defined with
default constructor and destructor. The class gamma, which is derived from both alpha and beta, also has its default
constructor and destructor. In the body of the default constructor and destructor of each of the classes, an output
statement is made to display a string notifying the class name.
In the main(), the object g of gamma class is declared. During the course of its creation, as can be seen from the output of
the program, the constructor in alpha class (first base class for gamma) gets executed first; then the constructor in beta
class (second base class for gamma) gets executed and then the constructor in gamma class gets executed, During the
course of the destruction of the object g, the destructor in gamma class gets executed, followed by the execution of the
destructor in beta class, which is then followed by the execution of the destructor in alpha class. Note that the order of
execution of destructor in the inheritance hierarchy is opposite to the order of the execution of the constructors.
Note: If the line of code class gamma : public alpha, public beta is replaced with the line of code class class gamma
: public alpha, virtual public beta, the order of execution of alpha and beta class constructors and destructors is
reversed. However, the gamma class constructor and destructor get executed as in the earlier case.
Implicit Derived Class Object to Base Class Object Conversion (Object Slicing)
If A is a class and B is another class derived from the class A, we can assign an object of B type and an object of A
type (however, the reverse assignment is not permissible) and this assignment lead to a phenomenon called object
slicing.
#include<iostream.h>
#include<conio.h>
class alpha
{
protected: int i;
public:
alpha()
{
i = 10;
}
www.edutechlearners.com
Page 15
void display()
{
cout << i = << i << \n;
}
~alpha()
{
}
};
class beta : public alpha
{
private: int j;
public:
beta () : alpha()
{
j = 20;
}
void display()
{
alpha :: display();
cout << j = << j << \n;
}
~beta()
{
}
};
int main( )
{
clrscr();
alpha a;
beta b;
cout << size of a << sizeof(a) << \n; a.display();
cout << size of b << sizeof(b) << \n; b.display();
a = b ; //object slicing
cout << size of a << sizeof(a) << \n;
a.display();
getch();
return 0;
}
input output:
size of a 2
i =10
size of b 4
10 20
size of a 2 i = 10
www.edutechlearners.com
Page 16
Explanation
In Program, the class alpha is defined with an integer member data I, a constructor to initialize the member data of the
objects of the class, a member function display() to display the member data values of the objects of the class and
destructor. The class beta is derived from the class alpha and it contains its own member data j of int type, a constructor
and display() to display the member data of the objects of the class. Note that there is a call to display() of alpha class.
In the main(), a and b are declared to be objects or type alpha and beta respectively, The size and values of both the
objects are displayed. Note that the size of a is two and that of b is four. This is because the object a of alpha class has one
member data of int type and the object b of beta class has two member data of int type (one of itself and the other of its
base class alpha). The statement a = b ; assigns the object b of beta type to the object a of alpha type. The object b has
two integer values (its member data j and the inherited member data I of alpha class) associated with it as already
mentioned. But the assignment assigns only inherited member data to a, since the object a cannot accommodate both the
members of b. This effect is called object slicing. Here, the object b is said to have been sliced when it is assigned to the
object a of alpha type.
Note: Assignment of a derived class object to a base class object is possible. But the converse is not permitted,
www.edutechlearners.com
Page 17
public:
void get();
void display();
};
void date :: get()
{
cout << enter day, month and year \n;
cin >> d >> m >> y;
t.get();
}
void date :: display()
{
cout << d << / << m << / << y << \n;
t.display();
}
int main( )
{
date dt; dt.get();
dt.display();
return 0;
}
input output:
enter day, month and year 12 01 1863
enter hours, mintues and seconds 10 23 45 12/01/1863 10:23:45
Explanation
In Program, an object of time is made a member of the class date thereby making the code of time class reusable in
the date class. An object of date type comes packages with member data of both time and date classes.
The member data of the date class and those of the object t of time type are accepted through the function:
void date :: get()
{
cout <<enter day, month and year\n; cin >> d >> m >> y;
t.get();
}
The member data of the date class and those of the object t of time type are displayed through the function:
void date :: display()
{
cout << d << /<<m << /<<y<<\n; t.display();
}
www.edutechlearners.com
Page 18
Container Class
As we have seen in above example, 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.
Program: to illustrate container class
#include<iostream.h>
#include<iomanip.h>
class basic_info
{
private:
char name[20];
long int rollno;
char sex;
public:
void get();
void display();
};
void basic_info :: get()
{
cout << enter the name, rollno, sex \n;
cin >> name >> rollno >> sex;
}
void basic_info :: display()
{
cout << name << name;
cout << roll number << rollno;
cout << sex << sex;
}
class academic_info
{
private:
char course[20];
char sem[10];
int rank;
public:
void get();
void display();
};
void academic :: get()
{
cout << enter course, semester and rank;
cin >> course >> sem >> rank;
}
void academic_info :: display()
www.edutechlearners.com
Page 19
{
cout << course << course; cout << semester << sem;
cout << rank << rank;
}
class financial
{
private: basic_info bdata;
academic_info acd; float amount;
public:
void get();
void display();
};
void financial :: get()
{
bdata.get();
acd.get();
cout <<enter amount(rs.);
cin >> amount;
}
void financial :: display()
{
bdata.display();
acd.display();
cout << setprecision (5);
cout << amount << amount;
}
int main( )
{
financial f;
cout << enter the following details;
f.get();
cout << the details is;
f.display();
}
input output:
enter name, rollno and sex madhav 222 m
enter course, semester and rank b.e. 4th 1
enter amount 25000 name= madav rollno= 222 sex= m course= b.e. rank = 1
amount = 25000
Explanation
The program consist of two base classes and one derived class. The base class basic_info contains the data members:
name, rollno, and sex. Another base class academic_info contains the data members: course, semester, and rank. The
www.edutechlearners.com
Page 20
derived class financial contains the data member amount, besides the data members of the base classes. The objects
of these two base classes are defined as the data members of the derived class. The member function are used to get
information on the derived class and display the contents of the class object.
Overriding - Base Class Members in a Derived Class: The phenomenon of ignoring the same named functions in the
base class and invoking the same named functions of the derived class whenever an object of the derived class accesses the
functions is called Function Overriding.
Constructors
Destructor
User-defined new operators
User-defined assignment operators
Friend relationships
www.edutechlearners.com
Page 21