Program to illustrates Multiple Inheritance


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class base1                        // base class
{
        int x;
    public :
        void set_x(int);
        void display_x();
};
class base2                        // base class
{
        float y;
    public:
        void set_y(float);
        void display_y();
};
class derived : public base1, private base2
{
        char str[10];
    public:
        void set(float, char*);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'base1'

void base1::set_x(int c)
{
    x = c;
}
void base1::display_x()
{
    cout << "x = " << x << "\n";
}
// member function definitions for 'base2'
void base2::set_y(float c)
{
    y = c;
}
void base2::display_y()
{
    cout << "y = " << y << "\n";
}
// member function definitions for 'derived'
void derived::set(float b, char* c)
{
    /* public member of 'base2' becomes private in 'derived'
     hence, accessed within the function body */
    set_y(b);

    strcpy(str, c);
}
void derived::display()
{
    /* public member of 'base2' becomes private in 'derived'
        hence, accessed within the function body */
    display_y();

    cout << "str = ";
    cout << str << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    derived D;

    /* public member of the 'base1' is public in 'derived'
       hence, can be directly used */
    D.set_x(10);
    D.set(25.3, "God");
    /* public member of the 'base1' is public in 'derived'
       hence, can be directly used */
    D.display_x();

    D.display();
}

0 comments:

Post a Comment

 
 
 
 


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