I created a derived Octagon
class from a base Shape
class (written by Stroustrup..i'm using programming principles and practice). The constructor and other methods seem to be fine except that calling the function that modifies the class member doesn't change anything.
This is the derived class i wrote(following the base class interface):
class Octagon : public Shape
{
public:
Octagon(Point centre, int side_length);
double cot(double x) const{return (cos(x) / sin(x));}
void draw_lines() const;
void set_side(int x) { s_l = x; } //am i missing something?
double rad(int side){return (0.5*side*cot(PI / 8));}
private:
int s_l;
};
Octagon::Octagon(Point centre, int side_length)
:s_l( side_length )
{
for (int ang = 22.5; ang <= 337.5; ang += 45)
{
add(Point{ int(centre.x + rad(s_l) * cos(ang*PI / 180)),
int(centre.y - rad(s_l) * sin(ang*PI / 180)) });
};
}
The set_side(int)
function doesn't actually do anything when i call it in the main function...
int main()
{
Simple_window win{ Point{100,100}, 1200, 900, "window" }; //Simple_window class
//designed by Stroustrup to display Shape objects
Octagon oct{ Point(600,450), 100};
oct.set_color(Color::black);
oct.set_side(20); //supposed to change the side length from 100 to 20
//but it stays at 100 when i run the program
win.attach(oct); //attaches Shape object to Simple_window
win.wait_for_button();
}
I don't know if this is neccessary, but this is the base Shape
class designed by Stroustrup using the FLTK GUI Library.
class Shape { // deals with color and style, and holds sequence of lines
public:
void draw() const; // deal with color and draw lines
virtual void move(int dx, int dy); // move the shape +=dx and +=dy
void set_color(Color col) { lcolor = col; }
Color color() const { return lcolor; }
void set_style(Line_style sty) { ls = sty; }
Line_style style() const { return ls; }
void set_fill_color(Color col) { fcolor = col; }
Color fill_color() const { return fcolor; }
Point point(int i) const { return points[i]; } // read only access to points
int number_of_points() const { return int(points.size()); }
virtual ~Shape() { }
protected:
Shape();
virtual void draw_lines() const; // draw the appropriate lines
void add(Point p); // add p to points
void set_point(int i,Point p); // points[i]=p;
private:
vector<Point> points; // not used by all shapes
Color lcolor; // color for lines and characters
Line_style ls;
Color fcolor; // fill color
Shape(const Shape&); // prevent copying
Shape& operator=(const Shape&);
};
By the way, draw_lines()
is called by draw()
which is invoked by a display engine.(I am following the book chapter by chapter so I haven't reached the chapter where he fully discuses this).
The problem is that the shape points are computed in the constructor, and never reset. By the time the new value is set for s_l
, the horse is out of the barn: the points of the shape are not reconstructed with the new value of s_l
in mind.
Change the setter to reset the points to their new values to make it work:
void set_side(int x) {
s_l = x;
int pos = 0;
for (double ang = 22.5 ; ang <= 337.5 ; ang += 45) {
set_point(pos++, Point {
int(centre.x + rad(s_l) * cos(ang*PI / 180))
, int(centre.y - rad(s_l) * sin(ang*PI / 180))
});
};
}
Note that ang
above should be double
, not int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With