calling methods and constructor class

I didn't understand how this code works.
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 abcd {
public:
static int a;
int b;
abcd(int x) {
b=x;
a=b+1;
}
};

int abcd::a=0;  
int main(void) {
abcd b1(5);
abcd b2(12);
cout << "b1.a is " << b1.a << endl;
cout << "b1.b is " << b1.b << endl;
cout << "b2.a is " << b2.a << endl;
cout << "b2.b is " << b2.b << endl;
}


When it says abcd b1(5);
I literally insert 5 into
abcd(int x){
b=x;
a=b+1;
}
so b =5
and a=6
But I don't think it works like that.
Somebody please explain me how it works.
It works like that.
The value you pass to the function will be referred to as 'x' inside it. In this case you passed 5 so wherever you write 'x' it's like like writing 5. If you wrote abcd b1(7); it would have been like writing 7 instad of 'x' everywhere (not EXACTLY the same, but pretty much the same).
Thanks maeriden.
Also can you please explain me how b1.a and b1.b works?
cout << "b1.a is " << b1.a << endl;
cout << "b1.b is " << b1.b << endl;
Topic archived. No new replies allowed.