Hi! I'm having a proble with constructors. I'm supposed to write a program that has a default constructor, but also one that takes parameters. The program I wrote below is desigend to allow teh setting of age of the person, but when I compile it, I get a message saying "no appropriate default constructor available". Any help greatly appreciated. Umm, and hints on how to correctly insert my posts so code shows up better would be great as well. Thanks again!
Thanks, Bill
Well, that certainly worked... Maybe I don't understand constructors aswell as I thought... Does there have to be a default constructor? If I write a constructor that takes a parameter, am I obligated to write a seperate default constructor? Thanks again!
Bill
Well, is this right? If you make a constructor that accepts parameters, then you have to give it a parameter when you declare an object of the class. Unless you have another constructor which is default, then you have the option af adding a parameter, or not?
I hope that's right!
Bill
Only if you use the default constructor when making objects. If you do Object obj; then Object has to have a default constructor, but if you do Object obj( arg ); then Object only has to have a constructor that takes a single argument.
Quote ...
If I write a constructor that takes a parameter, am I obligated to write a seperate default constructor?
If you need a default constructor and there's already a constructor that takes parameters, you have to write the default constructor. If there's not already a constructor that takes parameters, all classes have a default constructor automatically so you don't need to write it unless it has to do something special beyond what the automatic one does.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.