title: Composition syntax in C++
body: <p>I am in the middle of getting my feet wet with the use of composition &amp; classes in C++. A code snippet I came across implements composition in the following manner:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  class Engine {
    public:
      int power;
  };
  class Car {
    public:
      Engine e;
      string company;
      string color;
      void show_details() {
        cout &lt;&lt; "Compnay is:            " &lt;&lt; company &lt;&lt; endl;
        cout &lt;&lt; "Color is:              " &lt;&lt; color   &lt;&lt; endl;
        cout &lt;&lt; "Engine horse power is: " &lt;&lt; e.power &lt;&lt; endl;
      }
  };
  int main() {
    Car c;
    c.e.power = 500;
    c.company = "hyundai";
    c.color   = "black";
    c.show_details();
    return 0;
  }
  </code></pre>
  
  <p>This compiles fine, and runs.  One thing I do not like about this implementation is the actual <em>location</em> of the function "void show_details", which I would prefer to place outside.</p>
  
  <p>However, if I naively try to do the following:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  class Engine {
    public:
      int power;
  };
  class Car {
    public:
    //Engine e;
      string company;
      string color;
      void show_details();
    //void show_details() {
    //  cout &lt;&lt; "Compnay is:            " &lt;&lt; company &lt;&lt; endl;
    //  cout &lt;&lt; "Color is:              " &lt;&lt; color   &lt;&lt; endl;
    //  cout &lt;&lt; "Engine horse power is: " &lt;&lt; e.power &lt;&lt; endl;
    //}
  };
  void Car::show_details(){
    cout &lt;&lt; "Compnay is:            " &lt;&lt; company &lt;&lt; endl;
    cout &lt;&lt; "Color is:              " &lt;&lt; color   &lt;&lt; endl;
    cout &lt;&lt; "Engine horse power is: " &lt;&lt; e.power &lt;&lt; endl;
  }
  int main() {
    Car c;
    c.e.power = 500;
    c.company = "hyundai";
    c.color   = "black";
    c.show_details();
    return 0;
  }
  </code></pre>
  
  <p>g++ returns the following errors:</p>
  
  <pre><code>comp3.cpp: In member function ‘void Car::show_details()’:
  comp3.cpp:22:44: error: ‘e’ was not declared in this scope
         cout &lt;&lt; "Engine horse power is: " &lt;&lt; e.power &lt;&lt; endl;
                                              ^
  comp3.cpp: In function ‘int main()’:
  comp3.cpp:26:9: error: ‘class Car’ has no member named ‘e’
        c.e.power = 500;
  </code></pre>
  
  <p>I am clearly confused regarding issues of scoping, but I am not sure as to what the missing piece is.</p>
  
  <p>Thanks for any &amp; all help!!!</p>