0

I have a project using the CImg library. What I have to do is declare classes that inherit from an abstract base class called shape. These classes are classes for the different shapes(circle, rectangle .. etc). What I want to do is use a struct class called point for the different points that I need for every class. So for example, for a triangle shape, I have to use three points to draw a triangle with each point having an x coordinate and a y-coordinate. So here's what I got so far.

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) How do I initialize the x-coordinate and y-coordinate of each struct ?

Triangle::Triangle() : first.x(0), first.y(0), second.x(0), second.y(0), third.x(0), third.y(0)

does not work

2) Is my overloaded constructor correct or should I use this constructor:

Triangle(Point, Point, Point);

3) How do i use the points after this whenever I want to draw something ?! 4) Is the struct before instantiating the Points necessary in c++?

Mustafa
  • 177
  • 1
  • 2
  • 10
  • Add a constructor to the `Point` structure? – Some programmer dude May 20 '14 at 11:23
  • How ?! I didn't even know structure can have constructors – Mustafa May 20 '14 at 11:25
  • 2
    The only difference between a `struct` and a `class` in C++, is the default visibility, for `struct` everything is `public` by default while it's `private` by default for `class`. That's it, otherwise they are equal. – Some programmer dude May 20 '14 at 11:26
  • 3
    “const is necessary because CImg drawing functions require them” – No, you have misunderstood something here. – Konrad Rudolph May 20 '14 at 11:26
  • as Joachim said - you are writing C++ and no C. – NirMH May 20 '14 at 11:27
  • @KonradRudolph Which is ?!?! The documentation of CImg says: CImg< T > & draw_triangle (const int x0, const int y0, const int x1, const int y1, const int x2, const int y2, const tc *const color, const float opacity=1)" – Mustafa May 20 '14 at 11:36
  • 2
    @Mustafa, Doesn't mean you have to pass `const int`s. Just means the function doesn't change its copy. The documentation shouldn't include the `const` for things taken by value, really. I really recommend a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for these types of things. – chris May 20 '14 at 11:36
  • @chris Damn whoever wrote the documentation then :). Thanks chris – Mustafa May 20 '14 at 11:41
  • 1
    @Mustafa, Well, it's still not *wrong*, just redundant and of no use to someone using the library. – chris May 20 '14 at 11:42
  • `const` decorations on parameters are more of an "interface contract" than a type requirement; it says "we will not modify these parameters in this function, or pass them to other functions that will modify them". It doesn't really make sense on pure value parameters like the `draw_triangle` example, but it won't stop you passing in plain old ints. – Rook May 20 '14 at 11:49

2 Answers2

1

You can invoke the constructors of your points, like this:

Triangle() : first{0, 0}, second{0, 0}, third{0, 0} {}

You can add an explicit constructor yourself, if you wanted to do something a little more complex:

struct Point
{
    int x;
    int y;

    Point(int a, int b) : x(a), y(b) { /* ... */ }
};
Rook
  • 5,734
  • 3
  • 34
  • 43
1

Considering 1)

You can simply use this:

Triangle::Triangle() : first{0,0}, second{0,0}, third{0,0} {}

Considering 2) I think that the constructor

Triangle(Point, Point, Point); 

is better. When you already have points, why not using them?

Considering 3) dependons on how things get drawn

Considering 4) No, it is not necessary.

gexicide
  • 38,535
  • 21
  • 92
  • 152
  • Cool. But will I be able to instantiate the points using ints. I do not want to have to instantiate Point structs everytime I want to draw something – Mustafa May 20 '14 at 11:30
  • 1
    @Mustafa - It would help then if you added to your question. Add a "preferred API" showing how you would _like_ to use your classes. – Dennis May 20 '14 at 11:43
  • @Mustafa: Instanciating a point is not less efficient than simply using two ints. Why don't you want to use points when drawing something? – gexicide May 20 '14 at 12:44
  • @gexicide After I started implementing the member function I realize how unnecessary the Point struct is. I just included it because the lecturer referred to using it, but I will just ignore it. – Mustafa May 20 '14 at 14:39