Class 来自字符串
Class derived from string
我遇到了一个问题,我不确定从这里该何去何从。
In this program, you are going to create a class called mystring, which is derived from class
string. Class mystring should include:
A private data member id, which is an integer, representing the ID of a string (see example in function main()).
A public method, constructor mystring(int, char *), which has two
parameters: an integer (int) and a string (char *). It should (1) call base class
constructor using the string parameter (char *) and (2) assign integer parameter (int)
to id.
A public method int getid(), which returns id of class mystring.
这是我目前所拥有的
class mystring : public string
{
private:
int id;
public:
mystring(int id, char *words);
int getid();
};
mystring::mystring(int id, char *words)
{
string a (words);
this->id = id;
}
int mystring::getid()
{
return id;
}
// If your class is implemented correctly, the main program should work as
//the followings
int main()
{
mystring x(101, "Hello Kitty"); // “hello Kitty” has an ID 101
cout << x.getid() << endl; //display 101
cout << x << endl; //display string value: “Hello Kitty”
cout << x.length() << endl; //display length of the string: 11
return 0;
}
我明白了
101
0
在您的构造函数中,您没有为您的实例调用 std::string
基本构造函数,您只是创建一个本地字符串,然后将其丢弃。
改变这个:
mystring::mystring(int id, char *words)
{
string a (words); //creates a local string called a
this->id = id; //initializes id
}
要使用初始化列表,像这样:
mystring::mystring(int id, char *words) :
string(words), //calls the string base constructor
id(id) //initializes id
{}
我遇到了一个问题,我不确定从这里该何去何从。
In this program, you are going to create a class called mystring, which is derived from class string. Class mystring should include:
A private data member id, which is an integer, representing the ID of a string (see example in function main()).
A public method, constructor mystring(int, char *), which has two parameters: an integer (int) and a string (char *). It should (1) call base class constructor using the string parameter (char *) and (2) assign integer parameter (int) to id.
A public method int getid(), which returns id of class mystring.
这是我目前所拥有的
class mystring : public string
{
private:
int id;
public:
mystring(int id, char *words);
int getid();
};
mystring::mystring(int id, char *words)
{
string a (words);
this->id = id;
}
int mystring::getid()
{
return id;
}
// If your class is implemented correctly, the main program should work as
//the followings
int main()
{
mystring x(101, "Hello Kitty"); // “hello Kitty” has an ID 101
cout << x.getid() << endl; //display 101
cout << x << endl; //display string value: “Hello Kitty”
cout << x.length() << endl; //display length of the string: 11
return 0;
}
我明白了
101
0
在您的构造函数中,您没有为您的实例调用 std::string
基本构造函数,您只是创建一个本地字符串,然后将其丢弃。
改变这个:
mystring::mystring(int id, char *words)
{
string a (words); //creates a local string called a
this->id = id; //initializes id
}
要使用初始化列表,像这样:
mystring::mystring(int id, char *words) :
string(words), //calls the string base constructor
id(id) //initializes id
{}