Whats wrong with this class???

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

Join Date: Aug 2005
Posts: 2
Reputation: neologic is an unknown quantity at this point 
Solved Threads: 0
neologic neologic is offline Offline
Newbie Poster

Whats wrong with this class???

 
0
  #1
Aug 23rd, 2005
  1. class String
  2. {
  3. char* pData;
  4. public:
  5. String(); //default constructor
  6. ~String(); //default destructor
  7. String( String&); //copy constructor
  8. const String& operator=( String&); //overloaded assignment operator
  9. void operator==( const String&); //overloaded compare operator
  10. String operator+( String); //overloaded concat operator
  11. void operator[]( int); //index operator
  12. String( char*); //one argument operator
  13. char* GetBuffer() { return pData;} //to get pData
  14. inline int Size(); //to find the length of pData;
  15. void ToUpper(); //convert to uppercase
  16. void ToLower(); //convert to lowercase
  17. void SubString( int, int); //to get a sub string
  18. void LeftTrim(); //to trim spaces on left
  19. //void RightTrim(); //to trim spaces on right
  20.  
  21. };
  22.  
  23. String::String()
  24. {
  25. //cout << "Constructor";
  26. pData = NULL;
  27. }
  28.  
  29. String::~String()
  30. {
  31. //cout << "Destructor";
  32. delete pData;
  33. }
  34.  
  35. String::String( String& strn1)
  36. {
  37. //cout << "Copy Constructor" << endl;
  38. int len1;
  39. len1 = strn1.Size();
  40. if ( pData)
  41. delete pData;
  42. pData = new char[len1];
  43.  
  44. strcpy( pData, strn1.pData);
  45. }
  46.  
  47. const String& String::operator=( String& strn2)
  48. {
  49. //cout << "Overloaded = Operator" << endl;
  50. int len2;
  51. if( this == &strn2)
  52. return *this;
  53.  
  54. len2 = strn2.Size();
  55.  
  56. if( pData)
  57. delete pData;
  58. pData = new char[len2];
  59.  
  60. strcpy( pData, strn2.pData);
  61. return *this;
  62. }
  63.  
  64. void String::operator==( const String& strn3)
  65. {
  66. //cout << "Inside ==";
  67. if( *strn3.pData == *pData)
  68. cout << "The strings match" << endl;
  69. else
  70. cout << "The strings dont match" << endl;
  71. }
  72.  
  73. String String::operator+( String strn4)
  74. {
  75. int len3, plen, totlen;
  76. len3 = strn4.Size();
  77. plen = Size();
  78. totlen = len3 + plen;
  79. char* temp = new char[totlen];
  80.  
  81. for ( int i = 0; i < plen; i++)
  82. {
  83. temp[i] = pData[i];
  84. }
  85. if( pData)
  86. delete pData;
  87. pData = new char[totlen];
  88. for ( int i = 0; i < len3; i++)
  89. {
  90. temp[plen + i] = strn4.pData[i];
  91. }
  92. String newtemp( temp);
  93. return newtemp;
  94. /*
  95.   for ( int i = 0; i < totlen; i++)
  96.   {
  97.   cout << temp[i];
  98.   } */
  99. cout << endl;
  100. }
  101.  
  102. void String::operator[]( int posi)
  103. {
  104. char ch;
  105. ch = pData[posi - 1];
  106. cout << ch << endl;
  107. }
  108.  
  109. String::String( char* strn5)
  110. {
  111. //cout << "One arg Constructor" << endl;
  112. pData = strn5;
  113. }
  114.  
  115. inline int String::Size()
  116. {
  117. return strlen(pData);
  118. }
  119.  
  120. void String::ToUpper()
  121. {
  122. int len4;
  123. len4 = Size();
  124. char* temp = new char[len4];
  125. temp = pData;
  126. char* ch = new char[len4];
  127.  
  128.  
  129.  
  130. for( int i = 0; i < len4; i++)
  131. {
  132. ch[i] = toupper(*temp);
  133. temp++;
  134. }
  135. for( int i = 0; i < len4; i++)
  136. {
  137. cout << ch[i];
  138. }
  139. }
  140.  
  141. void String::ToLower()
  142. {
  143. int len5;
  144. len5 = Size();
  145. char* ch = new char[len5];
  146.  
  147. for ( int i = 0; i < len5; i++)
  148. {
  149. ch[i] = tolower(*pData);
  150. pData++;
  151. }
  152. for( int i = 0; i < len5; i++)
  153. {
  154. cout << ch[i];
  155. }
  156. }
  157.  
  158. void String::SubString( int pos, int range)
  159. {
  160. int len6;
  161. len6 = Size();
  162. char* str = new char[len6];
  163.  
  164. str = pData;
  165. for( int i = pos - 1; i < range + pos - 1; i++)
  166. {
  167. cout << str[i];
  168. }
  169. cout << endl;
  170. }
  171.  
  172. void String::LeftTrim()
  173. {
  174. int count = 0, len7;
  175. len7 = Size();
  176. char *pTmp = new char[Size()];
  177. pTmp = pData;
  178. if( pData)
  179. delete pData;
  180. for( int i = 0; i < len7; i++)
  181. {
  182. if( *pTmp == ' ')
  183. {
  184. *pTmp++;
  185. count++;
  186. }
  187. }
  188. for( int i = 0 ; i < len7 - count; i++)
  189. {
  190. cout << pTmp[i];
  191. }
  192. cout << endl;
  193. }
  194.  
  195. void String::RightTrim()
  196. {
  197. int count = 0, len8;
  198. len8 = Size();
  199. char *pTmp = new char[Size()];
  200. pTmp = (pData + len8);
  201. if( pData)
  202. delete pData;
  203. for( int i = len8; i > 0; i--)
  204. {
  205. if( *(pTmp - 1) == ' ')
  206. {
  207. *pTmp--;
  208. count++;
  209. }
  210. }
  211. char *pnewTmp = new char[Size()];
  212. pnewTmp = pTmp;
  213. if( pTmp)
  214. delete pTmp;
  215. for( int i = 0; i < len8; i++)
  216. {
  217. cout << pnewTmp[i];
  218. }
  219. cout << endl;
  220. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Whats wrong with this class???

 
0
  #2
Aug 23rd, 2005
>Whats wrong with this class???
It has no point because the standard library already provides an excellent string class. That said, what is it not doing that you want it to? Don't expect us to test and debug your program just because you didn't give us enough information about your problem.

Though at a glance, your code is not exception safe because it leaves the object in an unpredictable state if any of your allocations fail, and you have a bunch of unnecessary member functions that could easily be non-members. And your index operator is...funky.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 2
Reputation: neologic is an unknown quantity at this point 
Solved Threads: 0
neologic neologic is offline Offline
Newbie Poster

Hey Sorry

 
0
  #3
Aug 23rd, 2005
Sorry guys did not post the questions....
I was trying to implement a String class similar to the one in Java in C++...Also i have used the bloodshe dev c++ compiler 4.0
I have a few questions regarding this:
1. I am passing a constant String reference to one of the methods and finding its size using Size()....but still its giving an error there saying constant functions cannot be modified.
2. There seems to be a problem in the overloaded operators....

cud some1 help me??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
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: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Hey Sorry

 
0
  #4
Aug 23rd, 2005
you need to make Size() method const
  1. inline int Size() const; //to find the length of pData;
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Whats wrong with this class???

 
0
  #5
Aug 23rd, 2005
If you are doing this as a learning exercise then I agree with narue. You need to think a little more about what would happen in the face of an exception when you design a class. Heres some thoughts from Herb Sutter . You would do well to heed most advice he gives. Read through the guru of the week exercises especially the ones about class design, exception safety and pimpl idiom. Then fix the problems with your code. Funky isnt really the word for your subscript operator. You should have two not one and neither returns a void. one returns const char the other returns char&.
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



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

©2003 - 2009 DaniWeb® LLC