Strings & Classes

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

Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Strings & Classes

 
0
  #1
Nov 29th, 2007
Menu.h

  1.  
  2. #pragma once
  3.  
  4. #include <iostream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. class Menu
  10. {
  11. private:
  12. string title;
  13.  
  14. public:
  15. Menu( void );
  16. Menu( const Menu &unMenu );
  17. ~Menu( void );
  18.  
  19. void setTitle ( string title );
  20. string getTitle() const;
  21. };


Menu.cpp

  1. #include "Menu.h"
  2.  
  3. Menu::Menu(void)
  4. {
  5. this->title = "Hello";
  6. }
  7.  
  8. Menu::Menu(const Menu &unMenu)
  9. {
  10. this->title = unMenu.title;
  11.  
  12. }
  13.  
  14. Menu::~Menu(void)
  15. {
  16. }
  17.  
  18. ///////////////////////////////////////////////////
  19.  
  20. void Menu::setTitle( string title )
  21. {
  22. this->title = title;
  23. }
  24.  
  25. string Menu::getTitle() const
  26. {
  27. return ( this->title );
  28. }

main.cpp

  1.  
  2. #include "Menu.h"
  3.  
  4. #include <iostream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. Menu m;
  12.  
  13. m.setTitle( "Spider Man 3" );
  14. cout << m.getTitle();
  15.  
  16. system("PAUSE");
  17. return 0;
  18. }

I'm getting a problem when i put
cout << m.getTitle();
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: danzona is an unknown quantity at this point 
Solved Threads: 3
danzona danzona is offline Offline
Newbie Poster

Re: Strings & Classes

 
0
  #2
Nov 29th, 2007
Use
#include <string>
instead of
#include <cstring>
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: Strings & Classes

 
0
  #3
Nov 29th, 2007
Originally Posted by danzona View Post
Use
#include <string>
instead of
#include <cstring>
That Fixed the Problem. Thanks

can you tell me what's the difference between
<cstring> & <string>

When do we use each one ?

I only know that <cstring> is more recent.

tx.
Last edited by Max_Payne; Nov 29th, 2007 at 2:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Strings & Classes

 
0
  #4
Nov 29th, 2007
cstring replaces string.h which is where a lot of the standard functions to manipulate C style strings (null terminated char array) are located. string is the header for the STL string class and it contains the functions to manipulate STL string objects.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: danzona is an unknown quantity at this point 
Solved Threads: 3
danzona danzona is offline Offline
Newbie Poster

Re: Strings & Classes

 
0
  #5
Nov 29th, 2007
The quick answer is that <cstring> is the C++ version of the old C header file string.h, while <string> is the std::string class from C++.

There is probably a long answer about the history of C and C++ and MFC and OLE, and it probably has something to do with Unicode but it is beyond my knowledge.

I'll take your word for it that <cstring> is newer, and it just goes to show that newer is not always better.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Strings & Classes

 
0
  #6
Nov 29th, 2007
<cstring> aka <string.h> is not more recent. It is the old C string stuff.

The only thing that is recent about it is the ability to refer to it as <cstring> instead of <string.h>.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 18
Reputation: Aashath is an unknown quantity at this point 
Solved Threads: 5
Aashath's Avatar
Aashath Aashath is offline Offline
Newbie Poster

Re: Strings & Classes

 
0
  #7
Dec 21st, 2007
Originally Posted by Duoas View Post
<cstring> aka <string.h> is not more recent. It is the old C string stuff.

The only thing that is recent about it is the ability to refer to it as <cstring> instead of <string.h>.
" <cstring> aka <string.h> is not more recent. It is the old C string stuff."

Is that the CString thats available in MFC is also the same the old C string stuff. Is Microsoft reinvented the wheel or its their own implementation.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Strings & Classes

 
0
  #8
Dec 21st, 2007
No, this is C++. Refer to old files like <string.h> as <cstring>.

Microsoft's CString is older than std::string, but far more recent than the old C-style string handling routines.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: Strings & Classes

 
0
  #9
Dec 21st, 2007
I would appreciate some help in this thread:
http://www.daniweb.com/forums/thread101965.html
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Strings & Classes

 
0
  #10
Dec 21st, 2007
Originally Posted by Max_Payne View Post
I would appreciate some help in this thread:
http://www.daniweb.com/forums/thread101965.html
The right place to ask about this is http://www.daniweb.com/forums/thread101965.html
The code is too big to read. Why dont you zip and upload the project and indicate the problem function. It will be easy to debug.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC