Virtual base class, constructor wrong..

I have declared a virtual base to avoid to duplicated the data members of my base class...the trouble is that the constructor dont get what I expect....



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 
#include <iostream>

using namespace std;

template<typename T>
class complex{
	T r;
	T i;
public:
	complex<T>():r(0),i(0){};
	complex<T>(T a,T b):r(a),i(b){};
	
	
	
	complex<T> (const complex<T>& a){		//copy constructor
		r = a.r;
		i = a.i;
	}
	
	complex<T>& operator=(const complex<T> & a){ // copy assignament;
		
		r = a.r;
		i = a.i;
		return *this;
	}
	
	const T get_r() const{		//get real part
		T a = r;
		return a;
	}
	
	const T get_i() const{		//get imaginarium part
		T a = i;
		return a;
	}
	
};
	template<typename T>
	ostream& operator<<(ostream& os,const  complex<T>& a){
		os<<"Parte real "<<a.get_r()<<" "<<"Parte imaginaria "<<a.get_i()<<endl;
		return os;
	}
template<typename T,typename N>
class Ncomplex:virtual public complex<T>{
	N z ; 
	
public:

	Ncomplex<T,N>():complex<T>(),z(0){};
	Ncomplex<T,N>(T a,T b,N c):complex<T>(a,b),z(c){};
	
	
	const N get_z() const {
		N a = z;
		return a;
	}
	
	
};

template<typename T,typename N>
ostream& operator<<(ostream& os,const Ncomplex<T,N>& a){
	os<<" z: "<<a.get_z()<<" r: "<<a.get_r()<<" i: "<<a.get_i()<<endl;
	return os;
}



template<typename T,typename M>
class Mcomplex: virtual public complex<T>{
	
	M y;
public:
	Mcomplex<T,M>():complex<T>(),y(0){};
	Mcomplex<T,M>(T a,T b,M c):complex<T>(a,b),y(c){};
	
	
	
	const M get_y()const{
		M a;
		a = y;
		return a;
	}
};
template<typename T,typename M>
ostream& operator<<(ostream& os,const Mcomplex<T,M>& a){
	os<<" y: "<<a.get_y()<<" r: "<<a.get_r()<<" i: "<<a.get_i()<<endl;
	return os;
}


template<typename T,typename M,typename N>
class MNcomplex: public Mcomplex<T,M>,public Ncomplex<T,N>{
	public:
	MNcomplex<T,M,N>():Mcomplex<T,M>(),Ncomplex<T,N>(){};
	MNcomplex<T,M,N>(T a,T b,M c,N d):Mcomplex<T,M>(a,b,c),Ncomplex<T,N>(a,b,d){};

	
};

template<typename T,typename M,typename N>
ostream& operator<<(ostream& os,const MNcomplex<T,M,N>& a){
	os<<" z: "<<a.get_z()<<" y: "<<a.get_y()<<" r: "<<a.get_r()<<" i: "<<a.get_i()<<endl;
	return os;
}


int main(){

		MNcomplex<int,int,int> test(2,3,4,5);
		cout<<test;

	

}



I get this output z: 5 y: 4 r: 0 i: 0

when I expect z:5 y: 4 r:2 i: 3

I know it mustbe a trouble with the declaration of the constructor but I dont know how to do it..
thanks
My constructor base was not declared at the most derivated class...
Topic archived. No new replies allowed.