using objects in class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

using objects in class

Is it possible to use objects that are from other class abd use it in a different class? If so how? As the object is not declared when the class is being created.

14th Sep 2017, 11:49 PM
jayrn02
jayrn02 - avatar
1 Answer
+ 14
Composition. class A { public: void speak() { std::cout << "Hi"; } }; class B { public: void doStuff() { obj.speak(); } private: A obj; }; int main() { B obj; obj.doStuff(); return 0; }
15th Sep 2017, 3:18 AM
Hatsy Rei
Hatsy Rei - avatar