Reputation: 67
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?
Upvotes: 1
Views: 257
Reputation: 310980
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.
Upvotes: 0
Reputation: 726569
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){}
In C++11 you can inherit constructors:
class string_extend:public string{
public:
using string::string;
};
Upvotes: 2
Reputation: 72
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);
Upvotes: 0
Reputation: 1974
By declaring string as the base class publicly, your class already inherits methods from string.
Upvotes: 0