943,879 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1426
  • C++ RSS
Sep 8th, 2007
0

Question on my constructor

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. class person
  9. {
  10. private:
  11. char first_name[30];
  12. char last_name[30];
  13. int itsAge;
  14.  
  15. public:
  16. person(int initialAge);
  17. ~person();
  18.  
  19. void setting()
  20. {
  21. cout<<"Enter first_name ";
  22. cin>>first_name;
  23. cout<<"Enter last_name ";
  24. cin>>last_name;
  25. }
  26.  
  27. void printing()
  28. {
  29. cout<<"The first_name ";
  30. cout<<first_name<<endl;
  31. cout<<"The last_name ";
  32. cout<<last_name<<endl;
  33. }
  34. };
  35.  
  36. //constructor of person
  37.  
  38. person::person(int initialAge) // constructor
  39. {
  40. itsAge=initialAge;
  41. }
  42.  
  43. person::~person() // destructor
  44. {}
  45.  
  46. void main()
  47. {
  48. int pause;
  49. person p1;
  50. p1.setting();
  51. p1.printing();
  52. cin>> pause;
  53. }
Last edited by Ancient Dragon; Sep 8th, 2007 at 5:42 pm. Reason: corrected code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Granprix is offline Offline
3 posts
since Sep 2007
Sep 8th, 2007
0

Re: Question on my constructor

C++ Syntax (Toggle Plain Text)
  1.  
  2. public:
  3. person();


C++ Syntax (Toggle Plain Text)
  1.  
  2. person::person () // constructor
  3. {
  4.  
  5. }

maybe add?
Last edited by iamthwee; Sep 8th, 2007 at 2:42 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 8th, 2007
0

Re: Question on my constructor

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Granprix is offline Offline
3 posts
since Sep 2007
Sep 8th, 2007
0

Re: Question on my constructor

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Granprix is offline Offline
3 posts
since Sep 2007
Sep 8th, 2007
0

Re: Question on my constructor

Quote ...
Does there have to be a default constructor?
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.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007

This thread is solved

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.
Message:
Previous Thread in C++ Forum Timeline: software
Next Thread in C++ Forum Timeline: Does anybody kno SU DOKU source code in C/C++?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC