I have studied C++ here for over 60 hours.

Hello,everybody here.
I am a newbie in programming and I only use this website for my learning resource.
Now I have studied C++ here for over 60 hours on my own.
And I am studying Class(1) in the tutorial and have a question below.

if A class is defined with a constructor within it and the constructor is not a default one, we cannot declare an object of that class by:
myclass abc; (assume that myclass is the class_name).

but if abc is declared within anonther class as a parameter,
myclass abc is accpetable.

Why?

Thanks!
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
 class circle
{
	double r;
	double t;
public:
	circle(double a,double b)
	{
		r = a;
		t = b;
	}
	double area()
	{
		return t * r*r;

	}
};

class cylinder
{
	double height;
	circle ccc; //it's ok here. but it' not ok with the main function.
public:
	cylinder(double, double,double);
	double volume()
	{
		return ccc.area() * height;
	}

};
cylinder::cylinder(double a, double c,double b) :ccc(a,c), height(b) {}
When you create a cylinder, the cylinder's constructor DOES construct a circle with parameters.

Note what happens if you don't have ccc(a,c) in the initialisation list for the cylinder constructor.
Topic archived. No new replies allowed.