Define a pure abstract base class called BasicShape that has the following members:
private:
double area; // Hold's the shape's area.
public:
getArea // Fn should return the value in the member variable area.
calcArea // Fn should be a pure virtual fn.
Next define a class named Circle. It should be derived from BasicShape class with the following members:
private:
long centerX; // Holds the X coordinate of the circle's center.
long centerY; // Holds the Y coordinate of the circle's center.
double radius; // Holds the circle's radius.
// Constructor - accepts values for width and length and call the overridden calcArea fn.
Rectangle(double a, long w, long l) :
BasicShape(a)
{
width = w;
length = l;
}
// Accessor functions
long getWidth() const
{
return width;
}
long getLength() const
{
return length;
}
// Overridden calcArea function, defined in implementation section rectangle.cpp.
virtual getCalcArea() const;
};
#endif;
After creating the above classes, I need to create a driver programthat defines a Circle obect and a Rectangle object.
Why do I get the following errors (gist of the project is explained in original post):
1. object of abstract class type "Circle" is not allowed
2. object of abstract class type "Rectangle" is not allowed
3. 'Circle': cannot instantiate abstract class
4. 'Rectangle': cannot instantiate abstract class
// Specification file for BasicShape class.
#ifndef BASICSHAPE_H
#define BASICSHAPE_H
usingnamespace std;
class BasicShape
{
protected:
double area; // Holds the shape's area.
public:
double getArea() const;
// Pure virtual function
virtualvoid getCalcArea() const = 0;
};
#endif
// Defining the Circle class (derivative of BasicShape)
// Specification file for the Circle class.
#ifndef CIRCLE_H
#define CIRCLE_H
// #include "BasicShape.h"
// Constant required to calculate the area of a circle.
//const double PIE = 3.14159;
class Circle : public BasicShape
{
// Private number variables
private:
long centerX; // Holds the X coordinate of the circle's center.
long centerY; // Holds the Y coordinate of the circle's center.
double radius; // Holds the circle's radius.
// Public member functions.
public:
// Default Constructor
Circle() : BasicShape ()
{
centerX = 0;
centerY = 0;
radius = 0.0;
}
// Constructor
Circle(long, long, double);
// Accessor functions
long getCenterX() const;
long getCenterY() const;
// Overridden calcArea function, defined in implementation section circle.cpp.
void calcArea();
};
#endif;
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle : public BasicShape
{
// Private number variables
private:
long width; // Holds the width of the rectangle.
long length; // Holds the length of the rectangle.
// Public member functions.
public:
// Default Constructor
Rectangle() : BasicShape()
{
width = 0;
length = 0;
}
// Constructor
Rectangle(long, long);
// Accessor functions
long getWidth() const;
long getLength() const;
// Overridden calcArea function, defined in implementation section rectangle.cpp.
void calcArea();
};
#endif;
// Implementation file for the BasicShape class.
double BasicShape::getArea() const
{
return area;
}
// Implementation file for Circle class.
// Constant double required for circle calculations.
Circle::Circle(long x, long y, double r)
{
centerX = x;
centerY = y;
radius = r; // Hold's the circle's radius.
calcArea();
}
long Circle::getCenterX() const
{
return centerX;
}
long Circle::getCenterY() const
{
return centerY;
}
// Calculate the area of a circle.
void Circle::calcArea()
{
area = 3.14159 * radius * radius;
}
// Implementation file for Rectangle class.
Rectangle::Rectangle(long w, long l)
{
width = w;
length = l;
calcArea();
}
long Rectangle::getWidth() const
{
return width;
}
long Rectangle::getLength() const
{
return length;
}
void Rectangle::calcArea()
{
area = length * width;
}
// Main Function
#include <iostream>
usingnamespace std;
int main()
{
long x; // Holds the X coordinate of the circle's center.
long y; // Holds the Y coordinate of the circle's center.
long w; // Holds the width of the rectangle.
long l; // Holds the length of the rectangle.
double r; // Holds radius value.
// Get circle values and calculate its area.
cout << "Please enter the x coordinate of the circle's center: ";
cin >> x;
cout << endl;
cout << "Please enter the y coordinate of the circle's center: ";
cin >> y;
cout << endl;
cout << "Please enter the radius of the circle: ";
cin >> r;
cout << endl;
// Create a Circle object for a BasicShape.
Circle circ(x, y, r);
cout << "The area of the circle is " << circ.getArea() << ".\n";
// Get rectangle values and calculate its area.
cout << "\n\nPlease enter the width of the rectangle: ";
cin >> w;
cout << endl;
cout << "Please enter the length of the rectangle: ";
cin >> l;
cout << endl;
// Create a Circle object for a BasicShape.
Rectangle rect(w, l);
cout << "The area of the rectangle is " << rect.getArea() << ".\n";
return 0;
}
// Base class
class BasicShape {
protected:
double area;
public:
void setArea(double);
double getArea() const;
// pure virtual function providing interface framework.
virtualvoid calcArea() = 0;
};
// Derived classes
class Circle : public BasicShape {
private:
long centerX;
long centerY;
double radius;
public:
Circle(long, long, double)
{
centerX = 0;
centerY = 0;
radius = 0.0;
}
long getCenterX() const;
long getCenterY() const;
void calcArea();
};
class Rectangle : public BasicShape {
private:
long width;
long length;
public:
Rectangle(long, long)
{
width = 0;
length = 0;
}
long getWidth() const;
long getLength() const;
void calcArea();
};
double BasicShape::getArea() const
{
return area;
}
long Circle::getCenterX() const
{
return centerX;
}
long Circle::getCenterY() const
{
return centerY;
}
void Circle::calcArea()
{
area = 3.14159 * radius * radius;
}
long Rectangle::getWidth() const
{
return width;
}
long Rectangle::getLength() const
{
return length;
}
void Rectangle::calcArea()
{
area = width * length;
}
#include <iostream>
usingnamespace std;
int main(void)
{
long x;
long y;
long w;
long l;
double r;
// Get circle values and calculate its area.
cout << "Please enter the x coordinate of the circle's center: ";
cin >> x;
cout << "Please enter the y coordinate of the circle's center: ";
cin >> y;
cout << "Please enter the radius of the circle: ";
cin >> r;
Circle circ(x, y, r);
cout << "The area of the circle is " << circ.getArea() << ".\n";
// Get rectangle values and calculate its area.
cout << "\n\nPlease enter the width of the rectangle: ";
cin >> w;
cout << "Please enter the length of the rectangle: ";
cin >> l;
// Create a Circle object for a BasicShape.
Rectangle rect(w, l);
cout << "The area of the rectangle is " << rect.getArea() << ".\n";
return 0;
}
1. calcArea() should be const
2. Circle and Rectangle class ctor's were setting everything to zero, giving you wrong outputs
3. calcArea() should return double, not void
4. without using pointers to base class you're not really capturing the power of polymorphism
5. finally, my comments re virtual destructors still stand
#include <iostream>
usingnamespace std;
class BasicShape {// Base class
protected:
double area;
public:
void setArea(double);
double getArea() const;
// pure virtual function providing interface framework.
virtualdouble calcArea() = 0;
};
class Circle : public BasicShape {// Derived classes
private:
long centerX;
long centerY;
double radius;
public:
Circle(long X, long Y, double r)
{
centerX = X;
centerY = Y;
radius = r;
}
long getCenterX() const;
long getCenterY() const;
double calcArea();
};
class Rectangle : public BasicShape {
private:
long width;
long length;
public:
Rectangle(long w, long l)
{
width = w;
length = l;
}
long getWidth() const;
long getLength() const;
double calcArea();
};
double BasicShape::getArea() const
{
return area;
}
long Circle::getCenterX() const
{
return centerX;
}
long Circle::getCenterY() const
{
return centerY;
}
double Circle::calcArea()
{
return 3.14159 * radius * radius;
}
long Rectangle::getWidth() const
{
return width;
}
long Rectangle::getLength() const
{
return length;
}
double Rectangle::calcArea()
{
return width * length;
}
int main(void)
{
long x;
long y;
long w;
long l;
double r;
// Get circle values and calculate its area.
cout << "Please enter the x coordinate of the circle's center: ";
cin >> x;
cout << "Please enter the y coordinate of the circle's center: ";
cin >> y;
cout << "Please enter the radius of the circle: ";
cin >> r;
Circle circ(x, y, r);
cout << "The area of the circle is " << circ.Circle.calcArea() << ".\n";
// Get rectangle values and calculate its area.
cout << "\n\nPlease enter the width of the rectangle: ";
cin >> w;
cout << "Please enter the length of the rectangle: ";
cin >> l;
// Create a Circle object for a BasicShape.
Rectangle rect(w, l);
cout << "The area of the rectangle is " << rect.calcArea() << ".\n";
}