Thread: class member functions.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    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) {
    		add(Point(origin.x+int(r*xscale), origin.y-int(f(r)*yscale)));
    		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. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by pianorain View Post
    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 subscribe to a feed

Similar Threads

  1. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM