Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

03.(25%) write a programinC++. Define a hape class with two member variables wid

ID: 3915932 • Letter: 0

Question

03.(25%) write a programinC++. Define a hape class with two member variables width and height, both are of protected access control; define a constructor with two default arg default initialized to 0) that can be assigned to the two member variables correspondingly. In Shape, also define a pure virtual function a Triangle and Rectangle,both inherit from Shape; redefine the member function area0 for calculating the area of that specific shape. Both sub-classes should have a constructor with two default (both default initialized to 0) that can be assigned to the two inherit member variables correspondingly by calling the constructor of the base class in the constructor initializer list. In the main) function for s (both rea0 with a return type of float. Then define two sub-classes arguments testing purpose is provided. Note: All member functions should be in-line functions.

Explanation / Answer

#include <iostream>
using namespace std;

// Abstract class
class Shape
{
protected:
int width;
int height;

//constructor
Shape(){
width=0;
height=0;
}
  
//setting data
public:
void setData(int w,int h)
{
width=w;
height=h;
}

// virtual Function
virtual float area() = 0;
};

//Traingle class
class Traingle : public Shape
{
  
public:
float area()
{ return 0.5*width*height; }
};

//Rectanle class
class Rectanle : public Shape
{
public:
float area()
{ return width*height; }
};

int main()
{
Traingle t;
Rectanle r;

t.setData(4,5);
cout<<"Area of Traingle: " << t.area()<<" ";
  
r.setData(5,6);
cout<<"Area of Rectanle: " << r.area()<<" ";

return 0;
}