我有一個使用CImg庫的項目。我所要做的是聲明從一個稱爲shape的抽象基類繼承的類。這些類是不同形狀(圓形,矩形等)的類。我想要做的是使用一個名爲point的結構類來爲每個類需要的不同點。例如,對於三角形形狀,我必須用三個點繪製一個三角形,每個點都有一個x座標和一個y座標。所以這是我到目前爲止。作爲私有數據成員的C++結構體
class Shape
{
public:
virtual void draw_shape() = 0;
virtual double area() = 0;
};
struct Point
{
const int x; //const is necessary because CImg drawing functions require them.
const int y;
};
class Triangle : public Shape
{
private:
struct Point first;
struct Point second;
struct Point third;
public:
Triangle();
Triangle(const int, const int, const int, const int, const int, const int);
virtual void draw_shape();
virtual double area();
};
1)如何初始化每個結構的x座標和y座標?
Triangle::Triangle() : first.x(0), first.y(0), second.x(0), second.y(0), third.x(0), third.y(0)
不起作用
2)是我重載的構造正確的,或者我應該使用這個構造:
Triangle(Point, Point, Point);
3)我如何使用積分在此之後,每當我想提請什麼?! 4)在實例化C++中必要的點之前,結構是?
將構造函數添加到'Point'結構中? –
怎麼樣!我甚至不知道結構可以有構造函數 – Mustafa
C++中'struct'和'class'之間的唯一區別是默認的可見性,對於'struct',缺省情況下默認爲'public',默認爲'private'爲'類'。就是這樣,否則它們是平等的。 –