This is from \"Programming -- Principles and Practice Using C++\" by Bjarne Stro
ID: 3829480 • Letter: T
Question
This is from "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup Chapter 14 Excersise 1
Modify the following code to:
- make the eyes round instead of vertice lines
- colour the faces in yellow colour
- make the mouth as half circle
class Smiley: public Circle {
private:
Open_polyline lefteye;
Open_polyline righteye;
Open_polyline mouth;
public:
// Explicitly call Circle's constructor. Then le's constructor gets called
Smiley(Point c, int r ) : Circle( c, r )
{
lefteye.add( Point(c.x - (r / 3), c.y - (r / 2)) );
lefteye.add( Point(c.x - (r / 3), c.y - (r / 6)) );
righteye.add( Point(c.x + (r / 3), c.y - (r / 2)) );
righteye.add( Point(c.x + (r / 3), c.y - (r / 6)) );
mouth.add( Point(c.x - (r / 1.5), c.y + (r / 6)) );
mouth.add( Point(c.x - (r / 3), c.y + (r / 2)) );
mouth.add( Point(c.x, c.y + (r / 1.65)) );
mouth.add( Point(c.x + (r / 3), c.y + (r / 2)) );
mouth.add( Point(c.x + (r / 1.5), c.y + (r / 6)) );
}
void draw_lines() const {
Circle::draw_lines() ;
lefteye.draw_lines() ;
righteye.draw_lines();
mouth.draw_lines();
}
};
class frowny: public Circle {
private:
Open_polyline lefteye;
Open_polyline righteye;
Open_polyline mouth;
public:
// Explicitly call Circle's constructor. Then le's constructor gets called
frowny(Point c, int r ) : Circle( c, r )
{
lefteye.add( Point(c.x - (r / 2.75), c.y - (r / 2)) );
lefteye.add( Point(c.x - (r / 2.75), c.y - (r / 7)) );
righteye.add( Point(c.x + (r / 2.75), c.y - (r / 2)) );
righteye.add( Point(c.x + (r / 2.75), c.y - (r / 7)) );
mouth.add( Point(c.x - (r / 1.75), c.y + (r / 2)) );
mouth.add( Point(c.x - (r / 3), c.y + (r / 3.5)) );
mouth.add( Point(c.x, c.y + (r / 5)) );
mouth.add( Point(c.x + (r / 3), c.y + (r / 3.5)) );
mouth.add( Point(c.x + (r / 1.75), c.y + (r / 2)) );
}
void draw_lines() const {
Circle::draw_lines() ;
lefteye.draw_lines() ;
righteye.draw_lines();
mouth.draw_lines();
}
};
class SmileyHat: public Smiley {
private:
Open_polyline hat ;
public:
SmileyHat(Point c, int r) : Smiley(c,r) {
hat.add( Point(c.x, c.y-r) ) ;
hat.add( Point(c.x+r, c.y-r) ) ;
}
void draw_lines() const {
Smiley::draw_lines() ;
hat.draw_lines() ;
}
};
int main()
{
using namespace Graph_lib;
Point tl(100,100);
Simple_window win(tl,600,400,"Canvas");
SmileyHat s( Point(150,150), 50 ) ;
frowny f( Point(350,150), 50 ) ;
win.attach (s);
win.attach (f);
win.wait_for_button();
}
Explanation / Answer
Modified code:
class Smiley: public Circle {
private:
Open_polyline lefteye;
Open_polyline righteye;
Open_polyline mouth;
public:
// Explicitly call Circle's constructor. Then le's constructor gets called
Smiley(Point c, int r ) : Circle( c, r )
{
lefteye.add( Point(c.x - (r / 3), c.y - (r / 2)) );
lefteye.add( Point(c.x - (r / 3), c.y - (r / 6)) );
righteye.add( Point(c.x + (r / 3), c.y - (r / 2)) );
righteye.add( Point(c.x + (r / 3), c.y - (r / 6)) );
mouth.add( Point(c.x - (r / 1.5), c.y + (r / 6)) );
mouth.add( Point(c.x - (r / 3), c.y + (r / 2)) );
mouth.add( Point(c.x, c.y + (r / 1.65)) );
mouth.add( Point(c.x + (r / 3), c.y + (r / 2)) );
mouth.add( Point(c.x + (r / 1.5), c.y + (r / 6)) );
}
void draw_lines() const {
Circle::draw_lines() ;
lefteye.draw_lines() ;
righteye.draw_lines();
mouth.draw_lines();
}
};
class frowny: public Circle {
private:
Open_polyline lefteye;
Open_polyline righteye;
Open_polyline mouth;
public:
// Explicitly call Circle's constructor. Then le's constructor gets called
frowny(Point c, int r ) : Circle( c, r )
{
lefteye.add( Point(c.x - (r / 0.75), c.y + (r / 2)) );
lefteye.add( Point(c.x - (r / 1.75), c.y + (r / 3.5)) );
lefteye.add( Point(c.x, c.y + (r / 5)) );
lefteye.add( Point(c.x + (r / 2), c.y + (r / 3.5)) );
lefteye.add( Point(c.x + (r / 1.75), c.y + (r / 2)) );
righteye.add( Point(c.x - (r / 0.75), c.y + (r / 2)) );
righteye.add( Point(c.x - (r / 1.75), c.y + (r / 3.5)) );
righteye.add( Point(c.x, c.y + (r / 5)) );
righteye.add( Point(c.x + (r / 2), c.y + (r / 3.5)) );
righteye.add( Point(c.x + (r / 1.75), c.y + (r / 2)) );
mouth.add( Point(c.x - (r / 1.75), c.y + (r / 2)) );
mouth.add( Point(c.x - (r / 3), c.y + (r / 3.5)) );
mouth.add( Point(c.x, c.y + (r / 5)) );
mouth.add( Point(c.x + (r / 3), c.y + (r / 3.5)) );
mouth.add( Point(c.x + (r / 1.75), c.y + (r / 2)) );
}
void draw_lines() const {
Circle::draw_lines() ;
lefteye.draw_lines() ;
righteye.draw_lines();
mouth.draw_lines();
}
};
class SmileyHat: public Smiley {
private:
Open_polyline hat ;
public:
SmileyHat(Point c, int r) : Smiley(c,r) {
hat.add( Point(c.x, c.y-r) ) ;
hat.add( Point(c.x+r, c.y-r) ) ;
}
void draw_lines() const {
Smiley::draw_lines() ;
hat.draw_lines() ;
}
};
int main()
{
using namespace Graph_lib;
Point tl(100,100);
Simple_window win(tl,600,400,"Canvas");
SmileyHat s( Point(150,150), 50 ) ;
frowny f( Point(350,150), 50 ) ;
win.attach (s);
win.attach (f);
win.wait_for_button();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.