Sunday, July 12, 2009

How does C++'s string differ from C' string class?

How does C++'s string differ from C' string class?

How does C++'s string differ from C' string class?
C didn't have a string class, it had a header file called string.h which contained the definitions and methods required for working with strings. This header file has been ported to C++ as well.


Recently it was removed though from the standard because of compatibility and porting issues.
Reply:C does not have classes. It only had a convention. To make an array of characters and end the string with a 0 byte. Also there were functions to operate on strings but they were independent.





C++ actually has everything encapsulated and the convention of how the string data is managed can be changed at any time and not effect user code. So the class manages itself.
Reply:C doesn't have concept of "classes"


Let me put it this way:


C is a procedural language (i.e. emphasis is on procedures/functions). You have a set of functions for string manipulation, for convenience they put all related functions in a single file in this case it's string.h.





After C came C++ which is an object oriented language so you have a different way of keeping all related functions together viz through the concept of classes. So you have C++ classes which give you the same functionality (copy, append, compare...) in form of class member functions instead of the "C style free functions".





So in short


- "string.h of C" contains the string manipulation functions provided by C as a "set of free functions".


- "string classes of C++" (e.g. CString, std::string, RWCString, WCString) provide similar functionality as a "set of class member functions".
Reply:There is no C string class. Sorry.

forsythia

No comments:

Post a Comment