I am not a C++ wizard, so please excuse me if this question is completely stupid.
I am struggling to understand what the correct way to initialize instances of a class for object orient programming is.
If I am not mistaken, there are 2 methods to initialize an instance "instance_name" of CLASS1 by calling the constructor:
(1) CLASS1 instance_name(argument);
(2) CLASS1 instance_name=CLASS1(argument);
As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Can anybody explain why only method (2) will work inside another class? Or is there another fundamental mistake I am making?
(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
using namespace std;
class CLASS1{
private:
bool status;
public:
// here comes the constructor for CLASS1:
CLASS1(bool argument){
status = argument;
cout << endl;
cout << "An instance of CLASS1 was created with argument '" << argument << "'" << endl;
};
// ... and some simple member function
bool hasStatus(){
return status;
};
};
class CLASS2{
private:
bool status;
CLASS1 Instance1Class1=CLASS1(true);
// The above line compiles with a warning "warning:
// non-static data member initializers only available with -std=c++11 or -
// std=gnu++11 [enabled by default]".
//
// CLASS1 Instance2Class1(true); will not compile.
// There is an error "expected identifier before 'true'"
public:
// here comes the constructor for CLASS2:
CLASS2(bool argument){
status = argument;
cout << endl;
cout << "An instance of CLASS2 was created with argument '" << argument << "'" << endl;
cout << "This instance of CLASS2 initialised a private instance of CLASS1. The status of this instance is '" << Instance1Class1.hasStatus() << "'"<<endl;
};
// ... and some simple member function
bool hasStatus(){
return status;
};
};
int main() {
CLASS1 Instance1Class1InMain(false);
CLASS2 Instance1Class2InMain(false);
CLASS2 Instance2Class2InMain(true);
cout << endl;
cout << "The declarations are over and the main program has started." << endl;
cout << "As you can see from the output above, the declarations of several CLASS1 and CLASS2 objects have already happened. "<< endl << endl;
cout << "One instance of CLASS1 was initialized in main() with status '" << Instance1Class1InMain.hasStatus() << endl << endl;
cout << "A first instance of CLASS2 was initialized in main() with status '" << Instance1Class2InMain.hasStatus() << endl;
cout << "This instance of CLASS2 will have created a private instance of CLASS1 ... I can't read the value from Main(), but I know that CLASS2 objects initilize their CLASS1 objects as '1'." <<endl<< endl;
cout << "A second instance of CLASS2 was initialized in main() with status '" << Instance2Class2InMain.hasStatus() << endl;
cout << "This instance of CLASS2 will have created a private instance of CLASS1 ... I can't read the value from Main(), but I know that CLASS2 objects initilize their CLASS1 objects as '1'." <<endl<< endl;
return 0;
}
|
And this is the output:
An instance of CLASS1 was created with argument '0'
An instance of CLASS1 was created with argument '1'
An instance of CLASS2 was created with argument '0'
This instance of CLASS2 initialised a private instance of CLASS1. The status of
this instance is '1'
An instance of CLASS1 was created with argument '1'
An instance of CLASS2 was created with argument '1'
This instance of CLASS2 initialised a private instance of CLASS1. The status of
this instance is '1'
The declarations are over and the main program has started.
As you can see from the output above, the declarations of several CLASS1 and CLASS2 objects have already happened.
One instance of CLASS1 was initialized in main() with status '0
A first instance of CLASS2 was initialized in main() with status '0
This instance of CLASS2 will have created a private instance of CLASS1 ... I can't read the value from Main(), but I know that CLASS2 objects initilize their CLASS1 objects as '1'.
A second instance of CLASS2 was initialized in main() with status '1
This instance of CLASS2 will have created a private instance of CLASS1 ... I can't read the value from Main(), but I know that CLASS2 objects initialize their CLASS1 objects as '1'.
Process returned 0 (0x0) execution time : 0.036 s
Press any key to continue.