All you have to do is call copy constructor A when copying construct B as
B(const B& other) : A(other) { num2 = other.num2; num3 = other.num3; }
Since B inherits from A , this is legal, and A will copy part of A other .
Also note that all of these pointers are not needed and make the code more complex. We could rewrite it like this:
class A { private: string field1; string field2; string field3; double num1; public: A(const string& o, const string& n, const string& m, double a = 0) : field1(o), field2(n), feild3(m), num1(a) {} A(const A& other) field1(other.field1), field2(other.field2), feild3(other.feild3), num1(other.num1) {} void show() { cout << field1 << " " << field2 << " " << field3 << "\n"; } }; class B : public A { private: double num2; double num3; public: B(double num2, double num3, const string& o, const string& n, const string& m, double a = 0) : A(o, n, num, a), num2(num2), num3(num3) {} B(const B& other) : A(other), num2(other.num2), num3(other.num3) {} void show() { cout << num2 << " " << num3 << "\n"; A::show(); } };
source share