Question About Defining Objects in Class Headers

I am attempting to do something fairly complicated, so I'll boil it down to the bare elements here. Essentially, I am writing a program that creates an ellipse (an ellipse object is defined by its minor and major radii), assigns its center point coordinate, and returns a set of coordinates as traced out by a parametric function. So, here are my files:

"class_definitions.h"
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
#ifndef CLASS_DEFINITIONS_H_
#define CLASS_DEFINITIONS_H_

// coordinate class:
class coordinate
{
public:
	coordinate(double, double); // constructor
	double x, y; // the coordinates
};

// closed_curve class (abstract base class):
class closed_curve
{
public:
	virtual coordinate place(double); // return a coordinate object for a point on the curve
};

// ellipse class:
class ellipse : public closed_curve
{
public:
	ellipse(double a_holder, double b_holder); // constructor

	double a, b; // major radius = a; minor radius = b

	coordinate place(double);
};
#endif 


"class_methods.cpp"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "class_definitions.h"
#include <math.h>

// constructor of coordinate object:
coordinate::coordinate(double x_holder, double y_holder)
{
	x = x_holder;
	y = y_holder;
}

// methods for ellipse:
// constructor:
ellipse::ellipse(double a_holder, double b_holder)
{
	a = a_holder;
	b = b_holder;
}

coordinate ellipse::place(double t)
{
	double pi = 3.14159265;
	coordinate ellipse_coordinate(x + a*cos(2*pi*t), y + b*sin(2*pi*t));
	return ellipse_coordinate;
}


"main.cpp"
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
#include "class_definitions.h"

int main()
{
	ellipse ellipse_object(1,2);
	coordinate ellipse_center(3,4);
        ellipse_object.place(0.1);

	return 0;
}


1) Is it legal to declare place using virtual in the base abstract class to give late binding? I know I can do this for functions that return built-in types, but I'm not sure if this is allowed for functions that return class objects themselves.
2) I know that, in the place method definition in the cpp file, it has no way to find out what x and y are (since they're not part of the ellipse class). How do I pull in x and y into this method, keeping the argument list of place as is?

Thanks!
1. closed_curve is not abstract. I'm not quite sure what you mean by it being legal or illegal, it seems just fine.
2. Why wouldn't it know what x and y are? It does because the function is in the class scope.
Last edited on
it has no way to find out what x and y are (since they're not part of the ellipse class). How do I pull in x and y into this method
¿what are 'x' and 'y'?
1) I should be have been more clear. I meant... "Can we make 'place' virtual?" I thought an abstract class is one that doesn't actually ever get instantiated (and therefore no constructors are needed)? Perhaps I need more clarity on this concept.
2) Even though 'place' is in the scope of the 'ellipse' class, 'place' is not in the scope of the 'coordinate' class.
1. An abstract class is one which contains at least one pure virtual member function. It still needs constructors and destructors for its member variables.

This is an abstract class:
1
2
3
4
5
6
7
8
9
10
11
12
class Base
{
    string s;
public:
    Base() : s("some text") {}
    Base(const Base &from) : s(from.s) {}
    virtual Base &operator=(const Base &from){ s = from.s; return*this; }

    virtual void DoSomething() = 0; //pure virtual, makes this class abstract

    virtual ~Base(){}
};


2. Oops. I misread your code. Where would you want x and y to come from?
Last edited on
'x' and 'y' correspond to the coordinate for the center of the ellipse object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class closed_curve
{
public:
	virtual coordinate place(double); // return a coordinate object for a point on the curve
};

// ellipse class:
class ellipse : public closed_curve
{
public:
	ellipse(double a_holder, double b_holder); // constructor

	double a, b; // major radius = a; minor radius = b

	coordinate place(double);
};


Where do you want x and y to come from? There is no x or y in either of the classes in the inheritance tree that place() is involved with.
Last edited on
I want x and y from the coordinate object for the center of the ellipse (see the line in main.cpp). I really don't know how to get access to those values, though, since I am restricted to use just one argument in my 'place' method call.
Your task is impossible to complete because you can not ever access another object like that. If you are allowed you can force the ellipse object to be constructed with a coordinate object, but other than that what you seek to achieve is not possible. You must have the variables you wish to access within scope.
That's what I was kind of thinking...

So, if I construct an ellipse object with a coordinate object, would it go something like this?:

1
2
3
4
5
6
ellipse::ellipse(double a_holder, double b_holder, double x_holder, double y_holder)
{
	a = a_holder;
	b = b_holder;
        coordinate::coordinate ellipse_center(x_holder, y_holder);
}
You should have the coordinate object be a member of your class just like and and b.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ellipse{
public:
//methods

  double major_radius, minor_radius; //use descriptive names
  coordinate center;
};

ellipse::ellipse( double a, double b, coordinate center ): major_radius(a), minor_radius(b), center(center){}
ellipse::ellipse( double a, double b, double x, double y ): major_radius(a), minor_radius(b), center(x,y){}

coordinate ellipse::place(double t){
  double x=center.x, y=center.y; //you can use them directly if you want
  //...
}
So, I have changed my code to the following:

"class_definitions.h"
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
#ifndef CLASS_DEFINITIONS_H_
#define CLASS_DEFINITIONS_H_

// coordinate class:
class coordinate
{
public:
	coordinate(); // empty constructor
	coordinate(double x_holder, double y_holder); // constructor
	double x, y; // the coordinates
};

// closed_curve class (base class):
class closed_curve
{
public:
	virtual coordinate place(double); // return a coordinate object for a point on the curve
};

// ellipse class:
class ellipse : public closed_curve
{
public:
	ellipse(double a_holder, double b_holder, double x_holder, double y_holder); // constructor

	double a, b, x, y; // major radius = a; minor radius = b
	coordinate ellipse_center; // a coordinate object

	coordinate place(double);
};
#endif 


"class_methods.cpp"
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
#include "class_definitions.h"
#include <math.h>

// empty constructor of coordinate object:
coordinate::coordinate()
{

}

// constructor of coordinate object:
coordinate::coordinate(double x_holder, double y_holder)
{
	x = x_holder;
	y = y_holder;
}

// methods for ellipse:
// constructor:
ellipse::ellipse(double a_holder, double b_holder, double x_holder, double y_holder)
{
	a = a_holder;
	b = b_holder;

	coordinate ellipse_center(x_holder, y_holder);
}
// return the coordinates for a point on the curve:
coordinate ellipse::place(double t)
{
	double pi = 3.14159265;
	coordinate ellipse_coordinate(ellipse_center.x + a*cos(2*pi*t), ellipse_center.y + b*sin(2*pi*t));
	return ellipse_coordinate;
}


"main.cpp"
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
#include "class_definitions.h"

int main()
{
	ellipse ellipse_object(1,2,3,4);
	ellipse_object.place(0.1);

	return 0;
}


I now get the error:
ld: symbol(s) not found

Before, when I double-click on an error message (I'm using Eclipse), it takes me right to the line of code that contains the error. However, this time, it doesn't take me into the code. I don't know what this error means. Could anyone please advise? Thanks.
Probably some misconfiguration of your project. ¿Is that the whole error?

1
2
3
4
5
6
7
ellipse::ellipse(double a_holder, double b_holder, double x_holder, double y_holder)
{
	a = a_holder;
	b = b_holder;

	coordinate ellipse_center(x_holder, y_holder); //this is a local variable
}

You've got uninitialized members.
Alos, it makes no sense to have 'center' and 'x', 'y' as members, choose one representation.
To initialize the center use initialization list
1
2
3
4
5
ellipse::ellipse( double a, double b, double x, double y ): //<- see the colon, it mark the start of the initialization list
  major_radius(a), 
  minor_radius(b), 
  center(x,y) //we call the constructors of our members here
{}
I have changed to the following:

"class_definitions.h"
1
2
3
4
5
6
7
8
9
10
class ellipse : public closed_curve
{
public:
	ellipse(double a_holder, double b_holder, double x_holder, double y_holder); // constructor

	double major_radius, minor_radius;
	coordinate center;

	coordinate place(double);
};


"class_methods.cpp"
1
2
3
ellipse::ellipse(double a, double b, double x, double y) : major_radius(a), minor_radius(b), center(x,y)
{
}


I'm still getting the same error, though:

Undefined symbols:
"typeinfo for closed_curve", referenced from:
typeinfo for ellipsein class_methods.o
typeinfo for rectanglein class_methods.o
"vtable for closed_curve", referenced from:
__ZTV12closed_curve$non_lazy_ptr in class_methods.o
"closed_curve::place(double)", referenced from:
vtable for rectanglein class_methods.o
vtable for squarein class_methods.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Either make closed_curve::place() pure virtual, or provide a body for it.
That does it! (I decided to make it a pure virtual function.) Thank you so much!
Topic archived. No new replies allowed.