Class question

I'm studying classes, constructor and deconstructor this week.
I thought I was understanding it properly, so I decided to input the following code to test for a better understanding.

I thought I would see 4 7 2, but the output doesn't cout the default constructor for the second input. It's just 4 2 followed by 2 4.

My question is, is the code wrong, or is my understanding of how the default constructor should work is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

class Package
{
private:
	int value;
public:
	Package()
	{ value = 7; cout << value << endl; }
	Package(int v)
	{ value = v; cout << value << endl; }
	~Package()
	{ cout << value << endl; }
};

int main()
{
	Package obj1(4);
	Package obj2();
	Package obj3(2);
	getchar();
	return 0;
}
I get the following warning when I compile your code:

warning C4930: 'Package obj2(void)': prototyped function not called (was a variable definition intended?)


If I change line 20 as follows, I get 4 7 2 as expected:
 
Package obj2;


I tested that as well and 7 finally shows. I guess it's a typo in the book then.

The book has the () to call the default con. This will help me in the future though.

Thank you.
Topic archived. No new replies allowed.