DESPERATE

How would I declare the print functions for this mess?
Sorry for the length, professor is weird and likes one file
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <iostream>
using std::cout;
using std::endl;
using std::ios;

#include <iomanip>
using std::setprecision;

#include <fstream>
using std::ifstream;

#include <cstring>
#include <cstdlib>

class Square
{
public:
	Square(char*);
	void print(Square*);
	void setSquare(Square*, double);
private:
	double side;
};

Square::Square(char* token)
{
	if (atof(token) == 0)
		side = 0;
	else
		side = atof(token);
}

class Rectangle
{
public:
	Rectangle(char*, char*);
	void print(Rectangle*);
	void setRectangle(Rectangle*, double, double);
private:
	double length;
	double width;
};

Rectangle::Rectangle(char* tokena, char* tokenb)
{
	if (atof(tokena) == 0)
		length = 0;
	else
		length = atof(tokena);
	if (atof(tokenb) == 0)
		width = 0;
	else
		width = atof(tokenb);
}

class Circle
{
public:
	Circle(char*);
	void print(Circle*);
	void setCircle(Circle*, double);
private:
	double radius;
};

Circle::Circle(char* token)
{
	if (atof(token) == 0)
		radius = 0;
	else
		radius = atof(token);
}

class Cylinder
{
public:
	Cylinder(char*, char*);
	void print(Cylinder*);
	void setCylinder(Cylinder*, double, double);
private:
	double radius;
	double height;
};

Cylinder::Cylinder(char* tokena, char* tokenb)
{
	if (atof(tokena) == 0)
		radius = 0;
	else
		radius = atof(tokena);
	if (atof(tokenb) == 0)
		height = 0;
	else
		height = atof(tokenb);
}

class Cube
{
public:
	Cube(char*);
	void print(Cube*);
	void setCube(Cube*, double);
private:
	double side;
};

Cube::Cube(char* token)
{
	if (atof(token) == 0)
		side = 0;
	else
		side = atof(token);
}

class Prism
{
public:
	Prism(char*, char*, char*);
	void print(Prism*);
	void setPrism(Prism*, double, double, double);
private:
	double length;
	double width;
	double height;
};

Prism::Prism(char* tokena, char* tokenb, char* tokenc)
{
	if (atof(tokena) == 0)
		length = 0;
	else
		length = atof(tokena);
	if (atof(tokenb) == 0)
		width = 0;
	else
		width = atof(tokenb);
	if (atof(tokenc) == 0)
		height = 0;
	else
		height = atof(tokenc);
}

const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* DELIMITER = " ";
const double PI = 3.14159265358979323846;

void Square::print(Square* sq)
{
	cout << "SQUARE" << " side=";
	cout.unsetf(ios::fixed|ios::showpoint);
	cout << setprecision(6);
	cout << sq->side << " area=";
	cout.setf(ios::fixed|ios::showpoint);
	cout << setprecision(2);
	cout << (sq->side * sq->side) << " perimeter=" << (4*sq->side) << endl;
}

void Rectangle::print(Rectangle* re)
{
	cout << "RECTANGLE" << " length=";
	cout.unsetf(ios::fixed|ios::showpoint);
	cout << setprecision(6);
	cout << re->length << " width=" << re->width << " area=";
	cout.setf(ios::fixed|ios::showpoint);
	cout << setprecision(2);
	cout << (re->length * re->width) << " perimeter=" << ((2*re->length)+(2*re->width)) << endl;
}

void Circle::print(Circle* ci)
{
	cout << "CIRCLE" << " radius=";
	cout.unsetf(ios::fixed|ios::showpoint);
	cout << setprecision(6);
	cout << ci->radius << " area=";
	cout.setf(ios::fixed|ios::showpoint);
	cout << setprecision(2);
	cout << (PI*(ci->radius * ci->radius)) << " circumference=" << (2*PI*ci->radius) << endl;
}

void Cube::print(Cube* cu)
{
	cout << "CUBE" << " side=";
	cout.unsetf(ios::fixed|ios::showpoint);
	cout << setprecision(6);
	cout << cu->side << " surface area=";
	cout.setf(ios::fixed|ios::showpoint);
	cout << setprecision(2);
	cout << ((cu->side*cu->side)*6) << " volume=" << (cu->side*cu->side*cu->side) << endl;
}

void Prism::print(Prism* pr)
{
	cout << "PRISM" << " length=";
	cout.unsetf(ios::fixed|ios::showpoint);
	cout << setprecision(6);
	cout << pr->length << " width=" << pr->width << " height=" << pr->height << " surface area=";
	cout.setf(ios::fixed|ios::showpoint);
	cout << setprecision(2);
	cout << ((2*pr->length*pr->width)+(2*pr->width*pr->height)+(2*pr->length*pr->height));
	cout << " volume=" << (pr->length*pr->width*pr->height) << endl;
}

void Cylinder::print(Cylinder* cy)
{
	cout << "CYLINDER" << " radius=";
	cout.unsetf(ios::fixed|ios::showpoint);
	cout << setprecision(6);
	cout << cy->radius << " height=" << cy->height << " surface area=";
	cout.setf(ios::fixed|ios::showpoint);
	cout << setprecision(2);
	cout << ((2*PI*(cy->radius*cy->radius)) + (2*PI*cy->radius*cy->height));
	cout << " volume=" << (PI*(cy->radius*cy->radius)*cy->height) << endl;
}
void Square::setSquare(Square* sq, double tempa)
{
	sq->side = tempa;
}

void Rectangle::setRectangle(Rectangle* re, double tempa, double tempb)
{
	re->length = tempa;
	re->width = tempb;
}

void Circle::setCircle(Circle* ci, double tempa)
{
	ci->radius = tempa;
}

void Cylinder::setCylinder(Cylinder* cy, double tempa, double tempb)
{
	cy->radius = tempa;
	cy->height = tempb;
}

void Cube::setCube(Cube* cu, double tempa)
{
	cu->side = tempa;
}

void Prism::setPrism(Prism* pr, double tempa, double tempb, double tempc)
{
	pr->length = tempa;
	pr->width = tempb;
	pr->height = tempc;
}

int main()
{
	ifstream fin;
	int x = 0;
	void* shapes[100] = {};
	int id[100] = {}; // 0=square, 1=rectangle, 2=circle, 3=cube, 4=prism, 5=cylinder
	fin.open("geo.txt");
	if (!fin.good())
		return 1;
	while (!fin.eof())
	{
		char buff[MAX_CHARS_PER_LINE];
		fin.getline(buff, MAX_CHARS_PER_LINE);
		int n = 0;
		char* token[MAX_TOKENS_PER_LINE] = {0};
		token[0] = strtok(buff, DELIMITER);
		if (token[0])
		{
			for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
			{
				token[n] = strtok(0, DELIMITER);
				if (!token[n]) break;
			}
		}
		for (int i = 0; i < n; i++)
		{
			if (!((strcmp(token[0],"SQUARE") == 0) || (strcmp(token[0],"RECTANGLE") == 0) || (strcmp(token[0],"CUBE") == 0) || (strcmp(token[0],"CIRCLE") == 0) || (strcmp(token[0],"PRISM") == 0) || (strcmp(token[0],"CYLINDER") == 0)))
			{
				cout << token[0] << " invalid object" << endl;
				i=n;
			}
			if(strcmp(token[0],"SQUARE") == 0)
			{
				if (n == 1)
				{
					token[1] = "0";
				}
				Square* sq = new Square(token[1]);
				shapes[x] = sq;
				id[x] = 0;
				i=n;
				x++;
			}
			if(strcmp(token[0],"RECTANGLE") == 0)
			{
				if (n == 1)
				{
					token[1] = "0";
					token[2] = "0";
				}
				if (n == 2)
				{
					token[2] = "0";
				}
				Rectangle* re = new Rectangle(token[1], token[2]);
				shapes[x] = re;
				id[x] = 1;
				i=n;
				x++;
			}
			if(strcmp(token[0],"CIRCLE") == 0)
			{
				if (n == 1)
				{
					token[1] = "0";
				}
				Circle* ci = new Circle(token[1]);
				shapes[x] = ci;
				id[x] = 2;
				i=n;
				x++;
			}
			if(strcmp(token[0],"CUBE") == 0)
			{
				if (n == 1)
				{
					token[1] = "0";
				}
				Cube* cu = new Cube(token[1]);
				shapes[x] = cu;
				id[x] = 3;
				i=n;
				x++;
			}
			if(strcmp(token[0],"PRISM") == 0)
			{if (n == 1)
				{
					token[1] = "0";
					token[2] = "0";
					token[3] = "0";
				}
				if (n == 2)
				{
					token[2] = "0";
					token[3] = "0";
				}
				if (n == 3)
				{
					token[3] = "0";
				}
				Prism* pr = new Prism(token[1], token[2], token[3]);
				shapes[x] = pr;
				id[x] = 4;
				i=n;
				x++;
			}
			if(strcmp(token[0],"CYLINDER") == 0)
			{
				if (n == 1)
				{
					token[1] = "0";
					token[2] = "0";
				}
				if (n == 2)
				{
					token[2] = "0";
				}
				Cylinder* cy = new Cylinder(token[1], token[2]);
				shapes[x] = cy;
				id[x] = 5;
				i=n;
				x++;
			}
		}	
	}
	fin.close();
	for (int c = 0; c < x; c++)
	{	
		//Print Functions Go Here
	}
	for (int d = 0; d < x; d++)
	{
		if (id[d] == 0)
			delete (Square*)shapes[d];
		if (id[d] == 1)
			delete (Rectangle*)shapes[d];
		if (id[d] == 2)
			delete (Circle*)shapes[d];
		if (id[d] == 3)
			delete (Cube*)shapes[d];
		if (id[d] == 4)
			delete (Prism*)shapes[d];
		if (id[d] == 5)
			delete (Cylinder*)shapes[d];
	}
	return 0;
}
Last edited on
Hello? Did you not read the rules? This forum is NOT for HOMEWORK! You can ask questions or ask for help in some certain things but you are NOT allowed to ask for a working HOMEWORK.
i am sorry, i have been sick the past week and am kinda stuck and not thinking straight
What's exactly your prob? Don't you know how to use these print(whatever)-functions in line 377?
this is what was there before we changed the classes from structs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int c = 0; c < x; c++)
	{
		if (id[c] == 0)
			printSquare((square*)shapes[c]);
		if (id[c] == 1)
			printRectangle((rectangle*)shapes[c]);
		if (id[c] == 2)
			printCircle((circle*)shapes[c]);
		if (id[c] == 3)
			printCube((cube*)shapes[c]);
		if (id[c] == 4)
			printPrism((prism*)shapes[c]);
		if (id[c] == 5)
			printCylinder((cylinder*)shapes[c]);
	}

but now with classes and the public and private tags i am a little lost and this professor is the only comp sci teacher that doesn't post his lectures online
these
[...]public and private tags[...]
, how you name them, are for the access. You can't use as private declared functions or variables outside the class. The opposite of this is public. E.g. in your class Rectangle (l. 33-42) you can access the constructor (Rectangle(/*whatever*/)) from outside the class, but you can't do so with length or width.
I think that you don't have to change anything, because in a
struct
you only have public variables but in a class is everything which is not specified as public or private automatically protected. I guess that protected variables can be used from outside the class, but they're like static ones: You can't alter them.
i have actually tried that and he doesn't want us to use static and he does want public and private tags.
it is not static, it's protected. But if he doesn't want you to use protected variables, you have to make public functions to return the variable value. E.g.
double get_width() {return width;};
You might use this in Rectangle to get the width of this rectangle.
btt: print() is a class function, isn't it? So you have to make for example
1
2
3
4
5
if(id[c]==1)
{
    Rectangle *r=(rectangle *)shapes[c];
    r->print(r);
}
The main syntactic differences between standalone functions and functions in a class are the calling convention, and the way you access data in the object.

e.g. in the world of pure procedural code, you'd do something like this
1
2
3
4
5
6
7
8
9
struct foo
{
    int n;
};

void func( foo& bar )
{
    bar.n = 5;
} 

and to call the function, you'd do this
1
2
3
4
5
int main()
{
    foo bar;
    func( bar );
} 



In the world of object-based programming, the above code would look a bit different
1
2
3
4
5
6
7
8
9
class foo
{
    int n;
public:
    void func()
    {
        n = 5;
    }
}; 

and to call the function, you'd do this
1
2
3
4
5
int main()
{
    foo bar;
    bar.func();
} 

Notice that 'func()' no longer accepts a 'foo' argument - because it doesn't need one; it always operates on its current object (As in, whatever is on the left-hand-side of the . or ->). The code is functionally identical, but the organisation of the program is completely different.


If this is your first foray into the world of object-based programming, then I'd strongly suggest looking in your book (or on google) for some basic information about classes.
Last edited on
FlashDrive wrote:
I think that you don't have to change anything, because in a struct
you only have public variables but in a class is everything which is not specified as public or private automatically protected. I guess that protected variables can be used from outside the class, but they're like static ones: You can't alter them.

You can declare members and methods as public private protected in a struct too.
The default visibillity for a struct is public
for a class is private

protected is using with inheritance in order to give the children access, while keep them secure from outsiders
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
class foo{
//private by default
  int a;
private:
  int b;
protected:
  int c;
public:
  int d;
};

class bar: public foo{
public:
  void method(){
    a = 42; //error foo::a is private
    b = 42; //error foo::b is private
    c = 42; //fine
    d = 42; //fine
  }
};

int main(){
  bar zzz;
  zzz.c = 42; //error foo::c is protected
}


@OP: void Square::print(Square* sq) That make no sense.
As pointed by Bench82 methods operates on its object.
So the prototype should be void Square::print() const (const because the object will not be modified)
The same goes for your settters.


About your code, yuck at your heterogeneous container. Take a look at polymorphism
(or at least abstract it a little)
Topic archived. No new replies allowed.