Implement a base class Rectangle with length and width as private members (use d
ID: 3566010 • Letter: I
Question
Implement a base class Rectangle with length and width as private members (use double). Write a constructor and write an utility method getArea() and get methods getLength() and getWidth() as well.
Implement a derived class Square with constructor that accepts just one value (side?) and invoke base class Rectangle's constructor appropriately. Write a method getSide() which will return the length of one side. Write your code in one source file.
Here is the UML diagram for both classes:
Rectangle:
-length : double
-width : double
+Rectangle(double, double)
+getLength() : double
+getWidth() : double
+getArea() : double
Square:
+Square(double)
+getSide() : double
Explanation / Answer
> class Rectangle{
> double length;
> double width;
>
> public:
>
> Rectangle(double l, double w);
> double getLength();
> double getWidth();
> double getArea();
>
> };
>
> class Square : public Rectangle{
>
> public:
> Square(double side);
> double getSide();
> };
>
> Rectangle::Rectangle(double l, double w){
> length = l;
> width = w;
> }
>
> double Rectangle::getLength(){
> return length;
> }
>
> double Rectangle::getWidth(){
> return width;
> }
>
> double Rectangle::getArea(){
> return length*width;
> }
>
> Square::Square(double side) : Rectangle(side, side){
> }
>
> double Square::getSide(){
> return getLength();
> }
Code in Action : http://ideone.com/Ip4K5H
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.