some class exercise

Hi,

Please read the exercise no.2 of this: http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA548#v=onepage&q&f=true
I've written below code for that. I know it's not complete but it's somewhat hard.

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
#include "Simple_window.h"  
#include "Graph.h"         
#include <iostream>

double curve(double x) {return x*x; }

//---------------------------------------

class FCT:public Shape {
public:
	FCT (Fct f, double _r1, double _r2, Point _xy, int _count = 100,
		 double _xscale = 25, double _yscale = 25): r1(_r1), r2(_r2),
		 xy(_xy), count(_count), xscale(_xscale), yscale(_yscale) 
	{
		if(r2-r1 <= 0) error("Bad graphing range");
		if(count <= 0) error("Non-positive graphing count");
		double dist = (r2-r1)/count;
		double r = r1;
		for(int i=0; i<count; i++) {
			add(Point(xy.x+int(r*xscale), xy.y-int(f(r)*_yscale)));
			r += dist;
		}
	}

		void reset_orig(Point _xy)   { xy = _xy;  }
		void reset_count(int c)      { count = c; } 
		void reset_r1(double _r1)    { r1 = _r1;  }
		void reset_r2(double _r2)    { r2 = _r2;  }
		void reset_xscale(double xs) { xscale = xs; }
		void reset_yscale(double ys) { yscale = ys; }

private:
	int count;
	double r1, r2, xscale, yscale;
	Point xy;
};

//-----------------------------------------------

int main()
	try
{
	Simple_window win(Point(100,100), 600, 400, "test");
	FCT g(curve, -10, 10, Point(200,200), 100, 25, 25);
	Text t(Point(200,200),"X");
	g.reset_orig(Point(300,300));


	win.attach(t);
	win.attach(g);
	g.reset_orig(Point(300,300));
	win.attach(g);
	win.wait_for_button();
	return 0;
}
catch(exception& e) {
	// some error reporting
	return 1;
}
catch(...) {
	// some more error reporting
	return 2;
}

//------------------------------------------------------------------------------ 


I don't know could I understand the exercise correctly or not. If possible please guide me to do that exercise.

PS: I DON'T WANT THE CODE. Just guide me please.
No reply!?
> Please read the exercise no.2
Define a class Fct that is just like Function...
¿what's Function?

> class FCT:public Shape
Please explain your decision.


I see that you have provided a constructor and setters, ¿but how do you use your class?
The definition of the Function is right here (15.3):
http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA524#v=onepage&q&f=true

I defined a class named FCT (rather than Fct, I'll say its reason latter :)). Now the class is defined and is just like the class Function.

Look at line 44 please. I've used my class there.
> (rather than Fct, I'll say its reason latter :))
I mean, explain why is `FCT' inheriting from `Shape'
(but well, I suppose that it is because `Function' inherits from `Shape')

> Look at line 44 please. I've used my class there.
You have constructed an object there, ¿is that the only interesting operation that it has?
If the constructor is drawing the curve on the screen, perhaps your "setters" should do that too (modified according)
> You have constructed an object there, ¿is that the only interesting operation that it has?
I don't know what do you mean by that. If your meaning is the operations, OK, I can use of that object to call the setter functions.

The work that should be done, IMO, is when I call some function of those setters, it must somehow 1- recall the constructor of the class (FCT) to redraw the curve this time with changed data or 2- Disable the drawing (here the Points added by add()) of the FCT constructor and draws the curve by its data.

This is the class Shape:
http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA494#v=onepage&q&f=true

An this is an incomplete code for (2-):

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
#include "Simple_window.h"  
#include "Graph.h"         
#include <iostream>

double curve(double x) {return x*x; }

//---------------------------------------

class FCT:public Shape {
public:
	FCT (Fct _f, double _r1, double _r2, Point _xy, int _count = 100,
		double _xscale = 25, double _yscale = 25): f(_f), r1(_r1), r2(_r2),
		xy(_xy), count(_count), xscale(_xscale), yscale(_yscale) 
	{
		if(r2-r1 <= 0) error("Bad graphing range");
		if(count <= 0) error("Non-positive graphing count");
		dist = (r2-r1)/count;
		r = r1;
		for(int i=0; i<count; i++) {
			add(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale)));
			r += dist;
		}
	}

	void reset_fct(Fct _f)       { reset(_f); }
	void reset_orig(Point _xy)   { xy = _xy;    reset(); }
	void reset_count(int c)      { count = c;   reset(); } 
	void reset_r1(double _r1)    { r1 = _r1;    reset(); }
	void reset_r2(double _r2)    { r2 = _r2;    reset(); }
	void reset_xscale(double xs) { xscale = xs; reset(); }
	void reset_yscale(double ys) { yscale = ys; reset(); }
	void reset(Fct f = 0) {
		for(int i=0; i<count; i++) {
			set_point(i,(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale))));
			r += dist;
		}
	}


private:
	Fct f;
	int count;
	double r, dist, r1, r2, xscale, yscale;
	Point xy;
};

//-----------------------------------------------

int main()
	try
{
	Simple_window win(Point(100,100), 600, 400, "test");
	FCT g(curve, -10, 10, Point(200,200), 100, 25, 25);
	g.reset_orig(Point(300,300));
	win.attach(g);
	win.wait_for_button();
	return 0;
}
catch(exception& e) {
	// some error reporting
	return 1;
}
catch(...) {
	// some more error reporting
	return 2;
}

//------------------------------------------------------------------------------ 


The problem of this is in line 12, when I wanted to replace the f with _f.
Last edited on
Still waiting for someone to help me !:(
Topic archived. No new replies allowed.