C++ language

adplus-dvertising
Inheritance
Previous Home Next

In C++ Inheritance is used for reusability of codes. One of the  important feature of OOP is reusability. It's done through creating new classes, and reuse the feature and attribute of existing old one

The mechanism where feature and attribute of an old class is deriving  to a new class is called inheritance. The old class is referred to as the base class and the new one is called the derived class or subclass.

The derived class or subclass can acquire the some or all property of base class. Derived class have its own feature and also the feature of its super or base class.

Defining The Derived Class:

Derived class are those classes who inherit the property of  base class. In any program can have many base class and also have many derived classes .

class derived_class_name : access_modifier base_class_name
{
.......	                //members 0of derived class
........                 //members 0of derived class
........                   //members 0of derived class
};
 class base
{
//member of base class
};
class derive1: private base //private derivation
{
//member of derived1
};
class derive2: public base //public derivation
{
//member of derived2
};
class derive3: base //private derivation by default
{
//member of derived3
};

Private Inheritance:

when derived class is private inherited the base class then public member of the base class is become private member of the derived class.

When a derived class inherit the base class then member function of derived class can only access the public member of base class. when base class is privately inherited then no member of the base class is accessible to the object of derived class.

Public Inheritance:

when derived class is public inherited the base class then public member of the base class is become public member of the derived class. When a derived class inherit the base class then member function of derived class can only access the public member of base class.

when base class is publicly inherited then public member of the base class is accessible to the object of derived class. But in both cases private member of the base class can't inherited therefore private members of a base class will never become the member of its derived class.

 #include<iostream>
class Rectangle
{
private:
float length ;         // private member and data can't inherited
public:
float width ;     // public member and data are inheritable
void dimention()       //  public member and data are inheritable
{
cout <<
 "\n Enter the length of the rectangle : ";
cin >> length ;
cout << 
"\n Enter the width of the rectangle : ";
cin >> width ;
}
float length1()        // The public member and data are inheritable
{ return length ; }
};                       // End of the class definition

class RectangleArea : public Rectangle
{
private:
float area ;
public:
void Rec_area(void)
{
area = length1() * width ;    
//length can't access directly because its a private member of base class
 }

void Display(void)
{
cout << 
"\n Length = " << length1() ; 
//length can't access directly because its a private member of baseclass
cout << 
"\n width = " << width ;
cout << 
"\n Area = " << area ;
}
};                       // End of the derived class definition
void main(void)
{
RectangleArea area1 ;
area1.length1();     
// The public member and data so its access through the object of derived class.
area1.Rec_area( );
area1.Display( );
}
Previous Home Next