Question on my constructor

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 3
Reputation: Granprix is an unknown quantity at this point 
Solved Threads: 0
Granprix Granprix is offline Offline
Newbie Poster

Question on my constructor

 
0
  #1
Sep 8th, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Question on my constructor

 
0
  #2
Sep 8th, 2007
  1.  
  2. public:
  3. person();


  1.  
  2. person::person () // constructor
  3. {
  4.  
  5. }

maybe add?
Last edited by iamthwee; Sep 8th, 2007 at 2:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 3
Reputation: Granprix is an unknown quantity at this point 
Solved Threads: 0
Granprix Granprix is offline Offline
Newbie Poster

Re: Question on my constructor

 
0
  #3
Sep 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 3
Reputation: Granprix is an unknown quantity at this point 
Solved Threads: 0
Granprix Granprix is offline Offline
Newbie Poster

Re: Question on my constructor

 
0
  #4
Sep 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Question on my constructor

 
0
  #5
Sep 8th, 2007
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.

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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC