Question about inheritance

I need class "Vector4D" to initialize all the variables,I have to create a default and a generic constructor in Vector4D to initialize all variables in all three classes regardless of the constructor call. Where am i going wrong.(The Comments are failed attempts)
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
  #include<iostream>
using namespace std;
class Vector2D
{
	protected:
	int x,y;
	public:
		Vector2D() {x=y=0;}
	//	Vector2D(int x1, int y1){x = x1; y = y1;}
	
};
class Vector3D
{
	protected:
		int z;
	public:
		Vecotr3D() {z=0;}
	//	Vector3D(int z1) {z = z1;}
	
};
class Vector4D: public Vector2D, public Vector3D
{
	protected:
		int t;
	public:
		Vector3D v3;
		Vector2D v2;
		Vector4D(){t=0;}
		Vector4D(int,int,int,int);
};
Vector4D::Vector4D(int a,int b, int c, int d)
{
//	Vector3D::Vector3D() ;
//	Vector2D::Vector2D();
//	x=a;
//	y=b;
//	z=c;

v2.x = a;
v2.y = b;
v3.z = c;
t=d;

	cout << x  << endl<<y <<endl << z  << endl<< t;
}
int main ()
{
	Vector4D x,y(1,2,3,20);
	x;
	y;
	return 0;
}	
Look up "member initializer list". Plenty of goog links.

Also http://www.informit.com/articles/article.aspx?p=1852519


The logic of your design is ... odd.

The Vector2D does make sense; it holds two values.

The Vector3D ... is one integer?

The Vector4D:
* is-a Vector2D
* is-a Vector3D
* has-a Vector2D object
* has-a Vector3D object
* has-a integer object

... sums up to 7 integers?
The reason the design is odd is due to my failed attempts,and just moving things around to see what works and what doesn't.
I can get it to work, but apparently that is not the way my professor wants it.
My real question is how do I intialize the variables from Vector2D and Vector3D from Vector4D
without initializing them directly in Vector4D such as:
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
class Vector2D
{
	protected:
	int x,y;
	public:
		Vector2D() {x=y=0;}
	
};
class Vector3D
{
	protected:
		int z;
	public:
		Vecotr3D() {z=0;}
	
};
class Vector4D: public Vector2D, public Vector3D
{
	protected:
		int t;
	public:
		
		Vector4D(){t=0;}
		Vector4D(int,int,int,int);
};
Vector4D::Vector4D(int a,int b, int c, int d)
{

	x=a;
	y=b;
	z=c;
	t=d;

	cout << x  << endl<<y <<endl << z  << endl<< t;
}

This is how I had it originally which is incorrect.
First off, I would say you inheritance is wrong.
Vector3D should inherit Vector2D.
Vector4D should inherit only from Vector3D.

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 Vector2D
{   int x,y;
public:
	Vector2D () : x(0), y(0)
	{}
	Vector2D (int _x, int _y) : x(_x), y(_y)   //  2 arg constuctor
	{}
};

class Vector3D : public Vector2D
{   int z;
public:
	Vector3D() : z(0)   //  Invokes Vector2D's default constructor
	{}
	Vector3D(int x,int y,int _z) : Vector2D(x,y), z(_z)  //  3 arg constructor
	{}
};

class Vector4D: public Vector3D
{   int t;
public:
	Vector4D() : t(0)       //  Invokes Vector3D's default constructor
	{}
	Vector4D(int x,int y,int z,int _t) : Vector3D(x,y,z), t(_t)  // 4 arg constructor
	{}
};

@AbstractionAnon follow up question.
1
2
3
4
5
6
public:
	Vector2D () : x(0), y(0) // <----------------
	{}
	Vector2D (int _x, int _y) : x(_x), y(_y)   //  2 arg constuctor
	{}
};

Why do you initialize this way? Was it part of my issue?
It's called initializer list member initialization. It's generally preferred over the following form which does the same thing:
1
2
3
4
	Vector2D () 
	{ x=0; y=0; }
	Vector2D (int _x, int _y) //  2 arg constuctor
	{ x=_x; y=_y; }


Thank you very much for your help.
You saved my life. I had the same problem for school project!
Topic archived. No new replies allowed.