944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 15833
  • C++ RSS
Jul 16th, 2005
0

C++ program using classes

Expand Post »
Below is a program that determines if one address comes before another address using classes. One constructor takes an apartment parameter and the other doesn't. My errors are invalid conversion from int to const and among others. Any ideas?


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <stdlib>
  3.  
  4. using namespace std;
  5.  
  6. class Address
  7. {
  8.  
  9. public:
  10. Address(int h, string st, int appt, string c, string s, string pc);
  11. Address(int h, string st, string c, string s, string pc);
  12. bool comes_before(Address a) const;
  13. void print() const;
  14.  
  15. private:
  16. int house_number;
  17. string street;
  18. int apartment_number;
  19. string city;
  20. string state;
  21. string postal_code;
  22. };
  23.  
  24. Address::Address(int h, string st, int appt, string c, string s, string pc)
  25. {
  26. // initialization of the variables
  27. house_number = h;
  28. street = st;
  29. apartment_number = appt;
  30. city = c;
  31. state = s;
  32. postal_code = pc;
  33. }
  34.  
  35. Address::Address(int h, string st, string c, string s, string pc)
  36. {
  37.  
  38.  
  39. // initialization of the variables
  40. house_number = h;
  41. street = st;
  42. city = c;
  43. state = s;
  44. postal_code = pc;
  45. }
  46. void Address::print()const
  47. {
  48.  
  49. cout << house_number << " " << street;
  50. if (apartment_number > 0 )
  51. cout << ", #" << apartment_number << endl;
  52. cout << city << ", " << state << ", " << postal_code << endl;
  53. }
  54.  
  55. bool Address::comes_before( Address other)const
  56. {
  57.  
  58. return postal_code < other.postal_code;
  59.  
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65. //creating an object of 'Address' for a house
  66. Address aHouse(100, "N.University Dr.", "Edmond", "OK", 73034);
  67.  
  68. //creating another object of 'Address' for an apartment
  69. Address anApartment(201, "N.E. 89th St.", 305, "Oklahoma City", "OK", 73134);
  70.  
  71. cout << " Comparing Address\n\n";
  72. a.House.print();
  73.  
  74. cout << "\nwith address\n\n";
  75.  
  76. anApartment.print();
  77.  
  78. if(a.House.comes_before(anApartment))
  79.  
  80. cout << "\nThe HOUSE address comes before the APARTMENT address\n";
  81. else
  82. cout << "\nThe HOUSE address does o come before the APARTMENT address\n";
  83.  
  84.  
  85. // comparing again
  86.  
  87. if (anApartment.comes_before(aHouse))
  88.  
  89. cout << " \nThe APARTMENT address comes before the HOUSE address\n";
  90. else
  91. cout << " \nThe APARTMENT address does not come before the HOUSE address\n";
  92.  
  93. return 0;
<< moderator edit: added code tags: [code][/code] >>
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 16th, 2005
-1

Re: C++ program using classes

figured it out was using a string in the class for zip code instead of int.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005

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: serious crashing problem in Visual C++ 6.0
Next Thread in C++ Forum Timeline: C++ program using classes with account balances





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


Follow us on Twitter


© 2011 DaniWeb® LLC