MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

C++ language

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