0
How can it be that sub-class in compositioin is initialized in constructor initializer?
How can the birthday class be initialized in Person class constructor although there's no such Birthday constructor that has its object as a parameter? -- please refer to the code bd(b) in Person class's constructor. ( Please anyone tell me the way how mechanism works ) class Person { public: Person(string n, Birthday b) : name(n), bd(b) { } void printInfo() { cout << name << endl; bd.printDate(); } private: string name; Birthday bd; };
1 Odpowiedź
+ 1
you have created an object of birthday class inside person class.
Suppose your birthday class contains two variables
int date;
int month;
So, when you write bd(b), it invokes constructor of birthday class where you initialise date and month variables.