Pure Abstract Base Class Project

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.

public:
// Default constructor
Circle() : BasicShape()
{
centerX = 0;
centerY = 0;
radius = 0.0;
}

// Constructor - accepts values for centerX, centerY and radius. It should call the overridden calcArea fn.

// Accessor functions
long getCenterX() const
{
return centerX;
}

long getCenterY() const
{
return centerY;
}

// Overridden calcArea function, defined in implementation section circle.cpp.
virtual double getCalcArea() const;
}; {
area = 0.0;
}

// Constructor
BasicShape(double a)
{
area = a;
}

/* // The setting the attribute data
void set(double a)
{
area = a;
} */

// Accessor function.
double getArea() const
{
return area;
}

// Pure virtual function - calculates the area of a circle (3.14159 * radius * radius)
virtual double getCalcArea() const = 0;

Next define a class called Rectangle also derived from BasicShape with the following:

private:
long width; // Holds the width of the rectangle.
long length; // Holds the length of the rectangle.

// Public member functions.
public:

Rectangle() : BasicShape()
{
width = 0;
length = 0;
}

// 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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Specification file for BasicShape class.

#ifndef BASICSHAPE_H
#define BASICSHAPE_H

using namespace std;

class BasicShape
{
protected:
	double area;	// Holds the shape's area.

public:
	double getArea() const;

	// Pure virtual function
	virtual void 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>
using namespace 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;
}
Last edited on
The thing that strikes me while reading your program is the absence of virtual destructors, try adding them in ...
http://stackoverflow.com/questions/270917/why-should-i-declare-a-virtual-destructor-for-an-abstract-class-in-c

edit: see also this recent post http://www.cplusplus.com/forum/general/202622/
Last edited on
OK, I made some changes, the only problem I'm having now is the area for the circle and the rectangle are not calculating correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Base class
class BasicShape {

protected:
	double area;
	
public:
	void setArea(double);
	double getArea() const;
	
	// pure virtual function providing interface framework.
	virtual void 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>
using namespace 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
using namespace std;

class BasicShape {// Base class

protected:
	double area;
public:
	void setArea(double);
	double getArea() const;

	// pure virtual function providing interface framework.
	virtual double 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";
}

Topic archived. No new replies allowed.