How can it be that sub-class in compositioin is initialized in constructor initializer? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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; };

9th Nov 2016, 5:58 AM
Sang Yoon Lee
Sang Yoon Lee - avatar
1 Resposta
+ 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.
9th Nov 2016, 7:02 AM
kamal joshi
kamal joshi - avatar