overloading problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

overloading problem

 
0
  #1
Oct 14th, 2005
hi, I've been staring at my program code forever now and I'm going in circle. I don't know what I've done wrong. Can anyone please help?
I'm supposed to design a month class that the following private members:
1) name: a MyString object that hold name of a month such as "January"
2) monthNumber: a int value that holds the number of month. January would be 1; Feb would be 2.

I have to have the following member functions:
1) a default constructor hat sets monthNumber to 1 and name to "January"
2) a constructor that accepts name of month as an argument. it should set name to the value passed as the argument and set monthNumber to the correct value
3) a constructor that accepts the number of month as an argument. it should set monthNumber to the value as the argument and set name to the correct month name
4) appropriate set and get functions for name and monthNumber
5) prefix and postfix overloaded ++ operator taht increment monthNumber and set name to the name of next month. EX: if monthNumber is set to 12, they should set monthNumber to 1 and name to "January"
6) prefix and postfix overloaded -- operator functions that decrement monthNumber and set name to the previous month. EX: if monthNumber is set to 1, they should set monthNumber to 12 and name to "December"

This is my code:

  1.  
  2. // Specification file for the MyString class
  3. #ifndef MYSTRING_H
  4. #define MYSTRING_H
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. class MyString; // Forward declaration.
  10. ostream &operator<<(ostream &, MyString &);
  11. istream &operator>>(istream &, MyString &);
  12.  
  13. // MyString class. An abstract data type for handling strings.
  14.  
  15. class MyString
  16. {
  17. private:
  18. char *str;
  19. int len;
  20. public:
  21. MyString() // Default constructor
  22. { str = NULL; len = 0; }
  23.  
  24. MyString(MyString &right) // Copy constructor
  25. { str = new char[right.length() + 1];
  26. strcpy(str, right.getValue());
  27. len = right.length(); }
  28.  
  29. MyString(char *sptr) // Constructor - initializes with
  30. { len = strlen(sptr); // a C-string.
  31. str = new char[len + 1];
  32. strcpy(str, sptr); }
  33.  
  34. ~MyString() // Destructor
  35. { if (len != 0) delete [] str; }
  36.  
  37. int length() // Returns the string length.
  38. { return len; }
  39.  
  40. char *getValue() // Returns the string.
  41. { return str; };
  42.  
  43. // Overloaded operators
  44. MyString operator+=(MyString &);
  45. char *operator+=(const char *);
  46. MyString operator=(MyString &);
  47. char *operator=(const char *);
  48. int operator==(MyString &);
  49. int operator==(const char *);
  50. int operator!=(MyString &);
  51. int operator!=(const char *);
  52. bool operator>(MyString &);
  53. bool operator>(const char *);
  54. bool operator<(MyString &);
  55. bool operator<(const char *);
  56. bool operator>=(MyString &);
  57. bool operator>=(const char*);
  58. bool operator<=(MyString &);
  59. bool operator<=(const char *);
  60. friend ostream &operator<<(ostream &, MyString &);
  61. friend istream &operator>>(istream &, MyString &);
  62. };
  63.  
  64. #endif

  1. //Month class
  2.  
  3. #ifndef MONTH_H
  4. #define MONTH_H
  5. #include "MyString.h"
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class Month
  10. {
  11. private:
  12. MyString name;
  13. int monthNumber;
  14. public:
  15. Month()
  16. { monthNumber = 1;
  17. name = "January";
  18. }
  19.  
  20. Month(char *month_pointer)
  21. {
  22. name = month_pointer;
  23. if (month_pointer == "January")
  24. monthNumber = 1;
  25. else if(month_pointer == "February")
  26. monthNumber = 2;
  27. else if(month_pointer == "March")
  28. monthNumber = 3;
  29. else if(month_pointer == "April")
  30. monthNumber = 4;
  31. else if(month_pointer == "May")
  32. monthNumber = 5;
  33. else if(month_pointer == "June")
  34. monthNumber = 6;
  35. else if(month_pointer == "July")
  36. monthNumber = 7;
  37. else if(month_pointer == "August")
  38. monthNumber = 8;
  39. else if(month_pointer == "September")
  40. monthNumber = 9;
  41. else if(month_pointer == "October")
  42. monthNumber = 10;
  43. else if(month_pointer == "November")
  44. monthNumber = 11;
  45. else if(month_pointer == "December")
  46. monthNumber = 12;
  47. }
  48.  
  49. Month(int x)
  50. {
  51. monthNumber = x;
  52. switch (monthNumber)
  53. {
  54. case 1:
  55. name = "January";
  56. break;
  57. case 2:
  58. name = "February";
  59. break;
  60. case 3:
  61. name = "March";
  62. break;
  63. case 4:
  64. name = "April";
  65. break;
  66. case 5:
  67. name = "May";
  68. break;
  69. case 6:
  70. name = "June";
  71. break;
  72. case 7:
  73. name = "July";
  74. break;
  75. case 8:
  76. name = "August";
  77. break;
  78. case 9:
  79. name = "September";
  80. break;
  81. case 10:
  82. name = "October";
  83. break;
  84. case 11:
  85. name = "November";
  86. break;
  87. case 12:
  88. name = "December";
  89. break;
  90. }
  91.  
  92. void setmonthName(char *month_pointer)
  93. { name = month_pointer;}
  94.  
  95. void setmonthNumber(int x)
  96. { monthNumber = x;}
  97.  
  98. MyString getmonthName()
  99. { return name;}
  100.  
  101. int getmonthNumber()
  102. { return monthNumber;}
  103.  
  104. Month operator++()
  105. {
  106. ++monthNumber;
  107. if(monthNumber == 13)
  108. monthNumber = 1;
  109. //Month();
  110. return *this;
  111. }
  112.  
  113. Month operator++(int)
  114. {
  115. Month temp(monthNumber);
  116. monthNumber++;
  117. if(monthNumber == 13)
  118. monthNumber = 1;
  119. Month(monthNumber);
  120. return temp;
  121. }
  122.  
  123. Month operator--()
  124. {
  125. --monthNumber;
  126. if(monthNumber == 0)
  127. monthNumber = 12;
  128. Month();
  129. return *this;
  130. }
  131.  
  132. Month operator--(int)
  133. {
  134. Month temp(monthNumber);
  135. monthNumber--;
  136. if(monthNumber == 0)
  137. monthNumber = 12;
  138. Month();
  139. return temp;
  140. }
  141.  
  142. };
  143.  
  144. ostream &operator << (ostream &strm, MyString &obj)
  145. {
  146. strm << obj.getmonthNumber << " is the number of the month." << endl;
  147. strm << obj.getmonthName << " is the name of the month." << endl;
  148. return strm;
  149. }
  150.  
  151. istream &operator >> (istream &strm, MyString &obj)
  152. {
  153. int x;
  154. cout << "Enter the month number: ";
  155. strm >> x;
  156. obj.setmonthNumber(x);
  157. return strm;
  158. }
  159.  
  160. #endif

  1.  
  2. //main program.cpp
  3.  
  4.  
  5. // Chapter 14
  6. // Programming Challenge 4: Month Class
  7. #include <iostream>
  8. #include "Month.h"
  9. #include "MyString.h"
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.  
  15. // Create a Month object
  16. Month m("October");
  17.  
  18. cout << m.getmonthName() << endl;
  19. cout << m.getmonthNumber() << endl << endl;
  20.  
  21. m.setName("July");
  22. cout << m.getmonthName() << endl;
  23. cout << m.getmonthNumber() << endl << endl;
  24.  
  25. m.setNumber(2);
  26. cout << m.getmonthName() << endl;
  27. cout << m.getmonthNumber() << endl << endl;;
  28.  
  29. // Test the prefix ++ operator.
  30. cout << ++m << endl;
  31.  
  32. // Test the postfix ++ operator.
  33. cout << m++ << endl;
  34. cout << m << endl << endl;
  35.  
  36. // Test the prefix -- operator.
  37. cout << --m << endl;
  38.  
  39. // Test the postfix ++ operator.
  40. cout << m-- << endl;
  41. cout << m << endl << endl;
  42.  
  43. return 0;
  44. }

I'm pretty sure that the problem lies within the Month class. And I'm sure that MyString.h MyString.cpp, and the main program.cpp are fine.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: overloading problem

 
0
  #2
Oct 14th, 2005
you didn't say what the problem is. But I see a few problems in the Month class. Whenever the month number changes the month name must also change, and whenever the month name changes the month number must also change. I suggest moving those two large switch statements in the two class constructors into the setmonthName() and setmonthNumber() functions, then call these functions from the constructors and each of the overloaded operators. This will keep the month number and month name class objects synchronized.

An alternate solution would be to not keep the month name anyplace in the class. Only set the month number. When the name is needed, it can easily be obtained from the month number. That way no synchronization is necessary. Change the function setMonthName() to set the month number based on the string passed into the function. There is no reason to keep a copy of that string. And change getMonthName() to return the month name string that is appropriate for the month number. An array of month names makes all that very simple and short -- no switch statement is needed. For example:
  1. std::string months[] = { "January","February" ...}
  2. // assume n >= 1 and n <= 12
  3. string getMonthName(int n) {return months[n-1];}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: overloading problem

 
0
  #3
Oct 14th, 2005
Originally Posted by Ancient Dragon

An alternate solution would be to not keep the month name anyplace in the class. Only set the month number. When the name is needed, it can easily be obtained from the month number. That way no synchronization is necessary. Change the function setMonthName() to set the month number based on the string passed into the function. There is no reason to keep a copy of that string. And change getMonthName() to return the month name string that is appropriate for the month number. An array of month names makes all that very simple and short -- no switch statement is needed. For example:
  1. std::string months[] = { "January","February" ...}
  2. // assume n >= 1 and n <= 12
  3. string getMonthName(int n) {return months[n-1];}
Hi,
If I do it this way, then there is no need for the MyString class, right? I'm supposed to make name an MyString object.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: overloading problem

 
0
  #4
Oct 14th, 2005
Originally Posted by Ancient Dragon
you didn't say what the problem is.
This is the errors my compiler (MV C++ 6.0) gives me:
--------------------Configuration: Chapter 14 testing - Win32 Debug--------------------
Compiling...
testing.cpp
x:\dtran5.pds\chapter 14 testing\month.h(143) : error C2804: binary 'operator <<' has too many parameters
x:\dtran5.pds\chapter 14 testing\month.h(143) : error C2333: '<<' : error in function declaration; skipping function body
x:\dtran5.pds\chapter 14 testing\month.h(150) : error C2804: binary 'operator >>' has too many parameters
x:\dtran5.pds\chapter 14 testing\month.h(150) : error C2333: '>>' : error in function declaration; skipping function body
x:\dtran5.pds\chapter 14 testing\testing.cpp(5) : error C2059: syntax error : 'PCH creation point'
x:\dtran5.pds\chapter 14 testing\testing.cpp(6) : error C2238: unexpected token(s) preceding ';'
x:\dtran5.pds\chapter 14 testing\testing.cpp(8) : error C2059: syntax error : 'PCH creation point'
x:\dtran5.pds\chapter 14 testing\testing.cpp(9) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
x:\dtran5.pds\chapter 14 testing\testing.cpp(43) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

testing.obj - 9 error(s), 0 warning(s)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: overloading problem

 
0
  #5
Oct 14th, 2005
Originally Posted by karen_CSE
Hi,
If I do it this way, then there is no need for the MyString class, right? I'm supposed to make name an MyString object.
Then nix my suggestion.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: overloading problem

 
0
  #6
Oct 14th, 2005
Originally Posted by karen_CSE
This is the errors my compiler (MV C++ 6.0) gives me:
month class is missing a close bracket } in the constructor that takes an integer. That is causing all the other errors.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC