两个构造函数(带和不带参数),没有输入 -> 没有参数运行。跳过上述类中的构造函数
Two constructors (with & without parameter), no input -> run without parameter. Skipping constructors in the classes above
现在我得到了一个带有多个子类的类。所有类都由一个构造函数组成,要求每个类中的数据输入(这是私有的,因此每个类都要求其自己的数据(。目前,我正在研究一个子类,该子类要索要一个名字。如果用户按ENTER(无输入(,它将在没有参数的情况下运行类的构造函数。如果有任何输入,它将将该输入输入到具有参数的类'构造器中。
我的问题是,每当我不投入输入时,都会问所有应该(从子类上方的类中(的所有问题,并且效果很好。但是,它跳过了名字的第一个字母。例如,我将"乔丹"写为名字,并将其保存为" ordan"。
第二,如果我不输入,它将使用没有参数的构造函数。它要求该名称(由"主要" Concontrctor(主类(询问。
我试图在" f"案件中形成一个物体:像鱼F1一样;这有效,但这不是我应该在这里做的(作业(。可以是什么?我认为可能是cin.ignore((;他妈的,但我需要在那里。
我很难解释,但是如果您使用以下代码进行以下操作(在控制台中写(。
- 您会被要求写" F"来添加鱼,写f。
- 写一个随机名称(不是空名(。
- 所有其他问题都将被回答,这不应该是。构造函数应该问问题 - 这是第一个问题。
,然后再开始新的:
- 写" F"来加一条鱼。
- 什么也没写,只需在被询问名称时按Enter。
- 写一个随机名称。
- 在淡水问题上写y或n,下一个问题也是如此。
- 您会看到名字丢失了第一字母。
这是代码(缩短(:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
using namespace std;
const int STRLEN = 20;
// FUNCTIONS
bool Question(); // User input y or n, sets the correct bool value
void Answer(bool n); // Changes true to yes, false to no
class Animal {
private:
char name[STRLEN];
public:
Animal() { cout << "nAnimal name: "; cin.ignore(); cin.getline(name, STRLEN); }
Animal(char nam[]) { strcpy(name, nam); } // Const with parameter, sent from Fish-class
void writeName() { cout << "Name: " << name << 'n'; }
};
class AnimalWater : public Animal {
private:
bool freshwater;
public:
AnimalWater() { cout << "Freshwater? (y/n): "; freshwater = Question(); }
AnimalWater(char name[]) : Animal(name) { } // Sendes name (used by Fish) up to Animal-class
void writeWater() { cout << "Freshwater? "; Answer(freshwater); cout << 'n'; }
};
class Fisk : public AnimalWater {
private:
bool haveGills;
public:
Fisk() { cout << "Does the fish have gills? (y/n): "; haveGills = Question(); }
Fisk(char name[]) : AnimalWater(name) { } // Send the Fish name up to Animal-class
void writeGills() { Answer(haveGills); }
};
int main() {
char choice;
cout << "nWrite F to add a fish: ";
cin >> choice; choice = toupper(choice);
while (choice != 'Q') { // If not Q(uit), run
switch (choice) {
case 'F': {
char fishname[STRLEN];
cout << "Name of the fish: "; cin.ignore();
cin.getline(fishname, STRLEN);
if (strlen(fishname) > 0) { // if user input > 0
Fisk f1(fishname); // sned fishname as parameter to the Fish constructor
f1.writeName(); f1.writeWater(); f1.writeGills();
}
else { // else use the constructor without parameter
Fisk f1; f1.writeName(); f1.writeWater(); f1.writeGills();
}
break;
}
}
cout << "nnWhat to do? ";
cin >> choice; choice = toupper(choice);
}
return 0;
}
bool Question() {
char answer; cin >> answer; answer = toupper(answer);
if (answer == 'Y') return true;
else return false;
}
void Answer(bool n) {
if (n) cout << "yes";
else cout << "no";
}
饮食首字母的问题发生了,因为您在Animal
的构造函数中吃了一个角色。调用cin.ignore()
忽略一个字符。如果您在cin >>
之后使用cin.getline()
,这是可以的,因为operator>>
在缓冲区中留下n
,并且cin.getline()
将读取空字符串。但是,在您的情况下,它不会发生。您应该在任何地方使用cin >>
方法。
此外,在大多数情况下,在模型的构造函数中获取用户的输入并不是一个好方法。
相关文章:
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在按值调用 (c++) 中转发构造函数参数
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 如何在构造函数参数中初始化"std::set"?
- 何时应在构造函数参数中使用 const C++?
- 使用模板化结构作为构造函数参数
- 使用 lambda 作为构造函数参数是否需要C++ 17?
- 如何使输入文本文件成为构造函数参数?c++
- shared_ptr构造函数参数是否应按值传递
- 是否允许使用初始值设定项列表将const数组引用实例化为构造函数参数
- 复制构造函数参数为0
- 用作成员构造函数参数的函数的求值顺序
- 结构中的默认成员值或默认构造函数参数
- C++将引用成员绑定到构造函数参数
- 如何通过可变参数模板将多个构造函数参数转发到数组初始值设定项列表?
- 模板函数指针参数与构造函数参数
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 如何基于构造函数参数模板化类成员函数的代码
- 将派生类构造函数参数传递给受保护的成员
- 如何根据构造函数参数使用超类类型初始化成员变量?