Friday, 26 July 2013

Simple Inheritance Example ♥

#include<iostream.h>
#include<conio.h>
class shape
{
    public:
        int width,height;
        void set_width(int w)
        {
            width = w;
        }
        void set_height(int h)
        {
            height = h;
        }
};
class rectangle:public shape
{
    public:
    int get_area()
    {
        return width * height;
    }
};
void main()
{
    clrscr();
    rectangle r1;
    r1.set_width(5);
    r1.set_height(7);
    cout <<"\n\tTotal area: " << r1.get_area();
    getch();
}

Here is the output :)


No comments:

Post a Comment