c++ - Create an object that contains another object -
i studying c++, , bit confused following situation:
for example,
class apple
class apple { int kg; apple(); }
class fruit
class fruit { private: int count; apple one; public: fruit();//do need call constructor apple, //or default fruit constructor calls well? }
in example default constructor gets called. if apple constructor example took int first param have set through initializer list:
class apple { public: int kg; apple(int _kg) : kg(_kg) {} }; class fruit { private: int count; apple one; public: fruit() : one(5) //right here or you'll error { this->count = 5; //this->one(5) doesn't work. } };
Comments
Post a Comment