【问题标题】:How to use an existing class for a new duty如何将现有课程用于新职责
【发布时间】:2014-04-26 15:21:17
【问题描述】:

问题说:

定义一个 Arc 类,用于绘制椭圆的一部分。提示:fl_arc().

Ellipse 是预定义的类,它通过一个语句在窗口上绘制一个椭圆,例如,Ellipse e1(Point(200,200),50,50); fl_arc() 是我之前在我的 Windows 机器上安装的 FLTK 的一部分。 由于问题想通过创建一个名为 Arc 的新类绘制椭圆的一部分,我认为我应该使用该名称创建一个新类,并使用 Ellipse 类的定义并对其进行修改这样它就可以根据需要显示椭圆的一部分而不是整个椭圆。这个问题在 Programming Principle and practice using C++ 一书中,我在那本书中找到的关于 Ellipse 类的唯一定义是:

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;
};

我还在书中发现了一个使用fl_arc() 的案例,用于绘制这样的圆圈:

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

这里r是半径。 所以我想,我改变了fl_arc()的参数并写了下面的代码来给我这个问题想要的东西:

#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();
}

幸运的是,代码运行成功,但没有显示椭圆的任何部分。

问题:

有人知道为什么吗?

PS:如果我们能找到修改类的方法,我们可以告诉新类为我们做一些新的工作。

【问题讨论】:

  • 感谢您的意见。但是直到现在我还没有在这本书中学习过关于虚拟的知识,我也不知道,所以作者应该如何让我解决你的一个如此复杂的问题!?

标签: c++ fltk


【解决方案1】:

这是一种可能的实现,您可以使用class Ellipse 的现有设施,覆盖函数draw_lines(),来定义class Arc

#include"Simple_window.h"
#include"Graph.h"
#include<iostream>

using namespace Graph_lib;

//---------------------------------------------------------------------------
//Class Arc
namespace Graph_lib{
class Arc : public Ellipse {
public:
    Arc(Point p, int w, int h, int arc_n, int arc_x) : Ellipse(p,w,h), arc_min(arc_n), arc_max(arc_x) {}
    void draw_lines() const;

private:
    int arc_min;
    int arc_max;
};

void Arc::draw_lines() const
{
    if(color().visibility())
        fl_arc(point(0).x,point(0).y, major()*2, minor()*2, arc_min, arc_max);
}
}
//---------------------------------------------------------------------------

int main()
try
{
    Simple_window win(Point(100,100), 800, 600, "Exercise #1");

    Graph_lib::Arc arc1(Point(100,100),50,50,0,90);

    win.attach(arc1);
    win.wait_for_button();
}
catch(exception& e)
{
    std::cout << e.what() << std::endl;
    return 1;
}
catch(...)
{
    std::cout << "unknown error..." << std::endl;
    return 2;
}

编程 - 使用 C++ 的原理和实践(pg.477,练习 1) 弗朗西斯科·塔瓦雷斯的解决方案

【讨论】:

  • 虽然这段代码可能很好地回答了这个问题。您可能需要提供一些上下文,以便提问者更好地理解解决方案。
【解决方案2】:

虽然我可以找到该代码的错误,但我仍然有一些关于该练习的问题,我想在你们专业人士中提及它们。

如果a.draw_lines(); 行将被win.attach(a); 行替换,则问题运行成功。

剩下的问题是:

1- 现在,当我在上面的代码中使用名称“Arc”而不是“arc”时,会出现此错误。

错误 8 错误 C2146:语法错误:缺少 ';'在标识符“a”之前 c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 25 错误 10 错误 C3861: 'a': 找不到标识符 c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 25

2- 问题是要我们定义一个类(不是结构),所以当我用 class 替换 struct 并放入关键字 publicclass arc : Shape { 之后,我得到了这个错误。

*Error 8 error C2243: 'type cast' : 从 'arc *' 到 'Graph_lib::Shape &' 的转换存在,但无法访问 c:\users\cs\documents\visual studio 2012\projects\test2\测试2\测试2.cpp 29 错误 9 错误 C2243: 'type cast' : 从 'arc ' 到 'Graph_lib::Shape &' 的转换存在,但无法访问 c:\users\cs\documents\visual studio 2012\projects\test2\test2\ test2.cpp 30

有什么想法吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多