Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Classes and Composition

Classes and Composition

Scheduled Pinned Locked Moved Design and Architecture
helpgraphicstutorialquestionlounge
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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**

    L 1 Reply Last reply
    0
    • L Lost User

      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**

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Armond Sarkisian wrote:

      explain to me how the variable string gets shared between class A and class B

      string generateString()
      {
      static int ticket = 1;
      char buf[2] = {0};
      itoa(ticket++, buf, 10);
      return buf;
      }

      led mike

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups