Inheritance and virtual functions

Hi All,
I am working on an assignment, where I am meant to construct a Point, and Shape class, from which I should derive, Circle , Square and Triangle derived classes and calculate some geometry.
I am starting with the circle class, but and having issues creating the abstract data type, and passing Point objects into the classes.
I am getting the error:
cannot declare the varible 'c1; to be of abstract type 'Circle'
because the following virtual function are pure within 'Circle;
virtual double Shape::area()
virtual double Shape::circumference
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
  #include <iostream>
#include <cmath>
using namespace std;

#define PI 3.14159

//Part A

class Point {
	public:
		int x;
		int y;
		Point(int px , int py) : x(px) , y(py){}

};
// Part B
 class Shape {
 	public:
		virtual double area() = 0;
		virtual double circumference() = 0;
		//virtual boundbox() = 0;        // vector?
		virtual void display() = 0;
};
class Circle: public Shape {
public:
    Circle();
    Circle(const Point &pt0, const Point &pt1);
    double area(const Point &pt0, const Point &pt1);
    double circumference(const Point &pt0, const Point &pt1);
    //Point boundBox(const Point &pt0, const Point &pt1);
    void display();
};

Circle::Circle(){
}
Circle::Circle(const Point &pt0, const Point &pt1){
}

double Circle::area(const Point &pt0, const Point &pt1){
    double areaResult;
    double circRadius = pt1.y - pt0.y;
    areaResult = circRadius * PI;
    cout << areaResult << endl;
}
double Circle::circumference(const Point &pt0, const Point &pt1){

}
/*Point Circle::boundBox(){

}
*/

void Circle::display(){}


// Part D
int main() {
	Point pt0(0, 0); // Point Center
	Point pt1(0, 23); // Radius

	Circle c1;
	c1.area(pt0, pt1);
}



Any advice?
The area(), circumference() and display() methods should all take no parameters. They should compute their values based on members of the class. For example, a circle has a center point and a radius. So class Circle might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Circle: public Shape {
public:
    Circle();
    Circle(const Point &_center, double _radius) :
         center(_center),
         radius(_radius)
         {}
    // over ridden from class Shape
    double area();
    double circumference();
    void display();
private:
    Point center;
    double radius;
};

Ok, but I need to pass in 2 Point obejects. One for the center, and one for a point on the perimeter, then calculate things like radius, and circumference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Circle: public Shape {
public:
    Circle();
    Circle(const Point &_center, const Point & _outer) :
         center(_center),
         outer(_outer)
         {}
    // over ridden from class Shape
    double area();
    double circumference();
    void display();
private:
    Point center;
    Point outer;
};

So here, the overloaded constructer will create to the accept two point arguments, and they my methods should be able to use them?
Last edited on
Yes, but you may be making a common mistake: just because the constructor passes in the center and a point doesn't mean that you store those exact values. I still think it's more useful to store the center and the radius. After all, you'll need to radius to compute the area and circumference anyway, so I'd have the constructor compute the radius from the two points and store that value along with the center.
Topic archived. No new replies allowed.