Ticker

6/recent/ticker-posts

DERIVED AND BASE CLASS IN C++ PROGRAMMING

DERIVED AND BASE CLASS

1. Base class: - A base class can be defined as a normal C++ class, which may consist of some data and functions. 

Ex - 

Class Base 

        : : : : : 

2. Derived Class: - 

i). A class which acquires properties from base class is called derived class. 

ii). A derived class can be defined by class in addition to its own detail. 

The general form is ñ Class derived-class-name: visibility-mode base-class-name 


            : : : : : 

The colon indicates that the derived class name is derived from the base-class- name. The visibility-mode is optional and if present it is either private or public.

PUBLIC INHERITANCE

i) When the visibility-mode is public the base class is publicly inherited. 

ii) In public inheritance, the 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. 

iii) Syntax is - 

class ABC : public PQR 

        members of ABC 
}; 

Where PQR is a base class illustrate public inheritance. 

iv) Let us consider a simple example to illustrate public inheritance. 

# include 
class base 

        int no1; 
    public: 
                int no2; 
                void getdata();
                int getno1();
                void showno1();
};
Class derived: public Base // public derivation. {
        int no3;
            public:
                void add();
                void display();
};
void Base :: getdata()
{
        no1 = 10;
        no2 = 20;
}
int Base :: getno1()
{
        return no1;
}
void Base :: showno1()
{
        cout <<îNumber1 =î<<no1 <<î\nî;
}
void Derived :: add()
{
        no3 = no2 + getno1(); // no1 is private
}
void Derived :: display()
{
        cout << ìNumber1 = ì<<getno1() <<"\n";
        cout << "Number2 = "<<no2<<"\n";
        cout << "Sum " <<no3 << "\n";
}
main ( )
{
        derived d;
        d.getdata ( );
        d.add ( );
        d.showno1 ( );
        d.display ( ) ;
        d.b = 100;
        d.add ( ) ;
        d.display ( ) ;
        return 0;
}

The output of the program is 
            Number1 = 10 
            Number1 = 10 
            Number2 = 20 
            Sum = 30 

            Number1 = 10 
            Number2 = 100 
            Sum = 110 

v) In the above program class Derived is public derivation of the base class Base. Thus a public member of the base class Base is also public member of the derived class Derived.

PRIVATE INHERITANCE

i) When the visibility mode is private, the base class is privately inherited. 

ii) When a base class is privately inherited by a derived class, public members of the base class becomes private members of the 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. 

iii) Public member of a class can be accessed by its own objects using the dot operator. So in private Inheritance, no member of the base class is accessible to the objects of the derived class. 

iv) Syntax is - 

class ABC: private PQR 

        member of ABC 

v) Let us consider a simple example to illustrate private inheritance. 

#include 
class base 

        int x; 
            public : 
                int y; 
                void getxy() ; 
                int get_x(void) ; 
                void show_x(void) ; 
}; 
void base ::getxy(void) 

        cout<<"Enter Values for x and y : " ; 
        cin >> x >> y ; 

int base : : get_x() 

        return x; 

void base :: show_x() 
{
        cout <<îx = ì << x << ì\nî;
}
void Derived : : mul( )
{
        getxy();
        z = y * get_x ( ) ;
}
void Derived ::display()
{
        show_x ( ) ;
        cout <<îy = ì <<y<<î\nî;
        cout<<îz = ì <<z<<î\nî;
}
main ( )
{
        Derived d;
        d. mul();
        d.display() ;
        //d.y = 4a; wonít work y has become private. d.mul();
        d.display();
}
Output: -
        Enter values for x and y: 510
        X = 5
        Y = 10
        Z = 50
        Enter values for x and y:1220
        X = 12
        Y = 20
        Z = 240

PROTECTED MEMBERS

1. If we want to inherit private data by a class, the only option is to change the visibility limit from private to public, but this will eliminate the advantage of data hiding. 

2. Therefore to achieve data hiding, C++ provides a third visibility modifier, protected which has limited purpose in inheritance. 

3. A member declared protected is accessible by the member functions within its class and any class immediately derived from it. It cannot be accessed by the functions outside these two classes. 

4. When a protected member is inherited in public mode, it becomes protected in the derived class too, and therefore is accessible by the member.

ALSO SEARCH:

"base class and derived class in C++"

"differentiate between base class and derived class in C++"

"difference between base class and derived class in C++"

"what is derived class in C++ with example"

"what is derived class in C++"

"how to write a derived class in C++"

"derived and base class in C++"

"in inheritance order of execution of base class and derived class destructor are"

"base class and derived class in C++"

"in case of inheritance where both base and derived class"

"base and derived class constructor in C++"

"pointer to derived class and base class in C++"

"in case of inheritance where both base and derived class are having constructor and destructor"

"base class and derived class have same function in C++"