Search code examples
c++fltk

How to use an existing class for a new duty


The problem says:

Define a class Arc, which draws a part of an ellipse. Hint: fl_arc().

Ellipse is predefined class which draws an ellipse on the window by a statement, e.g., Ellipse e1(Point(200,200),50,50); and fl_arc() is part of FLTK which I've previously installed it on my Windows machine. Since the problem has wanted to draw a part of an ellipse by creating a new class named Arc, I think I should make a new class with that name and also use the definition of the class Ellipse and modify it so that it shows a part of an ellipse instead of whole of an ellipse as wanted. This problem is in Programming Principle and practice using C++ book and the only definition that I found in that book about the Ellipse class was this:

struct Ellipse :Shape {
    Ellipse(Point p, int w, int h); //center, max and min distance from center

    void draw_lines()const;

    Point center()const;
    Point focus1()const;
    Point focus2()const;

    void set_major(int ww) {w=ww;}
    int major() const {return w;}

    void set_minor(int hh) {h=hh;}
    int minor() const {return h;}

private:
    int w;
    int h;
};

and also I found a case of using the fl_arc() in the book which was used for drawing a Circle like this:

fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);

Here r is radius. So on my think, I changed the parameters of the fl_arc() and wrote the bellow code to give me what the problem has wanted:

#include <Simple_window.h>

struct arc : Shape {

    arc(Point p, int w, int h)    // center, min, and max distance from center
        : w(w), h(h)
    { 
        add(Point(p.x-w,p.y-h));
    }

    void draw_lines() const {fl_arc(point(0).x,point(0).y,w+w,h+h,0,120);}

    Point center() const { return Point(point(0).x+w,point(0).y+h); }
    Point focus1() const { return Point(center().x+int(sqrt(double(w*w-h*h))),center().y); }
    Point focus2() const { return Point(center().x-int(sqrt(double(w*w-h*h))),center().y); }

    void set_major(int ww) { w=ww; }
    int major() const { return w; }
    void set_minor(int hh) { h=hh; }
    int minor() const { return h; }

private:
    int w;
    int h;
};

int main()
{
    using namespace Graph_lib;

    Point t(100,100);
    Simple_window win(t,600,400, "semi-ellipse");
    arc a(Point(200,200),150,50);
    a.draw_lines();
    win.wait_for_button();
}

The code runs successfully fortunately but it doesn't show any part of an ellipse.

Question:

Does anyone know why?

PS: If we can find some way for modifying a class we can tell that new class to does some new work for us.


Solution

  • Although I could to find the bug of that code but still I have some issues about that exercise that I like to mention them among you professional guys.

    If the line a.draw_lines(); will be replaced by this line win.attach(a); the problem runs successfully.

    The remained problems are these:

    1- Now when I use the name "Arc" instead of the "arc" in above code I get this error.

    Error 8 error C2146: syntax error : missing ';' before identifier 'a' c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 25 Error 10 error C3861: 'a': identifier not found c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 25

    2- The problem has wanted us to define a class (not a struct) so when I replace struct with class and put keyword public just after class arc : Shape {, I get this error.

    *Error 8 error C2243: 'type cast' : conversion from 'arc *' to 'Graph_lib::Shape &' exists, but is inaccessible c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 29 Error 9 error C2243: 'type cast' : conversion from 'arc ' to 'Graph_lib::Shape &' exists, but is inaccessible c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 30

    Any idea?