OOPS - [C++ Single Inheritance]

♠ Posted by Unknown in at 03:40

Single Inheritance

Reusability is yet another important feature of OOP. C++ strongly supports the concept of reusability. The C++ classes can be reused in several ways. The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class and the new one is called the derived class or subclass.

The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than one class or from more than one level. A derived class with only one class is called single inheritance. Following figure shows form of single inheritance. The direction of arrow indicate the direction of inheritance.

Image: Single Inheritance

Syntax :

class derived-class-name : visibility-mode base-class-name
{
            members of derived class;
};

In the above syntax the visibility-mode is optional and, if present, may be either private or public. The default visibility-mode is private.

When a base class is privately inherited by a derived class, ‘public members’ of the base class become ‘private members’ of derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class. They are inaccessible to the objects of the derived class.

Example: 1

#include<iostream.h>
#include<conio.h>
//Base Class
class A
{
                        int a;
            public:
                        void getvalue();
                        void putvalue();
};
void A::getvalue()
{
            cout<<"Enter Value for a : ";
            cin>>a;
}
void A::putvalue()
{
            cout<<"The Value of a is : "<<a<<endl;
}

//Derived Class Private Derivation
class B:private A
{
                        int b;
            public:
                        void getdata();
                        void putdata();
};
void B::getdata()
{
 getvalue();
 cout<<"Enter Value for b : ";
 cin>>b;
}
void B::putdata()
{
 putvalue();
 cout<<"The Value of b is : "<<b<<endl;
}

void main()
{
 B objB;
 clrscr();
 objB.getdata();
 objB.putdata();
 getch();
}

On the other hand, when the base class is publicly inherited, ‘public members’ of the base class become ‘public members’ of the derived class and therefore they are accessible to the objects of the derived class. in both the cases, the private members are not inherited and therefore the private members of a base class will never become the members of its derived class.

Example : 2

#include<iostream.h>
#include<conio.h>
//Base Class
class A
{
                        int a;
            public:
                        void getvalue();
                        void putvalue();
};
void A::getvalue()
{
            cout<<"Enter Value for a : ";
            cin>>a;
}

void A::putvalue()
{
            cout<<"The Value of a is : "<<a<<endl;
}

//Derived Class Public Derivation
class B:public A
{
                        int b;
            public:
                        void getdata();
                        void putdata();
};
void B::getdata()
{
 cout<<"Enter Value for b : ";
 cin>>b;
}
void B::putdata()
{
 cout<<"The Value of b is : "<<b<<endl;
}

void main()
{
 B objB;
 clrscr();
 objB.getvalue();
 objB.getdata();
 objB.putvalue();
 objB.putdata();
 getch();
}

0 comments:

Post a Comment