Classes and sub-Classes

Hi!
I have a problem.
I have this class and two sub-classes, something like 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
40
41
42
43
44
45
46
class object
{
public:
	VecH *pos;
	float rgb[3];
	float kd;
	float reflect;
	float refract;
	
	object();	
	
	void objectCreate(RTfloat xx,RTfloat yy,RTfloat zz,RTfloat rr,short int g,short int b,float k,float j){
		pos = new VecH(xx,yy,zz,1.0f);
		rgb[0]=(float)r;
		rgb[1]=(float)g;
		rgb[2]=(float)b;
		reflect=k;
		refract=j;
	}
  
        (...)
	  
	virtual ~object();
};

class plane:object
{
public:
	VecH *normal;
	float ladoa;
	float ladob;
	
	plane();

        (...)  
};

class sphere:object
{
public:
        float raio;
	
	sphere();

        (...)  
};


With that structure I need to create an array of "objects" that both contain "planes" and "spheres".
I tried that like this:

//initialized an array of "objects"
object sceneObject[TOTAL_NUMBER_OF_OBJECTS];

//with a for loop, created the real objects in each position of the array
//this objects could be "planes" or "spheres"
1
2
3
4
5
6
for(int i=0;i<TOTAL_NUMBER_OF_OBJECTS;i++){
        if(i<NUMBER_OF_PLANES)
	  sceneObject[i] = new plane();
        else
          sceneObject[i] = new sphere();
}


The intention is clear, but the compiler does not allow it.

Can anyone give some ideas to make this work?

Thank you
closed account (z05DSL3A)
I have only given it a quick look, but I would say your array of object needs to be an array of pointers to objects.

HTH
Last edited on
Why don't you make an array of the class object and create a sphere and a plane in each object?

something along:

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

class cPlane
{ // whatever you need
};

class cSphere
{
int MyVariable;
// whatever else you need
};

class cObject
{ cPlane MyPlane;
   cSphere MySphere;

//  whetever else you need
};

int main()
{
   cObject MyObjects[99];

// whatever  you need

}


Now you can access the values (after declaring the functions) like

1
2
MyObjects[6].MySphere.MyVariable

Does that solve your problem?

int main

P.S.: Since I'm quite new I would like to know if this solution is a common approach or more like an 'arghwhatdidyoudonow!'-thing


Last edited on
closed account (z05DSL3A)
int main,

The releationship between the classes is totaly different. The OPs code has an is-a[1] relationship between the classes.

If you are modelling a car park (car lot?) you have some classes this like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Car
{
…
}

class SportCar: Car
{
…
}

class Suv: Car
{
…
}


Then for your parking spaces you would have an array that would hold one car in each ‘slot’. Something like Car* spaces[100];. So now each slot can have a car, it may be a Suv as that is-a Car, or it may be a SportCar as that is-a Car.

In your code you have an has-a[2] relationship between classes.

If you map the classes above back to your code, you would have a Car the is made up of an Suv and a SprotCar, which would not make sense. The OP want an Object that is either a plane or a sphere not both a plane and a sphere.

HTH

[1] http://en.wikipedia.org/wiki/Is-a
[2] http://en.wikipedia.org/wiki/Has-a
Last edited on
Short version for tsimmi:
If you want to create an array, which can hold objects of differnt classes(spheres, planes etc.) of the same "family" (geometric objects e.g.) you need a common base class, like Car in Grey Wolf's example.

Then your array can be of type "pointer to your base class" but actually it can hold pointers to your derived classes. You find a lot of info about this topic if you search for keywords "(virtual) base class", "down cast" or "dynamic type casting".

Or show us your reworked version...
Hi again and thanks for all the help.
The structure that I have now is something like 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
class object
{
public:
	VecH *pos;
	float rgb[3];
	float kd;
	float reflect;
	float refract;
	
	virtual void oCreate()=0;
	
	virtual ~object();
};


class sphere:object{
public:
    RTfloat raio;
	
	sphere();
	
	void oCreate(...){...}
	
	virtual ~sphere();
};

class plane:object{
public:
	VecH *normal;
	RTfloat ladoa;
	RTfloat ladob;
	
	plane();
	
	void oCreate(...){...}

virtual ~plane();
};


Then in the main I initialize the "object" array like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
object *elemento[NO];
sphere *esfera[NE];
plane *plano[NP];

int main(int argc, char *argv[]){
  	int fd;

	esfera[0]->oCreate(...);
        ...
  	esfera[7]->oCreate(...);
	
	plano[0]->oCreate(...);
        ...
	plano[3]->oCreate(...);
	
	//initializing the scene objects to the array
	for(int i=0;i<NO;i++){
		if(i<NE)
	  		elemento[i] = esfera[i];         <--
                else
                        elemento[i] = plano[i];          <--
	}


With this configuration I get two errors(represented with above arrows), and they are:

error: ‘object’ is an inaccessible base of ‘sphere’ error: ‘object’ is an inaccessible base of ‘plane’

What can I do from here?

Thank you all!
closed account (z05DSL3A)
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
class object
{
public:
	VecH *pos;
	float rgb[3];
	float kd;
	float reflect;
	float refract;
	
	virtual void oCreate()=0;
	
	virtual ~object();
};


class sphere:  public  object  //use public inheritance
{ 
public:
    RTfloat raio;
	
	sphere();
	
	void oCreate(...){...}
	
	virtual ~sphere();
};

class plane: public object  //use public inheritance
{
public:
	VecH *normal;
	RTfloat ladoa;
	RTfloat ladob;
	
	plane();
	
	void oCreate(...){...}

virtual ~plane();
};
OK!
Things are moving again!

But I still have another little problem with that structure.
When I do:
normal->x=elemento[k]->raio;
I get this error:
error: ‘class object’ has no member named ‘raio’

I understand that I cannot access this field in both "planes" and "spheres",represented by elemento[k], because only "spheres" have this field.

How can I solve this one?

Thank you guys!
closed account (z05DSL3A)
I'm a bit pushed for time but this may help you:
http://en.wikipedia.org/wiki/Dynamic_cast
Sorry for the insistence.

Now I have this:
1
2
3
4
5
6
sphere* intObject = dynamic_cast<sphere*>(elemento);

	if (intObject != NULL)
		raio = intObject->getRadius();
	else
		std::cout << "This object is not of type B" << std::endl;


It seemed that it was going to work but now I get this:
undefined reference to `typeinfo for sphere'

I have the rigth includes so, what may it be this time?
Thanks

[[[[[[[[[[[[[[[[[[UPDATE]]]]]]]]]]]]]]]]]]]]]]

I have found why it gives that error and it is an obscure one. I get it from here:
http://www.wellho.net/mouth/802_undefined-reference-to-typeinfo-C-error-message.html?pwidth=wide

It seems that when I define the classes and subClasses, there can't be any method with an empty body, like:
virtual ~sphere();
It has to be like this:
virtual ~sphere(){};

I think that with this I can go forward with my work.

Thank you all very much!
Last edited on
Topic archived. No new replies allowed.