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?

6
  • 4
    Oh dear- using namespace std; and inheriting from a Standard class. Time to find a new teacher... Commented Aug 16, 2015 at 10:05
  • I didn't post that part up here, but I did type it in my code. Commented Aug 16, 2015 at 10:10
  • What did you type? New teacher? Or proper design? Commented Aug 16, 2015 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. Commented Aug 16, 2015 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. Commented Aug 16, 2015 at 10:18

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use inheriting constructors to inherit them.
0

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

1 Comment

But I still can't print out the class string_extend as the normal string class
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);

3 Comments

That's not what questions is about.
@MateuszGrzejek And yet it has been selected as the correct answer?
The question was about how to inherit all functions. I've said that all methods from class string have already been inherited and explained the principle of public inheritance.
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.

2 Comments

Sorry, i am just a beginner to c++. Could you tell me what this means? std::string::operator =( src );
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.