Create a class named 'Rectangle' with two data members 'length' and 'breadth' and two methods to print the area and perimeter of the rectangle respectively. Its constructor having parameters for length and breadth is used to initialize length and breadth of the rectangle. Let class 'Square' inherit the 'Rectangle' class with its constructor having a parameter for its side (suppose s) calling the constructor of its parent class as 'super(s,s)'. Print the area and perimeter of a rectangle and a square.
Answers
Answered by
2
Answer - Note that when a body is moving with a uniform velocity, it is moving in a straight line. This is because constant velocity means that the direction of motion is fixed.
Answered by
29
class Rectangle
{
int length,breadth;
public:
Rectangle(int l, int b)
{
length = l;
breadth = b;
}
void print_area()
{
cout << length*breadth << endl;
}
void print_perimeter()
{
cout << 2*(length+breadth) << endl;
}
};
class Square : public Rectangle
{
public:
Square(int side) : Rectangle(side,side)
{}
};
int main()
{
Rectangle r(4,5);
Square s(4);
r.print_area();
r.print_perimeter();
s.print_area();
s.print_perimeter();
return 0;
}
Similar questions