0

Multiple constructor in c++

I have two constructor in a class but I don't call the second constructor. But it seems that the program enters in the second constructor. Whyyyyyy? 🤔😠 https://code.sololearn.com/c9A148a87A14/?ref=app

9th Jan 2021, 7:08 AM
farkhondeh
4 Antworten
+ 3
You cannot have 2 constructors in classes unless you are overloading the constructors. That is class Class { public: Class(int x){} Class(int x){} }; is invalid, but class Class { public: Class(int x){ cout << "1"; } Class(string x){ cout << "2"; } }; is valid as each constructor is taking different data types. The constructor will be called on the basis of the argument you pass. For example, in the class declaration above, initializing an object by Class(2) will display 1 on the screen, i.e., the first constructor will be called. Doing `Class("string")` will display 2. So check which data type you are passing. If you want the 1st constructor to be called, passed in arguments according to the 1st constructor. Also please post your code along with the question for better answers
9th Jan 2021, 7:21 AM
XXX
XXX - avatar
+ 3
I can't say anything until I see your code. I will need to see what you are doing. So copy your code, make a new c++ code in the code playground, paste the code there and link the code here.
9th Jan 2021, 7:30 AM
XXX
XXX - avatar
+ 1
Ok in abstract tree I have two constructor that I don't use second constructor
9th Jan 2021, 7:41 AM
farkhondeh
0
XXX I have two constructor with different input . And my code is too much
9th Jan 2021, 7:27 AM
farkhondeh