CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2014
    Posts
    72

    some class exercise

    Hi,

    Please read the exercise no.2 of this: http://books.google.com/books?id=We2...epage&q&f=true

    I've written below code for that. I know this code is not complete but this exercise is somewhat hard.

    Code:
    #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;
    }
    
    //------------------------------------------------------------------------------
    The definition of the Function is right here (15.3):

    http://books.google.com/books?id=We2...epage&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.

    I don't know could I understand the exercise correctly or not.
    I DON'T WANT THE CODE. Just guide me how to solve it please.
    Last edited by abbassi; December 9th, 2014 at 04:43 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: some class exercise

    What exactly is the problem that you're facing?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2014
    Posts
    72

    Re: some class exercise

    I reviewed and edited the question.
    I hope it is fine now

  4. #4
    Join Date
    May 2014
    Posts
    72

    Unhappy Re: some class exercise

    This time I wrote the following code for the exercise:

    Code:
    #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;
    }
    
    //------------------------------------------------------------------------------
    I get some errors:
    1- Error 8 error C2436: 'f' : member function or nested class in constructor initializer list c:\users\cs\documents\visual studio 2012\projects\test_3\test_3\test_3.cpp 15

    2- 9 IntelliSense: "f" is not a nonstatic data member or base class of class "FCT" c:\Users\CS\Documents\Visual Studio 2012\Projects\test_3\test_3\test_3.cpp 13


    What is the type of the Fct, as the book says:

    What is the type Fct that we used to represent a function argument? It is a variant of a standard library type called std::function that can "remember" a function to be called later. Fct requires its argument to be a double and its return type to be a double.

    And the definition of class Shape is also here:
    http://books.google.com/books?id=We2...epage&q&f=true

    No help!?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: some class exercise

    Quote Originally Posted by abbassi View Post
    What is the type of the Fct, as the book says:

    What is the type Fct that we used to represent a function argument? It is a variant of a standard library type called std::function that can "remember" a function to be called later. Fct requires its argument to be a double and its return type to be a double.

    And the definition of class Shape is also here:
    http://books.google.com/books?id=We2...epage&q&f=true

    No help!?
    And where in your code this Fct is declared/defined?
    Victor Nijegorodov

  6. #6
    Join Date
    May 2014
    Posts
    72

    Re: some class exercise

    The Fct here acts as in a type. For example if I write Fct h; it doesn't show any error even when running. Please have a look at the definition of Function in second link of my first post of here.

    I don't know where really is the declaration/definition of Fct. May be in the library files (that I've included them in the program).
    Anyway, the problem here, I think, is about copying/replacing the two Fcts which has occurred in the declaration of the FCT's constructor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured