Thread: class member functions.

1. class member functions.

Hi,

I have this code that displays graphical data:
Code:
```Func::Func(Fct f, double r1, double r2, Point xy,
int cnt, double xscale, double yscale)
:count(cnt), range1(r1), origin(xy)

{
if(r2-range1<=0) error("bad graphing range");
if(count <=0) error("non-positive graphing count");
double dist = (r2-range1)/count;
double r = range1;
for(int i = 0; i<count; ++i) {
r+=dist;
}
}```
The Fct f, is a function that I pass in, such as square, factorial etc. It all works and the graphs are displayed using a simple graphics library used in the book.

A typical object is created as such:
Code:
`Func s1(square,-10,11, Point(300,300), 400, 25, 25);`
An exercise in the book asks me to create member functions to allow for parameter changes. For example, the Point(300, 300) above is the origin of the graph.

So I thought I would simple create a function as such:
Code:
```void Func::set_origin(Point& p)
{
origin = p;

}```
And, then when I re-attach the graph to the window, the changes would be made. But this is not the case. Any body got any ideas?

Thanks.

2. A lot of your work is done in the constructor of Fct. After construction, just setting the origin isn't going to change your collection of points. I would move most of the logic from your constructor into a private method and call that method from your setter functions, or move the logic to a public method and make sure you call that method after calling your setter functions.

3. Originally Posted by pianorain
A lot of your work is done in the constructor of Fct. After construction, just setting the origin isn't going to change your collection of points. I would move most of the logic from your constructor into a private method and call that method from your setter functions, or move the logic to a public method and make sure you call that method after calling your setter functions.
Will give that a try tomorrow when i'm back on it.

So, in essence, you advise to just use the constructor to initialise the variables and thenuse functions to draw the graph? Then when I want to make amendments I can simply call the relevant member function?

Thanks.

Popular pages Recent additions