which values does the c variable keep inside it? - C++

Hello,

I have the following code.
Our instructor told us, which values does the c variable keep inside it?
I wrote:
2-3-4-5-6-7-8-9-10-11-12-13-14-15 (because when an object is created, the default constructor is called and increases the c variable.

He told me it's incorrect! and the compiler has wrong and the correct answer is:
15-13-8-5

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

class a
{
public:
	static float c;
	a() { ++c; }
	~a() { cout << "a"; }
};
float a::c = 2;

int main()
{
	a m, f, s;
	cout << m.c;
	{a w, p, y; cout << m.c;}
	cout << "a";
	{ a h, k, t, z, r; }
	a b, c;
	cout << 27;
}
The first number is certainly not 2. Line 15 creates 3 instances of the class 'a'. So on line 16, c will be 5 (original value of 2, plus 3).

I don't understand why the professor is saying it should be 15, however.
99.99% of the time, the compiler is not wrong. Unless we're talking about some seriously outdated compiler like Turbo-C++.

(Random thought, not related to your post: I just realized the number of Turbo-C++ posts on this forum has become virtually non-existent over the past year. Hooray!)

PS: I dealt with arcane, bull*** questions like these back in my intro-ish programming classes in college. They stop asking questions like these later on, and real-life programming is nothing like this. Maybe that will bring some encouragement.
Last edited on
professor is wrong :)
it only does cout in a few places. it increments in a lot of places. there are a lot of annoying "a"s printed.
5 <---- 2, +1 for m, +1 for f, +1 for s and print it, its 5
+1 for 1, +1 for p, +1 for y is 8... print 8
it does not print any more copies of C, it just prints junk after that.

aside from printing, though,
it holds inside it
2 ... 15. every value from 2 to 15 inclusive is held by C at some point.

These are tough to deal with. The professor is wrong, but he may be stubborn, or not, about it.
Walk through it line by line with him, maybe, say you still don't get it and have him tell you its value on each line of the program, or better, after every ; in the program since its coded wonky.
Last edited on
And so you can explain the truth to your professor why not let the machine demonstrate what's going on. (Just to be safe use the prof's machine.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

class a
{
public:
    static float c;
    a() { ++c; cout << "constructor b c = " << c << '\n'; }
    ~a() { --c; cout << "destructor a c = " << c << '\n'; }
};
float a::c = 2;

int main()
{
    cout << "BEFORE CONSTRUCTION c = " << a::c << '\n';
    a m, f, s;
    a w, p, y;
    a h, k, t, z, r;
    a b, c;
    cout << "CONSTRUCTION ENDS\n";
}



BEFORE CONSTRUCTION c = 2
constructor b c = 3
constructor b c = 4
constructor b c = 5
constructor b c = 6
constructor b c = 7
constructor b c = 8
constructor b c = 9
constructor b c = 10
constructor b c = 11
constructor b c = 12
constructor b c = 13
constructor b c = 14
constructor b c = 15
CONSTRUCTION ENDS
destructor a c = 14
destructor a c = 13
destructor a c = 12
destructor a c = 11
destructor a c = 10
destructor a c = 9
destructor a c = 8
destructor a c = 7
destructor a c = 6
destructor a c = 5
destructor a c = 4
destructor a c = 3
destructor a c = 2
Program ended with exit code: 0


And then,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

class a
{
public:
    static float c;
    a() { ++c; cout << "constructor b c = " << c << '\n'; }
    ~a() { --c; cout << "destructor a c = " << c << '\n'; }
};
float a::c = 2;

int main()
{
    cout << "BEFORE CONSTRUCTION c = " << a::c << '\n';
    a m;
    cout << "CONSTRUCTION ENDS\n";
}



BEFORE CONSTRUCTION c = 2
constructor b c = 3
CONSTRUCTION ENDS
destructor a c = 2
Program ended with exit code: 0


There's one more case - guess what?
Ask the professor how the value of c can decrease when the only code that changes it adds to it.
Topic archived. No new replies allowed.