Inheritance by using FLTK

Hello guys,

This problem is completely understandable for who has used book PPP (http://www.stroustrup.com/programming1.html) and has installed and used the FLTK for exercises of that book.

The problem says this:
Define a class Smiley, which is derived from class Circle ans has a eye.
This is some part of the exercise 1 of chapter 14 of PPP.

I tried to write below code for it but the compiler (MS VS 2012) gets two errors as follows.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Simple_window.h>

class Smiley : public Circle {
public:
	Smiley(Point, int);
	void eyes();       
};

//***************************

Smiley::Smiley(Point p, int r)
{
	add(Point(p.x-r,p.y-r));
}

//******************************

void Smiley::eyes()
{
	  draw_lines();
}

//**************************************

int main()
{
	Simple_window win(Point(100,100),600,400, "test");
	
	Smiley s(Point(100,100),60);
	s.eyes();

	win.attach(s);
	win.wait_for_button();
	return 0;
}


Errors:

1
2
3
4
Error	8	error C2512: 'Graph_lib::Circle' : no appropriate default constructor available	c:\users\cs\documents\visual studio 2012\projects\test\test\test.cpp	12

	9	IntelliSense: no default constructor exists for class "Graph_lib::Circle"	c:\Users\CS\Documents\Visual Studio 2012\Projects\test\test\test.cpp	12


How to solve those errors please?
PS: I DON'T NEED CODE. The only thing I feel to need is guidance. If possible, please guide me so that myself can solve the issue.

Thanks in advance.
Your Smiley class derives from Circle. As such... it is as if your smiley class contains its own Circle.

When you construct your Smiley class, the Smiley class has to construct its parent Circle class. IE: you need to call one of Circle's constructors from Smiley's constructor.

You are not explicitly calling any Circle constructor from Smiley. Therefore the compiler is default constructing the Circle (ie: attempting to call a constructor with no parameters). This is giving you an error because your Circle class does not have any default constructor.


Your options are:

1) Give Circle a default constructor (one which takes no parameters)

or

2) Have Smiley explicitly call an available Circle constructor with parameters.
Thank you very much for your guide.

The below code gave the result.
I have one question about it. First please have a look at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <Simple_window.h>

class Smiley : public Circle {
public:
	Smiley(Point p, int r): Circle(p,r){}    
};

//*******************

int main()
{
	Simple_window win(Point(100,100),600,400, "test");
	
	Smiley s(Point(100,100),60);
	
	win.attach(s);
	win.wait_for_button();
	return 0;
}


As you consider, I have created one object here Smiley s(Point(100,100),60); and the Smiley's constructor calls the Circle's constructor by parameters. Then the Circle's constructor just stores that point and r . The below is code for Circle:

This one from Graph.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//------------------------------------------------------------------------------


struct Circle : Shape {
	Circle(Point p, int rr)	// center and radius
		:r(rr) { add(Point(p.x-r,p.y-r)); }

	void draw_lines() const;

	Point center() const;

	void set_radius(int rr) { set_point(0,Point(center().x-rr,center().y-rr)); r=rr;  }
	int radius() const { return r; }
private:
	int r;
};

//------------------------------------------------------------------------------  


And this one from Graph.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//------------------------------------------------------------------------------
/*
Circle::Circle(Point p, int rr)    // center and radius
:r(rr)
{
    add(Point(p.x-r,p.y-r));       // store top-left corner
}
*/
//------------------------------------------------------------------------------

Point Circle::center() const
{
    return Point(point(0).x+r, point(0).y+r);
}

//------------------------------------------------------------------------------

void Circle::draw_lines() const
{
  	if (fill_color().visibility()) {	// fill
		fl_color(fill_color().as_int());
		fl_pie(point(0).x,point(0).y,r+r-1,r+r-1,0,360);
		fl_color(color().as_int());	// reset color
	}

	if (color().visibility()) {
		fl_color(color().as_int());
		fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);
	}
}

//------------------------------------------------------------------------------ 


When I ran my code (first one above), it drew a circle. note that please, I haven't called draw_lines() function yet! It naturally should draw that circle when I would call the draw_lines() function because the Circle's constructor just stores that point and r and (according to the Graph.h and Graph.cpp) it doesn't do any other thing. So why it draws a circle in this situation, please?
Last edited on
Topic archived. No new replies allowed.