Next Previous Contents

3. Usage of mychar class

The 'mychar class' is a complete replacement for char and char * datatype. You can use 'mychar class' just like char and get much more functionalities. You should include the library 'libmychar.a' which you can build from the makefile given in Appendix H and copy the library to /usr/lib directory where all the "C++" libraries are located. To use the 'libmychar.a' compile your programs like -


        g++ example.cpp -lmychar

See illustration sample code as given below -
        mychar aa;

        aa = " Washington DC is the capital of USA ";

        // You can use aa.val like a 'char *' variable in programs !!
        for (unsigned long tmpii = 0; tmpii < aa.length(); tmpii++)
        {
                fprintf(stdout, "aa.val[%ld]=%c ", tmpii, aa.val[tmpii]);
        }

        // Using pointers on 'char *' val ...
        for (; *aa.val != 0; aa.val++)
        {
                fprintf(stdout, "aa.val=%c ", *aa.val);
        }

A complete example program "example_mychar.cpp" implementing the mychar class is given in Appendix A and mychar class is given in Appendix B.

3.1 Operators

The 'mychar class' provides these operators :-

For example to use operators -
        mychar aa;
        mychar bb("Bill Clinton");

        aa = "put some value string";  // assignment operator
        aa += "add some more"; // Add to itself and assign operator
        aa = "My name is" + " Alavoor Vasudevan "; // string cat operator

        if (bb == "Bill Clinton")  // boolean equal to operator
                cout << "bb is eqaul to 'Bill Clinton' " << endl;

        if (bb != "Al Gore")   // boolean 'not equal' to operator
                cout << "bb is not equal to 'Al Gore'" << endl;

3.2 Functions

The 'mychar class' provides these functions :-

3.3 Miscellaneous Functions

Some miscellaneous mychar functions are given here, but DO NOT USE these, and instead use operators like '+', '+=', '==' etc.. These are 'private' members of the 'mychar' class.

For example to convert integer to string do -
        mychar  aa;

        aa = 34;  // The '=' operator will convert int to string
        cout << "The value of aa is : " << aa.val << endl;

        aa = 234.878;  // The '=' operator will convert float to string
        cout << "The value of aa is : " << aa.val << endl;

        aa = 34 + 234.878;
        cout << "The value of aa is : " << aa.val << endl;
        // The output aa will be '268.878'

        // You must cast mychar to convert
        aa = (mychar) 34 + " Honourable President Ronald Reagan " + 234.878;
        cout << "The value of aa is : " << aa.val << endl;
        // The output aa will be '34 Honourable President Ronald Reagan 234.878'


Next Previous Contents