![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
C++ in Plain English
The CStr Class in SummaryWeve come a long way been through a lot with the CStr class, gradually building it up over the course of three chapters. Ive used the CStr class to introduce many of the core concepts in C++. If you understand these concepts, the CStr class is not difficult to write. You might question whether writing such a class is worth all the work involved. If you were going to create only a few instances of this class and then never use it again, you probably wouldnt want to spend the time to create such a class as CStr, with its feature set that includes memory management, multiple constructors, and built-in operators. But if you design such a class carefully enough, you can use it in many programs. If you think of the class as an extension to the C++ language, Cstr becomes highly cost-effective. You can define CStr objects in any program, as long as you do the following:
Here are the final contents of the CSTR.H file, declaring all the members of the CStr class presented until now. // CSTR.H Declaration of the CStr class class CStr { char *pData; int nLength; public: CStr(); // Constructors CStr(char *s); CStr(const CStr &str); ~CStr(); // Destructor char *get(void) const {return pData;} int getlength(void) const {return nLength;} void cpy(char *s); void cat(char *s); friend CStr operator+(CStr str1, CStr str2); friend CStr operator+(CStr str, char *s); friend CStr operator+(char *s, CStr str); CStr& operator=(const CStr &Source) {cpy(source.get()); return *this;} }; Three of the member functions are defined in the CStr declaration as inline functions. Because they are defined in the class declaration, they dont need to be defined in CSTR.CPP, the implementation file for the class. CSTR.CPP contains the remaining definitions. You would compile this code and link the resulting object file into any project that needed to use the class. // CSTR.CPPimplementation of CStr class #include cstr.h #include <string.h> #include <malloc.h> CStr::CStr() { pData = (char *) malloc(1); *pData = \0; nLength = 0; } CStr::CStr(char *s) { pData = (char *) malloc(strlen(s)); strcpy(pData, s); nLength = strlen(s); } CStr::CStr(CStr& s) { char* sz = s.get(); int n = s.getlength(); pData = (char *) malloc(n); strcpy(pData, sz); nLength = n; } CStr::~CStr() { free(pData); } void CStr::cpy(char *s) { // Copy string arg. int n; n = strlen(s); if (nLength != n) { if (pData) free(pData); pData = (char*) malloc(n + 1); nLength = n; } strcpy(pData, s); } void CStr::cat(char *s) { // Concatenate string arg int n; char *pTemp; n = strlen(s); if (n == 0) return; pTemp = (char*) malloc(n + nLength + 1); if (pData) { strcpy(pTemp, pData); free(pData); } strcat(pTemp, s); pData = pTemp; nLength += n; } CStr operator+(CStr str1, CStr str2) { CStr new_string(str1); new_string.cat(str2.get()); return new_string; } CStr operator+(CStr str, char *s) { CStr new_string(str); new_string.cat(s); return new_string; } CStr operator+(char *s, CStr str) { CStr new_string(s); new_string.cat(str.get()); return new_string; } Another Class Operator ExampleBefore leaving the subject of operator overloading, lets look at another example that uses operator functions: the CTemp_point class first introduced in Chapter 5. This class specifies a point on a three-dimensional grid, along with a temperature value. Some of the member functions are much easier to implement than CStr is, because the CTemp_point class does not involve memory allocation. class CTemp_point { int x, y, z; double temp; public: CTemp_point(); CTemp_point(int xx, yy, zz); CTempP_point(CTemp_point &pt); void set_point(int xx, yy, zz); void get_point(int *xx, *yy, *zz); void set_temp(double new_temp); double get_temp(void); CTemp_point operator+(double temp_diff); CTemp_point operator-(double temp_diff); CTemp_point& operator=(const CTemp_point &pt); }; When you design a class, one of the most important tasks is to decide which operations make sense. In the case of the CStr class, it made sense to define addition as an operation between two strings that produces another string. In the case of the CTemp_point class, it isnt so clear what addition should do. What would it mean to add together two points on a grid? (The operation would be meaningful if one of the points were interpreted as a vector or a size, but you might consider creating a separate vector class for that purpose.) For the CTemp_point class, I defined only one simple operation each for addition and subtraction. These operations add or subtract from the temperature data member (temp) and leave the grid coordinates untouched. Note that these functions support only the adding or subtracting of floating-point numbers (or values, such as integers, that the compiler knows how to convert to floating point): pt1 = pt2 + 33.3; // Ok; addition to flt pt defined pt2 = pt2 + 1; // Ok: 1 can be converted to // floating pt pt2 = ptl + pt3; // ERROR! Operation not defined
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |