The string class is the most vital object in programming, and string manipulations are most extensively used and they comprise of 20 to 60% of total code. There are 3 variety of string classes. Ofcourse, you can build your own string class by simply inheriting from these string classes -
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 from the GNU string class (Standard C++ Library) and the string class presented in Appendix H.
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 // ****************************************************************** #ifndef __STRING_MULTI_H_ #define __STRING_MULTI_H_ #include <string> #include "String.h" // 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 { public: 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 private: int mystrbb; // customizations of mystring }; #endif // __STRING_MULTI_H_