Program to illustrates Single Inheritance by PUBLIC Derivation


#include <iostream.h>
/*----------------------Class Interfaces-------------------------*/
class A
{
        int a1;                    // private data member
    public :
        int a2;                    // public data member

        void set_a1(int);        // public member functions
        void display_a1a2();
};
// B as an extension of A
class B : public A                // public derivation
{
        int b;                    // private data memeber
    public :
        void set_a2_and_b(int, int);// public member functions
        void display_b();
};

/*----------------------Class Implementations---------------------*/

// member function definitions for class A
void A::set_a1(int x)
{
    a1 = x;
}
void A::display_a1a2()
{
    cout << "a1 = " << a1 << "\n";
    cout << "a2 = " << a2 << "\n";
}

// member function definitions for class B
void B::set_a2_and_b(int x, int y)
{
    a2 = x;    // accessing public data member of base directly
    b = y;
}
void B::display_b()
{
    cout << "b  = " << b << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    B obj;

    // accessing public member function of the base directly
    obj.set_a1(10);

    obj.set_a2_and_b(20,30);

    // accessing public member function of the base directly
    obj.display_a1a2();

    obj.display_b();
}
/*
a1 = 10
a2 = 20
b  = 30
*/

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|