How to add additional member functions to existing C++ objects.

Hello there,

I'm not sure if what I'm trying to do is possible (I'd imagine it is). I am wanting to use the string class but I want to add additional functions to it.

I, effectively, want to be declaring a string but for it to have additional functions defined by me. I thought it may be possible by declaring my own class (gString) and inheriting from the string class and then I would define it as

string *sText = new gString;

Is this possible? Or am I simply dreaming? :P

Thanks for any help/advice anyone can give me. If you need further information to help you help me, just let me know.

Regards,
LJR
That's pretty much how inheritance works, so what you are planning to do would work. The only problem is with a pointer to string you won't be able to access your derived class's method.

You can also avoid making a new class and just make function that takes a string as a parameter.
Thanks for your post.

Yeah, I had thought that simply passing the string in as a parameter would be a lot easier- it's what I did to check the function was working in the first place! :) But I kinda had it in my head to make my class a header file that I could reuse in other projects without having to copy and paste the code everytime I wanted to use it.

I've decided to simply make the gString's only attribute a string, therefore I can do my string manipulation on that member, and now I have to set up my operators for it. :(

Btw, when you said,
The only problem is with a pointer to string you won't be able to access your derived class's method.


How could I do it differently to enable me to access my derived class's methods? Sorry if that's a dumb question, kinda new to C++ and its inheritance malarky.

Thanks again for your help.
Why don't you make a class that contains a std::string object as a data member? You can then implement the functions you want as member functions, and in order to save forwarding the std::string's functions you can simply have a member function that returns a reference to that member. Code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
using namespace std;

class MyString
{
    string str;

public:
    //constructors
    MyString():str(""){}
    MyString(const string & s):str(s){}
    MyString(const char * s):str(s){}
    MyString(const MyString & s):str(s.str){}

    //destructor
    ~MyString(){}

    //your functions
    void random_case()
    {
        int c;
        for (unsigned i=0; i<str.length(); i++)
        {
            c=rand()%2;

            switch (c)
            {
                case 0:
                str[i]=tolower(str[i]);

                break;
                case 1:
                str[i]=toupper(str[i]);
            }
        }
    }

    //std::string reference
    string & std_str()
    {
        return str;
    }
};

int main()
{
    srand(time(0));

    MyString my_string("asdf");
    cout << my_string.std_str() << endl;

    cout << "enter your name: ";
    getline(cin,my_string.std_str());

    my_string.random_case();

    cout << "hi there, " << my_string.std_str() << endl;
    cout << "your name is " << my_string.std_str().length();
    cout << " characters long" << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
But I kinda had it in my head to make my class a header file that I could reuse in other projects without having to copy and paste the code everytime I wanted to use it.

You can have a header with just functions.


To access derived class method you need to declare them as virtual in the base class.
You can cast the pointer but why don't just use de derived class.
either gString *p; or gString sText;
Last edited on
Thanks for the replies. I have actually implemented it the way yous have suggested lol

In fact, it's almost exactly the same as m4ster r0shi's example (apart from different class functions).

I've still got a lot to learn, but I think I'm getting my head around it :)

Thanks again.
Topic archived. No new replies allowed.