Classes and Composition
-
Hi everyone, i am going nutz. I can't seem to figure something out. I will post my example down below but let me explain what type of problem I have. Say you have a class which you want to put into a vector (example purposes lets say class "dog"). You create 2 to 3 elements (instances) of this class dog. The attributes and private data within the class dog is shared between the 2-3 different instances or should they not be shared. I always imagined they should not be shared but please look at this problem below and explain to me how the variable string gets shared between class A and class B. Also since I have five instances, should the constructor not be accessed five times to initialize the object? It only shows it being accessed once. Any help would be appreciated. Thanks. #include #include #include #include using namespace std; #define MAX 5 class B { public: B( ) :string( generateString( ) ) // initialize the string with the random string { cout << "B Created" << endl; } // just to indicate the B was accessed ~B( ) { } // generate a random string either 0 or 1 string generateString( ) { srand( time( NULL ) ); if( ( rand( ) % 10 ) < 5 ){ return( "1" ); } else{ return( "0" ); } } string string; }; class A { public: A( ) :names( MAX ) // creating MAX (5) instances of the B class using names vector { } ~A( ) { } // composition vector of B class vector **names; }; int main( ) { A a; for( int i = 0; i < MAX; ++i ) { cout << a.names.at( i ).string << endl; } return( EXIT_SUCCESS ); }
- Armond**
-
Hi everyone, i am going nutz. I can't seem to figure something out. I will post my example down below but let me explain what type of problem I have. Say you have a class which you want to put into a vector (example purposes lets say class "dog"). You create 2 to 3 elements (instances) of this class dog. The attributes and private data within the class dog is shared between the 2-3 different instances or should they not be shared. I always imagined they should not be shared but please look at this problem below and explain to me how the variable string gets shared between class A and class B. Also since I have five instances, should the constructor not be accessed five times to initialize the object? It only shows it being accessed once. Any help would be appreciated. Thanks. #include #include #include #include using namespace std; #define MAX 5 class B { public: B( ) :string( generateString( ) ) // initialize the string with the random string { cout << "B Created" << endl; } // just to indicate the B was accessed ~B( ) { } // generate a random string either 0 or 1 string generateString( ) { srand( time( NULL ) ); if( ( rand( ) % 10 ) < 5 ){ return( "1" ); } else{ return( "0" ); } } string string; }; class A { public: A( ) :names( MAX ) // creating MAX (5) instances of the B class using names vector { } ~A( ) { } // composition vector of B class vector **names; }; int main( ) { A a; for( int i = 0; i < MAX; ++i ) { cout << a.names.at( i ).string << endl; } return( EXIT_SUCCESS ); }
- Armond**