hello All,
I just tried with the following code & got some error in both Turbo & Visual studio
class ABC{
int a=10;
ABC(){}
}
1) why cant we declare & initialize a variable like this (a=10)
2) Is there any way to initialize variables
3) It is possible to initialize variables like this in C# etc
In C++ you can supply a member initialisation list in a constructor. It has a leading semi-colon and is comma separated and comes before the first opening brace of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13
class ABC
{
ABC(void)
int a, b, c;
std::string str;
}
ABC::ABC(void)
:a(0), b(0), c(12), str("Hello")
{
// code body. Variables have been initialised to:
// a = 0, b = 0, c = 12 and str = "Hello"
}