944,062 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2964
  • C++ RSS
Nov 29th, 2007
0

Strings & Classes

Expand Post »
Menu.h

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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();
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Max_Payne is offline Offline
32 posts
since Oct 2007
Nov 29th, 2007
0

Re: Strings & Classes

Use
#include <string>
instead of
#include <cstring>
Reputation Points: 10
Solved Threads: 3
Newbie Poster
danzona is offline Offline
19 posts
since Nov 2007
Nov 29th, 2007
0

Re: Strings & Classes

Click to Expand / Collapse  Quote originally posted by danzona ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Max_Payne is offline Offline
32 posts
since Oct 2007
Nov 29th, 2007
0

Re: Strings & Classes

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 29th, 2007
0

Re: Strings & Classes

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.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
danzona is offline Offline
19 posts
since Nov 2007
Nov 29th, 2007
0

Re: Strings & Classes

<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>.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 21st, 2007
0

Re: Strings & Classes

Click to Expand / Collapse  Quote originally posted by Duoas ...
<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.
Reputation Points: 11
Solved Threads: 5
Newbie Poster
Aashath is offline Offline
18 posts
since Dec 2007
Dec 21st, 2007
0

Re: Strings & Classes

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 21st, 2007
0

Re: Strings & Classes

I would appreciate some help in this thread:
http://www.daniweb.com/forums/thread101965.html
Reputation Points: 10
Solved Threads: 0
Light Poster
Max_Payne is offline Offline
32 posts
since Oct 2007
Dec 21st, 2007
0

Re: Strings & Classes

Click to Expand / Collapse  Quote originally posted by Max_Payne ...
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.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006

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: how to compile a program
Next Thread in C++ Forum Timeline: strings with vs2008





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


Follow us on Twitter


© 2011 DaniWeb® LLC