父类默认构造函数由用户然后通过C++输入
parent class default constructor to be enter by user then through C++
int 我的子类,当我使用继承和设置默认值时,: UserAccout("user","pass");
是否可以从cin
获取输入?
以下是我的代码示例。
我有我的父类
class UserAcct
{
private:
string userName;
string userPassword;
public:
UserAcct(string newUserName, string newPassword);
~UserAcct();
};
这是用户Acct.cpp
UserAcct::UserAcct(string newUserName, string newPassword)
{
userName = newUserName;
userPassword = newUserName;
}
可以这么说,这是我的孩子班
class GameSettings : public UserAcct
private:
ofstream odataBase("gameSettings.txt", ios::app);
ifstream idataBase("gameSettings.txt", ios::app);
int settingSet;
public:
GameSettings(int newSettings);
儿童班.cpp
GameSettings::GameSettings (int newSettings) : UserAcct("user","pass")//this right here
{
settingsSet = newSettings;
}
附言。由于某种原因,继承不起作用,我不确定为什么。在子类下.cpp在": UserAcct("user","pass");
"中的:
之前,我收到一个错误说
错误:应为"{"
问题 1:
您可以通过进行函数调用从cin
获取输入。
GameSettings::GameSettings (int newSettings) : UserAccout(getUserName(), getPassword()) {}
其中getUserName()
可以是GameSettings
的static
成员函数或非成员全局函数。
static std::string getUserName()
{
std::string name;
cin >> name;
return name;
}
getPassword()
可以是类似的函数。
问题2:
您看到一个编译器错误,因为您以 ;
结束了以下内容。
GameSettings::GameSettings (int newSettings) : UserAccout("user","pass");
^^^
;
需要替换为{}
和函数主体内部的任何其他内容。
PS您正在使用UserAcct
和UserAccout
。这需要解决。也许你应该把它说出来,以避免将来的混乱。使用 UserAccount
。
您的实现需要一个主体:
GameSettings::GameSettings (int newSettings) : UserAccout("user","pass") { }
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type