Thursday 3 October 2013

Inheritance

Inheritance
When we make a class on the basis of the existing class is known as inheritance. The existing class in known as  Base class( or parent class) and the new class is known as derived class
( or child class)

The general form is ,

        class DerivedClassName : BaseClassName
        {
        body of the Derived Class
        }



Here we have 5 types of inheritance
1) single inheritance
2 multilevel
3 multiple
4 hierarchial

5 hybride 
single  inheritance

                
In the case of Single Inheritance , we derive the single derived class , on the basis of the single base class.
 
 # include <iostream.h>
    # include <conio.h>
          class A
          {
            int a;
        public:
        void setA(int i)
        {
        a=i;
        }
        void showA( )
        {
        cout<<"a="<<a;
        }
             };

         class B : public A
        {
        int b;
        public:
        void setB(int j)
{
b=j;
        }
 void showB( )
        {
cout<<"b="<<b;
               
               }
               };

                 void main()
          {
 B ob;
ob.setA(5);
ob.setB(10);
ob.showA();
ob.showB();
getch();
}

private members :
                In the case of inheritance ,  the private memebers have the following features,
(i) These members are not directly accesable by the                         object of a class.

                        e.g.
                                ob.b=10; is Invalid

(ii) These members will not get inherited.That is, they cannot  be used in the derived class or by the object of the derived class.
                                        showB()
                                        cout<<"a="<<a;
                        it will be invalid

public members :
        In the case of inheritance ,  the public  memebers have the following features,

(i) These members are  directly accesable by the                      object of a class.
                        e.g.
                                ob.setA(5) ; is valid

(ii) These members will  get inherited. That is, they can  be used in the derived class  or by the object of the derived class.

                        B ob;
       
                        ob.showA();   

No comments:

Post a Comment