Passing objects into class constructors

Hi All,
I am working on a problem where I am meant to create a Point class, and then use these objects to pass into a Shape class hierarchy. I am comfortable with the concepts of derived classes, and overiding/virtual functions and operator overloading.
What is giving me problems is the idea of Points, with X/Y co ordinates, and passing them into functions, particularly constructors of the class hierarchy.

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


class Point{
public:
int x;
int y;
Point(int px, int py): x(px), y(py){}
friend ostream &operator << (ostream &stream, Point &p);
Point operator+(const Point& pt);
Point operator-(const Point& pt);
};
 
class Shape {
public:
virtual double area() = 0;
virtual double circumference() = 0;
virtual void display() = 0;
};

class Circle:: public Shape {

Point c1,c2;
public:
double distrad, area_c, circum_c;
Circle();
Circle(const Point &pt0, const Point &pt1): c1(pt1), c2(pt2){}
double area();
double circumference();
void display();
};
Circle::Circle(const Point &pt0, const Point &pt1){
c1 = pt0;
c2 = pt2;
}


}

I'm struggling with my Circle() overloaded constructor, and setting it up for calculations.

Any advice is much appreciated
Last edited on
Where is Shape's constructor?
Your line 26 implements Circle(const Point &, const Point &). Why do you reimplement the same constructor on lines 31-34 (and do it differently too)?
Last edited on
I was trying to think about how to define the Circle constructor there. I'm looking to have the Circle constructor accept two point objects as arguments, and set one to c1(center point) and c2(a point on the perimeter, or radius marker)
Your line 26 does exactly that.
So then I do now need to define lines 31-33? I can go ahead and start making geometric functions (area, circumference etc...)

and in main:

int main() {
Point pt0(0, 0); // Point Center
Point pt1(0, 23); // Radius
Circle oc; //create a circle object, ( or can I overload the constructor here? Circle oc(&pt1, &pt2)
oc.Circle(pt 0, pt1) // call the constructor?


Last edited on
Updated code:

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

class Point{
public:
int x;
int y;
Point(int px, int py): x(px), y(py){}
friend ostream &operator << (ostream &stream, Point &p);
Point operator+(const Point& pt);
Point operator-(const Point& pt);
};
 
class Shape {
public:
virtual double area() = 0;
virtual double circumference() = 0;
virtual void display() = 0;
};

class Circle:: public Shape {

Point c1,c2;
public:
double distrad, area_c, circum_c;
Circle();
Circle(const Point &pt0, const Point &pt1): c1(pt1), c2(pt2){}
double area();
double circumference();
void display();
};
int main() {
Point pt0(0, 0); // Point Center
Point pt1(0, 23); // Radius
Circle oc; //create a circle object,? or
Circle oc(&pt1, &pt2);// think?
oc.Circle(pt 0, pt1); // call the constructor? not needed?
}


which of the last three lines is the proper way to create the object with the appropritate arguments?
Last edited on
I am still workign at this,

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
#include <iostream>

using namespace std;

class Point{
public:
int x;
int y;
Point(int px, int py): x(px), y(py){}
friend ostream &operator << (ostream &stream, Point &p);
Point operator+(const Point& pt);
Point operator-(const Point& pt);
};

class Shape {
public:
virtual double area() = 0;
virtual double circumference() = 0;
virtual void display() = 0;
};

class Circle: public Shape{

Point c1,c2;
public:
double distrad, area_c, circum_c;
Circle();
Circle(const Point &pt0, const Point &pt1): c1(pt0), c2(pt1){}
double area();
double circumference();
void display(){cout << "display function holder" << endl; }
};

int main(){
Point pt0(0, 0); // Point Center
Point pt1(0, 23); // Radius
Circle oc(&pt0, &pt1);// think?
oc.display();
}


I've tried passing the points in as references(above) and as copies, but I can't get it figured out.

22: Error: no matching function for call Circle::Circle(Point*, Point*)


how am I supposed to pass these objects into the new object?

When I try it:
[code]
Circle oc(pt0,pt1);
I get error:
28: undefined reference to 'vtable for Circle'


any help?
Last edited on
That last syntax is correct. If you had searched the web with the error message (undefined reference to vtable for) you could have found something like:
http://stackoverflow.com/questions/7665190/undefined-reference-to-vtable-for-xxx

Where are the implementations for area() and circumference()?

(And default constructor too, but that you could write:)
[code]class Circle // ...
Circle() = default;
// other members
};
Topic archived. No new replies allowed.