![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
C++ in Plain English
Chapter 8
|
Table 8.1 Functions and operations of the CStr class. | |
Function or Operation | Description |
---|---|
get | Return pointer to null-terminated string data. |
getlength | Return length of string data. |
cpy | Copy string from char* argument. |
cat | Concatenate char* argument onto current string data. |
+ | Concatenate two strings, of which at least one is a CStr object. The other may be a CStr object or a char* |
= | Copy string data from another CStr object. |
The class also supports a number of useful constructors. Lets assume that youd like to retain all this functionality as well as add the following:
Deriving a class through inheritance makes it easy to add this functionality without access to the original CStr code and without having to rewrite any of the CStr class. The next section introduces the syntax.
Assume that a class Parent_class has already been declared. The following syntax derives Class_class from Parent_class; this means that Child_class has all the members defined in Parent_class in addition to any declarations of its own.
class Child_class:public Parent_class { declarations };
The only part that is new here is the colon {:} and the syntax that follows:
public Parent_class
In this context, public is the base-class access specifier. Alternatively, you can specify either private or protected. In the majority of cases, public suffices. For information about the effect of the other keywords, see the topic Base-Class Access in Part II.
The declarations for Child_class include the new members to be added to the declarations in Parent_class. For example, the following declaration creates CStr as a class derived from CStr. CIoStr contains all the CStr members in addition to the member functions input and output.
// CIOSTR.H declaration of the CIOStr class #include cstr.h class CIoStr : public CStr { public: void input(void); void output(void); };
Note that the file CSTR.H is included, which results in CStr being declared before CIoStr. A class must be declared before you can derive another class from it.
Given this declaration of the CIoStr class, you can use CIoStr to declare objects just as you can with CStr. These objects support two new functionsinput and outputin addition to all the functions supported by a CStr object. For example:
CIoStr iostring1, iostring2, iostring3; iostring1 = This is a new string; iostring2 = iostring1 + .; iostring2.output(); iostring1.input(); iostring3.cpy(iostring1);
CIoStr follows the same rules that any class follows for implementing member functions: for each member function, you can either define the function inside the class declaration (in which case it is automatically an inline function), or you can define the function outside the declaration. In the latter case, you use CIoStr:: to prefix the name.
Previous | Table of Contents | Next |
![]() |
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. |