inheritance problem

im asked to create a class and have it be publicly inherited by the string class. The way I have thought about this is that because my new class will inherit all protected and public members of the string class, any object I create will essentially be a string. If that's incorrect, please tell me what is right.

Assuming Im right though, I should be able to do this:

1
2
3
newClass object = "Hello";

cout << object;


If I have to overload the ofstream operator, thats fine I can do that. But Im more concerned about what I said at the top. If I'm wrong, I would like to know just how to make that small segment of code work. Thank you
don't inherite from an stl container. They're not designed for that. I.e. they have no virtual members. What gain do you think you get?

if newClass is inherited from string you need to overwrite each and every constructor and operator= since they are hidden.

Yes better make string a part of your class and overload the operator<< since that's the way it's meant to be
^^ thats what my assignment is though. I have to inherit from the string class. If the answer to my problem is overloading some operators then thats ok with me.

Also my teacher says not to use any strings as variables in my new Class. What can I do?
Do what I said: overwrite the constructor and the operator=(). For C strings it would look like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
class newClass : public std::string
{
public:
  newClass(const char *str) : std::string(str)
  {
  }

  newClass &operator=(const char *str)
  {
    std::string::operator=(str);
    return *this;
  }
};
Last edited on
Topic archived. No new replies allowed.