+ 1

constructors?

Can there be more than 1 constructor?

15th Jul 2016, 4:26 AM
Bryan Aguilar
Bryan Aguilar - avatar
1 Antwort
+ 2
yes just like function overloading can have multiple functions with same name but different arguments....constructors can also be overloaded. ...... class X { int a,b; public: //make sure the constructor you //write is in public X (){ //#1 a=0; b=10; } X (int p, int q){ //#2 a=p; b=q; } X (const char *str) //#3 {std::cout <<str;} }; ...... int main () { ....... X x , y (4,-1), z("constructor#3"); ......... }
15th Jul 2016, 4:50 AM
Mukul Kumar
Mukul Kumar - avatar