两个构造函数(带和不带参数),没有输入 -> 没有参数运行。跳过上述类中的构造函数

Two constructors (with & without parameter), no input -> run without parameter. Skipping constructors in the classes above

本文关键字:构造函数 参数 运行 输入 两个 gt      更新时间:2023-10-16

现在我得到了一个带有多个子类的类。所有类都由一个构造函数组成,要求每个类中的数据输入(这是私有的,因此每个类都要求其自己的数据(。目前,我正在研究一个子类,该子类要索要一个名字。如果用户按ENTER(无输入(,它将在没有参数的情况下运行类的构造函数。如果有任何输入,它将将该输入输入到具有参数的类'构造器中。

我的问题是,每当我不投入输入时,都会问所有应该(从子类上方的类中(的所有问题,并且效果很好。但是,它跳过了名字的第一个字母。例如,我将"乔丹"写为名字,并将其保存为" ordan"。

第二,如果我不输入,它将使用没有参数的构造函数。它要求该名称(由"主要" Concontrctor(主类(询问。

我试图在" f"案件中形成一个物体:像鱼F1一样;这有效,但这不是我应该在这里做的(作业(。可以是什么?我认为可能是cin.ignore((;他妈的,但我需要在那里。

我很难解释,但是如果您使用以下代码进行以下操作(在控制台中写(。

  1. 您会被要求写" F"来添加鱼,写f。
  2. 写一个随机名称(不是空名(。
  3. 所有其他问题都将被回答,这不应该是。构造函数应该问问题 - 这是第一个问题。

,然后再开始新的:

  1. 写" F"来加一条鱼。
  2. 什么也没写,只需在被询问名称时按Enter。
  3. 写一个随机名称。
  4. 在淡水问题上写y或n,下一个问题也是如此。
  5. 您会看到名字丢失了第一字母。

这是代码(缩短(:

#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 >>方法。

此外,在大多数情况下,在模型的构造函数中获取用户的输入并不是一个好方法。