optimization program:parser

Hi guys,

I have done a program with a hierarchy of 4 levels, and the base is virtual.I have overload operator<< and >> for each class...and I have created a class Vector where I have as a data member and int (it's supossed to be the size of the vector) and a pointer to the most derivated class...Vector class has as well operator << and >> suited for it....

My question is.....in operator<< and >>I have had to define everything and I haven't been able to use the operator<< and >> that I used for the other class, I think that could have done in somehow, instead of coding such a long functions...

and as well I would like to create a "parser" which was able to detect how many MNcomplex I have written in cin and detect their formats and allow or not to get in to the Vector(which is in fact a Vector of MNcomples)....I know this one question is a bit long...just I would like to get some idea or clue or something to work with...

thanks so much I leave my code below..
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#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):r(a),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;
	}
	
	void push_r(T aux){
		r = aux;
	}
	
	void push_i(T aux){
		i = aux;
	}
	
};
template<typename T>
ostream& operator<<(ostream& os,const  complex<T>& a){
		os<<"Real part "<<a.get_r()<<" "<<"Imaginarium Part "<<a.get_i()<<endl;
		return os;
	}

template<typename T>
istream& operator>>(istream& is,complex<T>& a){
	T aux;
	cout<<"Enter real part"<<endl;
	cin>>aux;
	a.push_r(aux);
	cout<<"Enter imaginarium part"<<endl;
	cin>>aux;
	a.push_i(aux);
	return is;
	
}




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,N b):complex<T>(a),z(b){};
	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;
	}
	
	void push_z(N aux){
		z = aux;
	}
	
	
};

template<typename T,typename N>
istream& operator>>(istream& is,Ncomplex<T,N>& a){
	T aux;
	cout<<"Enter real part"<<endl;
	cin>>aux;
	a.push_r(aux);
	cout<<"Enter imaginarium part"<<endl;
	cin>>aux;
	a.push_i(aux);
	N auxn;
	cout<<"Enter z part"<<endl;
	cin>>auxn;
	a.push_z(auxn);
	return is;
}

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,M b):complex<T>(a),y(b){};
	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;
	}
	
	void push_y(M aux){
		y = aux;
	}
	
	
};

template<typename T,typename M>
istream& operator>>(istream& is,Mcomplex<T,M>& a){
	T aux;
	cout<<"Enter real part"<<endl;
	cin>>aux;
	a.push_r(aux);
	cout<<"Enter imaginarium part"<<endl;
	cin>>aux;
	a.push_i(aux);
	M auxm;
	cout<<"Enter z part"<<endl;
	cin>>auxm;
	a.push_y(auxm);
	return is;
}

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,M b,N c):complex<T>(a),Mcomplex<T,M>(a,b),Ncomplex<T,N>(a,c){};
	MNcomplex<T,M,N>(T a,T b,M c,N d):complex<T>(a,b),Mcomplex<T,M>(a,b,c),Ncomplex<T,N>(a,b,d){};

	
};


template<typename T,typename M,typename N>
istream& operator>>(istream& is,MNcomplex<T,M,N>& a){
	T aux;
	cout<<"Enter real part"<<endl;
	cin>>aux;
	a.push_r(aux);
	cout<<"Enter imaginarium part"<<endl;
	cin>>aux;
	a.push_i(aux);
	M auxm;
	cout<<"Enter y part"<<endl;
	cin>>auxm;
	a.push_y(auxm);
	N auxn;
	cout<<"Enter z part:"<<endl;
	cin>>auxn;
	a.push_z(auxn);
	cin.ignore();
	return is;
}
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;
}
template<typename T,typename M,typename N>
class Vector{
	MNcomplex<T,M,N> *elem {};
	int sz {};
public:
	
	const int get_size() const{
		int a;
		a = sz;
		return a;
	}
	
	MNcomplex<T,M,N>* get_elem() {
		MNcomplex<T,M,N>* a = elem;
		return a;
	} 

	
	Vector(int a):elem ( new MNcomplex<T,M,N>[a]),sz(a){
		
		for(int i= 0;i !=sz;++i){
			MNcomplex<T,M,N> cero;
			elem[i] = cero;
		}
		
		};
	
	
	~Vector(){delete[]elem;}
	
	int get_sz() const{
		int a = sz;
		return a;
	}


};
template<typename T,typename M,typename N>
istream& operator>>(istream& is,Vector<T,M,N>& a){
	cout<<"You'll enter "<<a.get_size()<<" MNcomplex numbers "<<endl;
	for(int i = 0;i != a.get_size();++i){
		MNcomplex<T,M,N> aux;
		cin>>aux;
		MNcomplex<T,M,N>* paux ;
		paux = a.get_elem();
		paux[i] = aux;
	}
	return is;
}

template<typename T,typename M,typename N>
ostream& operator<<(ostream& os,Vector<T,M,N>& a){
	cout<<"You have entered "<<a.get_size()<<" MNcomplex numbers"<<endl;
	
		MNcomplex<T,M,N> aux;
		MNcomplex<T,M,N>* paux ;
		paux = a.get_elem();
	for(int i = 0;i != a.get_size();++i){	
		aux = *paux;
		cout<<aux;
		++paux;
	}	
	return os;
}

/*
template<typename T,typename M,typename N>
 
 MNcomplex<T,M,N>& operator>>(istream& is,Vector<T,M,N>& cont){
	cout<<"You can enter this amount "<<cont.get_sz()<<"of NMcomplex"<<endl;
	cout<<"You must write the NMcomplex with this format"<<endl;
	cout<<"{1,2,3,4}"<<endl;
	MNcomplex<T,M,N> trans;
	T aux,a,b;
	M auxM,c;
	N auxN,d;
	int count = cont.get_sz()-1;
	while(count>0){
	if(is>>aux&&aux=='{')
		
		while(is.get(aux)&&aux!=','){
			a = aux;
			
		}	
	if(is>>aux&&aux==',')
		while(is.get(aux)&&aux!=','){
			b = aux;
		}
	if(is>>auxM&&auxM==',')
		while(is.get(auxM)&& auxM !=','){
			c = auxM;
		}
	if(is>>auxN&&auxN==',')
		while(is.get(auxN)&& auxN !='}'){
			d = auxN;
		}
	
	count--;
	}
	return trans(a,b,c,d);
 }
*/

int main(){
	Vector<char,char,char> cube(3);
	cin>>cube;
	cout<<cube;
}


Topic archived. No new replies allowed.