1

I am getting stuck with a homework which requires a self-build class called string_extend that inherits from class string. The string_extend class below is my code, and the main() part is the requested part for homework.

class string_extend:public string{
    public:
        string_extend(const string& str):string(str){}

};
int main(){
    string_extend a("amd");
    string_extend b(a);
    cout<<a<<b;
}

Could anyone give any hint about how to inherit all the functions from class string?

jombo
  • 67
  • 7
  • 4
    Oh dear- using namespace std; and inheriting from a Standard class. Time to find a new teacher... – Puppy Aug 16 '15 at 10:05
  • I didn't post that part up here, but I did type it in my code. – jombo Aug 16 '15 at 10:10
  • What did you type? New teacher? Or proper design? – Mateusz Grzejek Aug 16 '15 at 10:12
  • @jombo Puppy figured out that you have `using std` elsewhere in your code. His comment meant to say that it is not a good practice; neither is inheriting from a type defined in the Standard C++ library. – Sergey Kalinichenko Aug 16 '15 at 10:13
  • Sorry, I didn't post the full code. I meant that I have typed the include...and using namespace...but I only posted the part I am confused with here. My apology for misguiding. – jombo Aug 16 '15 at 10:18
  • And this is the homework. Thanks for comment from puppy, but I still have to finish it:-) – jombo Aug 16 '15 at 10:19

4 Answers4

2

Could anyone give any hint about how to inherit all the functions from class string?

Your code does that already. However, your main is not using any of string's member functions; it uses constructors, which are not inherited, unless you tell the compiler otherwise (see below).

In order to use a constructor from the base class you need to define a constructor with the same signature in your derived class. You did that for a constructor taking a string&, but not for other constructors that your main is using.

In C++03 it is done the way you did with the first constructor, i.e.

string_extend(const char* str):string(str){}

demo 1.

In C++11 you can inherit constructors:

class string_extend:public string{
    public:
        using string::string;
};

demo 2.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

By declaring string as the base class publicly, your class already inherits methods from string.

aslg
  • 1,966
  • 2
  • 15
  • 20
0

Your class have already inherited methods from class string. You can use methods like append or insert.

Public inheritance implies that all public methods from string have become public in your class, protected and private are also the same in your class.

So you can use your objects like this:

string_extend obj("Hello world");
string_extend obj2("Test");
obj.append(obj2);
Stivius
  • 72
  • 7
0

You can access member functions of class std::string without redeclaration them in the derived class.

A simplified version of the class can look the following way

#include <iostream>
#include <string>

int main()
{
    class string_extend : public std::string
    {
    public:
        using std::string::string;
        using std::string::operator =;

        string_extend() : std::string() {}
        string_extend( const string_extend &src ) : std::string( src ) {}
        string_extend & operator =( const string_extend &src )
        {
            std::string::operator =( src );

            return *this;
        }
    };

    string_extend s1;
    string_extend s2( { 'A', 'B', 'C' } );
    string_extend s3( "Hello" );
    string_extend s4( 8, '*' );

    std::cout << s1.size() << std::endl;
    std::cout << s2.size() << std::endl;
    std::cout << s3.size() << std::endl;
    std::cout << s4.size() << std::endl;

    std::cout << "\"" << s1 << "\"" << std::endl;
    std::cout << "\"" << s2 << "\"" << std::endl;
    std::cout << "\"" << s3 << "\"" << std::endl;
    std::cout << "\"" << s4 << "\"" << std::endl;

    s4 += { 'A', 'B', 'C' };

    std::cout << "\"" << s4 << "\"" << std::endl;
}    

The program output is

0
3
5
8
""
"ABC"
"Hello"
"********"
"********ABC"

If you want you can add also the move constructor and the move assignment operator.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Sorry, i am just a beginner to c++. Could you tell me what this means? std::string::operator =( src ); – jombo Aug 18 '15 at 08:43
  • @jombo It is an assignment operator overloaded in class std::string. For example you can write: std::string s; s = "ABC"; in the last statement there is used an assignment operator. – Vlad from Moscow Aug 18 '15 at 09:01