0% found this document useful (0 votes)
8 views2 pages

Example of Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Example of Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Example of inheritance

class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle();
Rectangle(int l,int b);
Rectangle(Rectangle &r);
int getLength(){return length;}
int getBreadth(){return breadth;}
void setLength(int l);
void setBreadth(int b);
int area();
int perimeter();
bool isSquare();
~Rectangle();
};
class Cuboid:public Rectangle
{
private:
int height;
public:
Cuboid(int h)
{
height=h;
}
int getHeight(){return height;}
void setHeight(int h){height=h;}
int volume(){return getLength()*getBreadth()*height;}
};

int main()
{
Cuboid c(5);
c.setLength(10);
c.setBreadth(7);
cout<<"Volume is "<<c.volume()<<endl;

}
Rectangle::Rectangle()
{
length=1;
breadth=1;
}
Rectangle::Rectangle(int l,int b)
{
length=l;
breadth=b;
}
Rectangle::Rectangle(Rectangle &r)
{
length=r.length;
breadth=r.breadth;
}
void Rectangle::setLength(int l)
{
length=l;
}
void Rectangle::setBreadth(int b)
{
breadth=b;
}
int Rectangle::area()
{
return length*breadth;
}
int Rectangle::perimeter()
{
return 2*(length+breadth);
}
bool Rectangle::isSquare()
{
return length==breadth;
}
Rectangle::~Rectangle()
{
// cout<<"Rectangle Destroyed";
}

You might also like