Social Icons

banner image

INHERITANCE..IN C++

INTRODUCTION

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Access Control and Inheritance:

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
We can summarize the different access types according to who can access them in the following way:
Accesspublicprotectedprivate
Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono
A derived class inherits all base class methods with the following exceptions:
  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

Type of Inheritance:

In C++, we have 5 different types of Inheritance. Namely,
  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual InheritancE)
When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:
  • Public Inheritance: When deriving a class from a public base class, publicmembers of the base class become public members of the derived class andprotected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • Protected Inheritance: When deriving from a protected base class, public andprotected members of the base class become protected members of the derived class.
  • Private Inheritance: When deriving from a private base class, public andprotected members of the base class become private members of the derived class.

SINGLE LEVEL INHERITANCES

EX 1:
#include<iostream.h>
#include<conio.h>
class first
{
             int a;
public:

       /*void getdata()
       {
            cout<<"enter value in a";
            cin>>a;
            }*/
            void dispdata()
            {
                 cout<<"base class a="<<a<<endl;
                 }
};
class sec:public first
{
      int b;
      public:
             void get()
             { 
                  //getdata();
cout<<"enter value in b";
                  cin>>b;
                  
                  }
             void show()
             {
                  dispdata();
                  cout<<"derived class b="<<b;
                  }
};
int main()
{
    sec s;
    s.get();
    s.show();
    getch();
    return 0;
}
EX 2:
#include<iostream.h>
#include<conio.h>
class first
{
      public:
      int a;
};
class sec
{
      public:
             
      int b;
};
      class third:first,sec
      {
            public:
            int c;
            void get()
            {
                 cin>>a>>b>>c;
                 }
            void disp()
            {
                 cout<<a*b*c;
                 }
                 };
            
int main()
{
    third t;
    t.get();
    t.disp();
    getch();
    return 0;
}
EX 3:
SINGLE LEVEL INHERITANCE USING PUBLIC 
#include<iostream.h>
#include<conio.h>
class B
{
      int a;
 public:
        int b;
        void get_ab();
        int get_a(void);
        void show_a(void);
};
class D:public B
{
      int c;
      public:
             void mul(void);
             void display(void);
};
void B::get_ab(void)
{
     a=5; b=10;
}
int B::get_a()
{
    return a;
}
void B::show_a()
{
     cout<<"a="<<a<<"\n";
}
void D::mul()
{
     c=b*get_a();
}
void D::display()
{
     cout<<"a="<<get_a()<<"\n";
     cout<<"b="<<b<<"\n";
     cout<<"c="<<c<<"\n";
}
int main()
{
    D d;
    d.get_ab();
    d.mul();
    d.show_a();
    d.display();
    d.b=20;
    d.mul();
    d.display();
    return 0;
}     





INHERITANCE..IN C++ INHERITANCE..IN C++ Reviewed by Shobhit Goel on May 05, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.