Confusion between member initializer list and invoking parent class constructor upon making daughter class constructor
#include <iostream> using namespace std; class animal { public: animal(string n, int a) { name = n; age = a; } string name; int age; }; class dog : public animal { public: dog(string n, int a) : animal(n,a), name(n), age(a) { } void speak() { cout << "woof" << endl; } ~dog() { cout << "dog destructor" << endl; } string name; int age; }; int main() { dog tommy("tommy", 10); cout << tommy.name << endl << tommy.age << endl; return 0; } In the daughter constructor line I have to invoke the parent constructor too as otherwise it raises error... So my question is that if I use member initializer list then how would I write it...The way I wrote above by just combining both of them is not working...