Oop 3
Oop 3
Property1
Property2
Property3
The derived class D also contains the
same three properties as those in the
base class B. Instead of defining these
Derived Class D properties again, they can be reused or
inherited from the base class B. Instead
Property1
of defining these properties again, they
can be reused or inherited from the
Property2
base class B.
Property3
This is the unique property of the
Property4 derived class D.
Cont.
• In above figure, the class B contains the three member i.e.
Property1, Property2 and Property3.
public:
float breadth
Enter_lb( )
Enter_l( )
Display_l( )
Rectangle r
Rec_area( )
Display( )
Not accessible
Rectangle1 r1 Accessible
Private inheritance
• When a derived class privately inherits a base class, all
the public members of the base class become private
for the derived class.
public:
void area_rec()
{
enter();
cout << "\n Area = " << (getLength() * getBreadth());
}
};
void main()
{
clrscr();
RecArea r ;
r.area_rec();
getch();
}
Example:
#include<iostream.h>
class Rectangle // Base class
{
private:
float length ; // This data member can't be inherited
public:
float breadth ; // This data member is inheritable
void Enter_lb(void)
{
cout << "\n Enter the length of the rectangle: "; cin >> length ;
cout << "\n Enter the breadth of the rectangle: "; cin >> bredth ;
}
float Enter_l(void)
{
return length ;
}
void Display_l(void)
{
cout << "\n Length = " << length ;
}
}; // End of the base class Rectangle.
Cont.
// Defining the derived class Rectangle1. This class has been derived from the
// base class i.e. Rectangle, privately.
class Rectangle1 : private Rectangle // All the public members of the base class
{ // Rectangle become private for the derived class Rectangle1.
private:
float area ;
public:
void Rec_area(void)
{
Enter_lb( );
area = Enter_l( ) * bredth ; // length can't be used
directly
}
void Display(void)
{
Display_l( ); // Displays the value of length.
cout << "\n Bredth = " << bredth ;
cout << "\n Area = " << area << endl ;
}
};
Cont.
void main(void)
{
Rectangle1 r1 ;
r1.Rec_area( ) ;
// r.Enter_lb( ); will not work as it has become private for the derived class.
r1.Display( ) ;
// r.Display_l( ) will not work as it also has become private for the derived class.
}
class Rectangle
Fig: private inheritance private:
float length
public:
float bredth
Enter_lb( )
Enter_l( )
Display_l( )
Rectangle r
Rec_area( )
Rectangle1
The protected access specifier
• The third access specifier provided by C++ is protected.
• On other hand, if the protected members are inherited in the private mode,
the members also become private in the derived class.
• They can also be accessed by the member functions of the derived class, but
cannot be inherited further.
Table: Accessibility
protected:
protected:
public: public:
public:
public:
void Enter_data( ) // Overriding of the member function
{
Employee :: Enter_data( ) ;
cout << "\n Enter the designation of the Engineer: " ; cin >> design ;
}
student
In the above Fig., student is a base class, from which the three
classes viz. arts, science and commerce have been derived. Now,
let us write a program that illustrates the hierarchical inheritance,
based on the above design.
Example:
include<iostream.h>
const int len = 20 ;
class student // Base class
{
private: char F_name[len] , L_name[len] ;
int age, int roll_no ;
public:
void Enter_data(void)
{
cout << "\n\t Enter the first name: " ; cin >> F_name ;
cout << "\t Enter the second name: "; cin >> L_name ;
cout << "\t Enter the age: " ; cin >> age ;
cout << "\t Enter the roll_no: " ; cin >> roll_no ;
}
void Display_data(void)
{
cout << "\n\t First Name = " << F_name ;
cout << "\n\t Last Name = " << L_name ;
cout << "\n\t Age = " << age ;
cout << "\n\t Roll Number = " << roll_no ;
}
};
Cont.
class arts : public student
{
private:
char asub1[len] ;
char asub2[len] ;
char asub3[len] ;
public:
void Enter_data(void)
{
student :: Enter_data( );
cout << "\t Enter the subject1 of the arts student: "; cin >> asub1 ;
cout << "\t Enter the subject2 of the arts student: "; cin >> asub2 ;
cout << "\t Enter the subject3 of the arts student: "; cin >> asub3 ;
}
void Display_data(void)
{
student :: Display_data( );
cout << "\n\t Subject1 of the arts student = " << asub1 ;
cout << "\n\t Subject2 of the arts student = " << asub2 ;
cout << "\n\t Subject3 of the arts student = " << asub3 ;
}
};
Cont.
class science : public student
{
private:
char ssub1[len] ;
char ssub2[len] ;
char ssub3[len] ;
public:
void Enter_data(void)
{
student :: Enter_data( );
cout << "\t Enter the subject1 of the science student: "; cin >> ssub1;
cout << "\t Enter the subject2 of the science student: "; cin >> ssub2;
cout << "\t Enter the subject3 of the science student: "; cin >> ssub3;
}
void Display_data(void)
{
student :: Display_data( );
cout << "\n\t Subject1 of the science student = " << ssub1 ;
cout << "\n\t Subject2 of the science student = " << ssub2 ;
cout << "\n\t Subject3 of the science student = " << ssub3 ;
}
};
Cont.
class commerce : public student
{
private: char csub1[len], csub2[len], csub3[len] ;
public:
void Enter_data(void)
{
student :: Enter_data( );
cout << "\t Enter the subject1 of the commerce student: ";
cin >> csub1;
cout << "\t Enter the subject2 of the commerce student: ";
cin >> csub2 ;
cout << "\t Enter the subject3 of the commerce student: ";
cin >> csub3 ;
}
void Display_data(void)
{
student :: Display_data( );
cout << "\n\t Subject1 of the commerce student = " << csub1 ;
cout << "\n\t Subject2 of the commerce student = " << csub2 ;
cout << "\n\t Subject3 of the commerce student = " << csub3 ;
}
};
Cont.
void main(void)
{
arts a ;
cout << "\n Entering details of the arts student\n" ;
a.Enter_data( );
cout << "\n Displaying the details of the arts student\n" ;
a.Display_data( );
science s ;
cout << "\n\n Entering details of the science student\n" ;
s.Enter_data( );
cout << "\n Displaying the details of the science student\n" ;
s.Display_data( );
commerce c ;
cout << "\n\n Entering details of the commerce student\n" ;
c.Enter_data( );
cout << "\n Displaying the details of the commerce student\n";
c.Display_data( );
}
Multiple Inheritance
• When a class is inherited from more than one
base class, it is known as multiple inheritance.
• The syntax for defining a subclass, which is
inheriting more than one classes is:
class Subclass : access_specifier Baseclass1,
access_specifier Baseclass2, ………
…….. access_specifier Baseclass_n
{
members of the derived class ;
};
• The following figure illustrates the use of
multiple inheritance.
Fig: Multiple Inheritance
class Rectangle
class Circle
protected:
protected: float length
float radius
float breadth
public:
public:
void Enter_r ( ) void Enter_lb ( )
void Display_ca ( ) void Display_ar ( )
Base
First level inheritance
Derive1
In the above figure, class B represents the base class. The class D1
that is called first level of inheritance, inherits the class B. The
derived class D1 is further inherited by the class D2, which is
called second level of inheritance.
Example: Multilevel Inheritance
#include<iostream.h>
class Base
{
protected:
int b;
public:
void EnterData( )
{
cout << "\n Enter the value of b: ";
cin >> b;
}
void DisplayData( )
{
cout << "\n b = " << b;
}
};
Cont.
class Derive1 : public Base
{
protected:
int d1;
public:
void EnterData( )
{
Base:: EnterData( );
cout << "\n Enter the value of d1: ";
cin >> d1;
}
void DisplayData( )
{
Base::DisplayData();
cout << "\n d1 = " << d1;
}
};
Cont.
class Derive2 : public Derive1
{
private:
int d2;
public:
void EnterData( )
{
Derive1::EnterData( );
cout << "\n Enter the value of d2: ";
cin >> d2;
}
void DisplayData( )
{
Derive1::DisplayData( );
cout << "\n d2 = " << d2;
}
};
Cont.
int main( )
{
Derive2 objd2;
objd2.EnterData( );
objd2.DisplayData( );
return 0;
}
Constructors revisited
• In object-oriented programming, a constructor in a class is a special block
of statements called when an object is created, either when it is declared.
• A constructor is similar to a class method, but it differs from a method in
that it never has an explicit return type, it's not inherited, and usually has
different rules for modifiers.
• Constructors are often distinguished by having the same name as the
declaring class.
• Their responsibility is to pre-define the object's data members.
• In most languages, the constructor can be overloaded in that there can be
more than one constructor for a class, each having different parameters.
• Some languages take consideration of some special types of constructors:
• default constructor - a constructor which can take no arguments
• copy constructor - a constructor which takes one argument of the type of the
class.
• Some of the differences between constructors and other member functions:
Constructors never have an explicit return type.
Constructors cannot be overridden, nor are they inherited.
Constructors cannot be const.
Constructors cannot be virtual.
Constructors cannot be static.
Constructors in single inheritance:
// This program illustrates the use of constructors in the single inheritance
#include<iostream.h>
class A // Base class
{
private:
int x ;
public:
A( ) // Constructor without any argument
{
x=0;
cout << "\n The constructor of the class A without any argument is invoked***
";
}
A(int X) // Constructor with one argument
{
x=X;
cout << "\n The constructor of the class A with one argument is invoked***" ;
}
void Enter_x(void)
{
cout << "\n\n\t Enter the value of x: "; cin >> x ;
}
void Display_x(void)
{ cout << "\n\t x = " << x ; }
};
Cont.
// ***************Derived Class******************
class B : public A
{
private:
int y ;
public:
B( ) : A ( ) // Constructor of the derived class B without any argument
{
y=0;
cout << "\n The constructor of the class B without any argument is
invoked**" ;
}
// Constructor of the derived class B with two arguments
B(int X, // Argument for constructor A
int Y) // Argument for constructor B
: A(X) // Call for the constructor A
{
y=Y;
cout << "\n The constructor of the class B with two arguments is invoked***" ;
}
void Enter_y(void)
{ cout << "\t Enter the value of y: " ; cin >> y ; }
void Display_y(void)
{ cout << "\n\t y = " << y ; }
};
Cont. void main(void)
{
cout << "\n\n The first object b1 is in use********\n " ;
B b1 ; // Invokes the constructor with zero arguments
b1.Enter_x( );
b1.Enter_y( );
b1.Display_x( );
b1.Display_y( );
cout << "\n\n The second object b2 is in use********\n " ;
B b2(5, 6); // Invokes the constructor with two arguments
b2.Display_x( );
b2.Display_y( );
}
Output: The first object b1 is in use********
The constructor of class A without any argument is invoked*******
The constructor of class B without any argument is invoked*******
Enter the value of x: 10
Enter the value of y: 12
x = 10
y = 12
The second object b2 is in use********
The constructor of class A with one argument is invoked******
The constructor of class B with two arguments is invoked*****
x=5
y=6
Constructors in multilevel inheritance:
# include<iostream.h>
class A
{
protected:
int x ;
public:
A( ) // Constructor without argument
{
x=0;
cout << "\n Constructor of class A without any argument is
invoked" ;
}
A(int X) // Constructor with one argument
{
x=X;
cout << "\n Constructor of class A with one argument is invoked" ;
}
void Enter_x(void)
{ cout << "\n\t Enter the value of x: " ; cin >> x ; }
void Display_x(void)
{ cout << "\n\t x = " << x ; }
};
Cont.
class B : public A
{
protected:
int y ;
public:
B( ) : A( ) // Constructor without argument
{
y = 0;
cout << "\n Constructor of class B without any argument is invoked"
;
}
// Constructor with two arguments
B( int X, // Argument for constructor A
int Y ) // Argument for constructor B
: A(X) // Call for constructor A
{
y = Y;
cout << "\n Constructor of class B with two arguments in invoked" ;
}
void Enter_y(void)
{ cout << "\t Enter the value of y: " ; cin >> y ; }
void Display_y(void)
{ cout << "\n\t y = " << y ; }
};
Cont.
class C : public B
{
private:
int z ;
public:
C( ) : B( ) // Constructor without argument
{
z = 0;
cout << "\n Constructor of class C without any argument is invoked\n" ;
}
// Constructor with three arguments
C(int X, int Y, // Arguments for constructor B
int Z) // Argument for constructor C
: B(X, Y) // Call for constructor B
{
z=Z;
cout << "\n Constructor of class C with three arguments is invoked" ;
}
void Enter_z(void)
{ cout << "\t Enter the value of z: " ; cin >> z ; }
void Display_z(void)
{ cout << "\n\t z = " << z ; }
};
Cont.
void main(void)
{
cout << "\n The first object is in use now******** \n " ;
C c1 ;
c1.Enter_x( );
c1.Enter_y( );
c1.Enter_z( );
c1.Display_x( );
c1.Display_y( );
c1.Display_z( );
cout << "\n\n The second object is in use now******** \n" ;
C c2(5, 6, 7) ;
c2.Display_x( );
c2.Display_y( );
c2.Display_z( );
}
Output:
The first object is in use now*********
x = 11
y = 13
z = 27
The second object is in use now*********
x=5
y=6
z=7
Constructors in multiple Inheritance:
#include<iostream.h>
class A // First Base class
{
private:
int x ;
public:
A( ) // Constructor of the base class A without any argument
{
x=0;
cout << "\n Constructor of class A without any argument is invoked" ;
}
A(int X) // Constructor of the base class A with one argument
{
x=X;
cout << "\n Constructor of class A with one argument is invoked" ;
}
void Enter_x(void)
{ cout << "\n\n\t Enter the value of x: " ; cin >> x ; }
void Display_x(void)
{ cout << "\n\t x = " << x ; }
};
Cont.
class B // Second Base class
{
private:
int y ;
public:
B( )
{
y = 0 ; // Constructor of the base class B without any argument
cout << "\n Constructor of class B without any argument is invoked“ ;
}
B(int Y) // Constructor of the base class B with one argument
{
y=Y;
cout << "\n Consrtuctor of class B with one argument isinvoked"
;
}
void Enter_y(void)
{ cout << "\t Enter the value of y: " ; cin >> y ; }
void Display_y(void)
{ cout << "\n\t y = " << y ; }
};
Cont.
class C : public B, public A //Derived class, inherited from base classes A & B
{
private:
int z ;
public:
C( ) : A( ), B( ) // Constructor of the derived class C without any argument
{
z=0;
cout << "\n Constructor of class C without any argument is invoked" ;
}
// *****Constructor of the derived class C with three arguments*****
C(int X, // Argument for the constructor A
int Y, // Argument for the constructor B
int Z) // Argument for the constructor C
: A(X), B(Y) // Calls for the constructors A and B
{
z=Z;
cout << "\n Consrtuctor of class C with three arguments is invoked\n"
;
}
void Enter_z(void)
{ cout << "\t Enter the value of z: " ; cin >> z ; }
void Display_z(void)
{ cout << "\n\t z = " << z ; }
};
Cont.
void main(void)
{
cout << "\n The first object c1 is in use********\n" ;
C c1 ;
c1.Enter_x( );
c1.Enter_y( );
c1.Enter_z( );
c1.Display_x( );
c1.Display_y( );
c1.Display_z( );
cout << "\n\n The second object c2 is in use********\n" ;
C c2(5, 6, 7) ;
c2.Display_x( );
c2.Display_y( );
c2.Display_z( );
}
Output:
The first object c1 is in use********
x =9
y = 10
z = 12
x =5
y=6
z=7
Virtual base classes:
D1 D2
Virtual path
D3
Cont.
• In above Figure , the classes D1 and D2 are derived from the
base class B (hierarchical inheritance).
• Also a class D3 inherits the base class B (multilevel
inheritance) through the classes D1 and D2 (multiple
inheritance).
• Each of the classes D1 and D2 inherit a copy of base class B
known as suboject.
• Now, come to the class D3. When the derived class D3
inherits the members of the base class B, which of two copies
will it access (copy of sub-object of the class D1 or D2)?
• This introduces an ambiguous situation for the compiler,
hence it gives an error message.
• For example, consider the following program, based on above
design.
#include<iostream.h>
class B // Base class
{
protected:
int base_data ;
public:
void get_b_data(int b)
{
cout << "\n Accessing the data from the base class B------------>" ;
base_data = b ;
}
void display_b_data(void)
{ cout << "\n base_data = " << base_data ; }
};
class D1 : public B // First base class.
{ // This class is derived from the class B.
protected:
int der1_data ;
public:
void get_d1_data(int d1)
{
cout << "\n Accessing the data from the derived class D1---------->" ;
der1_data = d1 ;
}
void display_d1_data(void)
{
cout << "\n der1_data = " << der1_data ;
}
};
Cont.
class D2 : public B // Second base class.
{ // This class is also derived from the class B
protected:
int der2_data ;
public:
void get_d2_data(int d2)
{
cout << "\n Accessing the data from the derived class D2---------->" ;
der2_data = d2 ;
}
void display_d2_data(void)
{
cout << "\n der2_data = " << der2_data ;
}
};
Cont.
class D3 : public D1, public D2 // This class inherits the properties of the
{ // classes B, D1 and D2.
public:
int der3_data ;
public:
void get_d3_data(int d3)
{
cout << "\n Accessing the data from the derived class D3---------->" ;
der3_data = d3 ;
}
void display_d3_data(void)
{ cout << "\n der3_data = " << der3_data ; }
};
void main(void)
{
D3 der3 ; // Object of the class D3.
der3.get_b_data(7); // Accessing the member functions of the base class B.
der3.display_b_data( ) ;
der3.get_d1_data(8) ; // Accessing the member functions of the class D1.
der3.display_d1_data( );
der3.get_d2_data(18) ; // Accessing the member functions of the class D2.
der3.display_d2_data( );
der3.get_d3_data(15); // Accessing the member functions from its own class D3.
der3.display_d3_data( );
}
Output:
Example:
#include<iostream.h>
// This program illustrates the use of the virtual base clas
class B // Base class
{
protected:
int base_data ;
public:
void get_b_data(int b)
{
cout << "\n Accessing the data from the base class B------------>" ;
base_data = b ;
}
void display_b_data(void)
{
cout << "\n base_data = " << base_data ;
}
};
class D1 : virtual public B // First virtual base class.
{ // This class is derived from the class B.
protected:
int der1_data ;
public:
void get_d1_data(int d1)
{
cout << "\n Accessing the data from the derived class D1---------->" ;
der1_data = d1 ;
}
void display_d1_data(void)
{
cout << "\n der1_data = " << der1_data ;
}
};
Cont.
class D2 : virtual public B // Second virtual base class.
{ // This class is also derived from the class B.
protected:
int der2_data ;
public:
void get_d2_data(int d2)
{
cout << "\n Accessing the data from the derived class D2---------->" ;
der2_data = d2 ;
}
void display_d2_data(void)
{
cout << "\n der2_data = " << der2_data ;
}
};
class D3 : public D1, public D2 // This class inherits the properties of the
{ // classes B, D1 and D2.
public:
int der3_data ;
public:
void get_d3_data(int d3)
{
cout << "\n Accessing the data from the derived class D3---------->" ;
der3_data = d3 ;
}
void display_d3_data(void)
{
cout << "\n der3_data = " << der3_data ;
}
};
Cont.
void main(void)
{
D3 der3 ; // Object of the class D3.
der3.get_b_data(7); // Accessing the member functions of the base class B.
der3.display_b_data( ) ;
der3.get_d1_data(8) ; // Accessing the member functions of the class D1.
der3.display_d1_data( );
der3.get_d2_data(18) ; // Accessing the member functions of the class D2.
der3.display_d2_data( );
der3.get_d3_data(15); // Accessing the member functions from its own class D3.
der3.display_d3_data( );
}
Output: