Friday, 2 August 2013

Inheritance example ♥

#include<iostream.h>
#include<conio.h>
class value
{
    protected:
        int width,height;
    public:
        void set_values(int a,int b)
        {
            width=a;
            height=b;
        }
};
class coutput
{
    public:
        void output(int i);
};
void coutput::output(int i)
{
    cout<<"\n\t"<<i<< endl;
}
class crectangle: public value, public coutput
{
    public:
    int area ()
    {
        return(width*height);
    }
};
class ctriangle: public value, public coutput
{
    public:
    int area ()
    {
        return(width*height/2);
    }
};
void main()
{
    clrscr();
    crectangle r1;
    ctriangle t1;
    r1.set_values(4,5);
    t1.set_values(4,5);
    r1.output(r1.area());
    t1.output(t1.area());
    getch();
}

Here is the output :)

 

No comments:

Post a Comment