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

Define a class named Rectangle . It should be derived from the BasicShape class.

ID: 658179 • Letter: D

Question

Define a class named Rectangle. It should be derived from the BasicShape

      class. It should have the following members:

Private Member Variables:

      width, a integer used to hold the width of the rectangle.

      length, a integer used to hold the length of the rectangle.

Constructor and Public Member Functions:

Rectangle(double a, int w, int l): constructor that should call the base class constructor to set value of member area with a. The constructor will also set value of member width with w and length with l.

calcArea(): Overridden function that calculates the area of the rectangle (area = length * width) and stores the result in the inherited member area.

print(): Overridden function that will print the length, width and area. This function should call the base class

Explanation / Answer

BasicShape.h

#ifndef BASICSHAPE_H
#define BASICSHAPE_H
using namespace std;

class BasicShape
{
protected:
double area; // data member to hold area

public:
double getArea() const
{
return area;
}
// Pure virtual function
virtual double calcArea() const = 0;
};
#endif



// SAVE IT AS Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGEL_H
#include "BasicShape.h"

class Rectangle : public BasicShape
{
private:
long width; // To hold width
long length; // To hold length

public:
// Constructor
Rectangle(long, long);
long getWidth() const;
long getLength() const;
virtual double calcArea() const;
};
#endif


// SAVE IT AS Rectangle.cpp
#include "Rectangle.h"

// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
Rectangle::Rectangle(long width, long length)
{ this->width = width;
this->length = length;
// Call the overridden calcArea function
calcArea(); }

// getWidth function returns the width *
long Rectangle::getWidth() const
{ return width; }


// getLength function returns the length *
long Rectangle::getLength() const

{ return length; }

// Rectangle::calcArea() calculates the area of a
// Rectangle *
double Rectangle::calcArea() const
{
        return length*width;
}


// MAIN PROGRAM.....

#include "Rectangle.h"
#include <iostream>
using namespace std;

// Function prototype
void DisplayArea(BasicShape* shape)
{
     cout <<"Area Given by " << shape->calcArea() << endl;
}

void main()
{
    Rectangle R1(4,5);  

   cout <<"Rectangle ";
     DisplayArea(&R1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote