Whole document tree

Whole document tree

C++ Programming HOW-TO: String Class Varieties Next Previous Contents

3. String Class Varieties

The string class is the one of the most vital objects in programming, and string manipulations are most extensively used. There is a lot of varieties of string classes. Of course, you can build your own string class by simply inheriting from these string classes -

3.1 Multiple Inheritance - Sample Custom String class

As mentioned above, you can build your own custom string class from the pre-built classes by single or multiple inheritance. In this section we will build a sample custom string class by using multiple inheritance, inheriting the standard C++ library string class and the String class presented in Appendix A.

Start by downloading the sample file 'string_multi.h' from Appendix A .

That file is reproduced below:

// ******************************************************************
// Sample program to demonstrate constructing your own string class
// by deriving from the String class and stdlib's "string" class
// ******************************************************************


#include <string>
#include "String.h"
#include "StringBuffer.h"

using namespace std;  // required for MS Visual C++ compiler Version 6.0

// Important Notes: In C++ the constructors, destructors and copy 
//       operator are NOT inherited by the derived classes!!
//       Hence, if the operators like =, + etc.. are defined in
//       base class and those operators use the base class's contructors
//       then you MUST define equivalent constructors in the derived 
//       class. See the sample given below where constructors mystring(), 
//       mystring(char[]) are defined.
//       Also when you use operator as in atmpstr + mstr, what you are really
//       calling is atmpstr.operator+(mstr). The atmpstr is declared a mystring

class mystring:public String, string
                mystring():String() {}  // These are needed for operator=, +
                mystring(char bb[]):String(bb) {}  // These are needed for operator=, +
                mystring(char bb[], int start, int slength):String(bb, start, slength) {}
                mystring(int bb):String(bb) {}  // needed by operator+
                mystring(unsigned long bb):String(bb) {}  // needed by operator+
                mystring(long bb):String(bb) {}  // needed by operator+
                mystring(float bb):String(bb) {}  // needed by operator+
                mystring(double bb):String(bb) {}  // needed by operator+
                mystring(const String & rhs):String(rhs) {}  // Copy Constructor needed by operator+
                mystring(StringBuffer sb):String(sb) {}  // Java compatibility
                mystring(int bb, bool dummy):String(bb, dummy) {}  // for StringBuffer class

                int mystraa; // customizations of mystring
                int mystrbb; // customizations of mystring


Next Previous Contents