I was told to write just the function. Did I do that? Question 9. Write a base c
ID: 3695563 • Letter: I
Question
I was told to write just the function. Did I do that?
Question 9. 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
class shape {
protected:
int width, height;
public:
shape(int a=0, int b=0)
{
Width =a;
Height=b;
}
Cout<<”Parent class area”<<endl;
Explanation / Answer
class Rectangle
{
int ar,length,breadth;
Rectangle( )
{
length=10;
breadth=20;
}
Rectangle(int l, int b)
length=l;
breadth=b;
}
void area( )
{
ar=length*breadth;
System.out.println(” “+ar);
}
public static void main(String args[ ])
{
Rectangle r1=new Rectangle( );
Rectangle r2=new Rectangle(15,15);
System.out.println(“Area of rectangle with default constructor is: “);
r1.area( );
System.out.println(“Area of rectangle with parameterized constructor is: “);
r2.area( );
}
}
I did this with parametrized constructor also so take a look afterthis
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.