Question

Hi, please provide a code solution written in the C++ language to the following programming problem....

Hi, please provide a code solution written in the C++ language to the following programming problem. The answer you provide has to include code comments, as it is being explained to a person with no technical background. In your answer, please show which portion of the code is a .cpp and which is a .h (if used). We are asked to separate them out (ie. main.cpp, classname.cpp, headername.h). Please show the output of your program to show it compiled error free. Thanks!

SEE PROGRAMMING PROBLEM BELOW

Hi, please provide a code solution written in the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp


// This program demonstrates an abstract base class and overriding a pure virtual function
// This program creates a Circle object and a Rectangle object (both derived from the
// BasicShape class) and displays their areas

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

void displayArea(BasicShape* shape);

int main()
{
       // create a Circle object pointer - cirPtr
       // create a Circle object - cir
       // point the pointer to the object's address
   Circle *cirPtr = nullptr;
   Circle cir(5, 2, 3.0);
   cirPtr = &cir;

       // create a Rectangle object pointer - recPtr
       // create a Rectangle object - rec
       // point the pointer to the object's address
   Rectangle *recPtr = nullptr;
   Rectangle rec(7, 8);
   recPtr = &rec;

       // call the displayArea function on both shapes to display the area
   displayArea(cirPtr);
   displayArea(recPtr);

   system("pause");
   return 0;
}

// this function displays the area of the shape passed to it
void displayArea(BasicShape* shape)
{
   cout << shape->getArea() << endl;
}

Rectangle.h


// This Rectangle class is derived from the BasicShape class. it has a width and lengh attribute
// used to calculate the area (a virtual function overridden from the BasicShape class)

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "BasicShape.h"

class Rectangle : public BasicShape
{
private:
   long width;
   long length;
public:
   // constructor
   Rectangle(long, long);

   // getters
   long getWidth() const;
   long getLength() const;

   // overriden function from BasicShape class
   virtual double calcArea();
};
#endif

BasicShape.cpp


// This BasicShape class is a generic shape with an area, a function to return the area, and a virtual function
// to be overridden by derived classes to calculate the area

#include "BasicShape.h"

// getter
double BasicShape::getArea() const
{
   return area;
}

Circle.cpp


// This Circle class is derived from the BasicShape class. it has a centerX and CenterY coordinates, as well
// as a radius attributes used to calculate the area (a virtual function overridden from the BasicShape class)

#include "Circle.h"

// constructor
Circle::Circle(long x, long y, double r)
{
   centerX = x;
   centerY = y;
   radius = r;

   calcArea();
}

// getters
long Circle::getCenterX() const
{
   return centerX;
}

long Circle::getCenterY() const
{
   return centerY;
}

// overriden virtual function from the BasicShape class
double Circle::calcArea()
{
   area = 3.14159 * radius * radius;
   return area;
}

Circle.h


#ifndef CIRCLE_H
#define CIRCLE_H

#include "BasicShape.h"

class Circle : public BasicShape
{
private:
   long centerX;
   long centerY;
   double radius;
public:
   // constructor
   Circle(long, long, double);

   // getters
   long getCenterX() const;
   long getCenterY() const;

   // overriden function from BasicShape class
   virtual double calcArea();
};
#endif

BasicShape.h

#ifndef BASICSHAPE_H
#define BASICSHAPE_H

class BasicShape
{
protected:
        double area;
public:
        // getter
        double getArea() const;

        // virtual function to be overriden by derived classes
        virtual double calcArea() = 0;
};
#endif

Rectangle.cpp

#include "Rectangle.h"

// constructor
Rectangle::Rectangle(long w, long l)
{
        width = w;
        length = l;
        calcArea();
}

// getters
long Rectangle::getWidth() const
{
        return width;
}

long Rectangle::getLength() const
{
        return length;
}

// overriden virtual function from the BasicShape class
double Rectangle::calcArea()
{
        area = width * length;
        return area;
}

Add a comment
Know the answer?
Add Answer to:
Hi, please provide a code solution written in the C++ language to the following programming problem....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT