Could you comment? Write a base class called Rectangle, which has two private me
ID: 3695094 • Letter: C
Question
Could you comment?
Write a base class called Rectangle, which has two private member variables length and width. Write a constructor functions to set the values of width and length of the rectangle and also a function called area, where you calculate the area of the rectangle. Next derive a class called Square from Rectangle. Note that square has no member variable rather it uses the member variable from base class rectangle. Write constructor for this class and also provide a function called areaSquare to calculate area of square
Explanation / Answer
Program:
//Rectangle class
class Rectangle {
private double width;
private double length;
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
//Area function
public double area() {
return width * length;
}
}
//Square class derived from rectangle class.
class Square extends Rectangle {
//constructor
public Square(double length) {
super(length, length);
}
//area function
public double areaSquare() {
return getLength()*getLength();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.