c++ - How to initialize object in different class? -
i'm learning c++ , have problem basics. how init object in different class?
for example have code:
class { private: static int num; static string val; public: a(int n, string w) { num = n; val = w; } };
i want create object in class b, have try this:
class b { private: obja; public: b(int numa, string vala){ obja = new a(numa, vala); } };
different ways(same constructor):
public: b(a oba){ obja = oba; }
or
public: b(int numa, string vala){ obja = a(numa, vala); }
always i'm getting error: no default constructor exist class "a". i've read default constructor constructor without arguments, give them, why searching default?
you can following way
class b { private: obja; public: b(int numa, string vala) : obja( numa, vala ) {} };