【发布时间】:2015-07-05 13:26:51
【问题描述】:
我有一个问题,我不知道从这里去哪里。
在这个程序中,您将创建一个名为 mystring 的类,它是从类派生的 细绳。类 mystring 应包括:
一个私有数据成员id,是一个整数,代表一个字符串的ID(见函数main()中的例子)。
一个公共方法,构造函数mystring(int, char *),它有两个 参数:一个整数 (int) 和一个字符串 (char *)。它应该(1)调用基类 使用字符串参数 (char *) 和 (2) 分配整数参数 (int) 的构造函数 到身份证。
一个公共方法int getid(),它返回类mystring的id。
这就是我目前所拥有的
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
【问题讨论】: