// using objects inside classes // Mikhail Nesterenko // 3/17/2016 #include #include using std::cout; using std::endl; using std::string; // example class class Example{ public: Example(int a, string b): a_(a), b_(b) {} // constructor with initializer list void print() const; private: int a_; string b_; }; void Example::print() const{ cout << a_ << ' ' << b_ << endl; } // second example class whose member is an object class CompExample{ public: CompExample(int x, int a, string b): x_(x), y_(a, b){} // calling Example constructor CompExample(): x_(0), y_(0, ""){} // void construtor void print() const; private: int x_; Example y_; }; void CompExample::print() const{ cout << x_ << endl; y_.print(); // invoking member function on y_ } int main(){ CompExample ob1(1, 2, "three"); CompExample ob2; ob2 = ob1; // copying object sate ob2.print(); // printing object state }