【问题标题】:create instances of the class by using different constructors使用不同的构造函数创建类的实例
【发布时间】:2016-03-02 21:10:39
【问题描述】:

我有一个不太明白的家庭作业问题。

向 Critter 类添加三个构造函数。每个构造函数都应该 还在屏幕上打印一条简单的信息消息,这样一个 可以查看何时以及调用了哪个构造函数。你应该可以 创建 Critter 类 1) 的实例而不提供任何 属性(应该将名称设置为“默认小动物”,高度 到 5 和其余到 0), 2) 只提供一个名称作为参数 (应该将高度设置为 5,其余设置为 0),以及 3)通过 提供姓名、饥饿、无聊和身高作为参数。你 也应该能够在没有的情况下创建 Critter 类的实例 指定高度。如果没有提供高度,则小动物有 默认高度为 10。编写一个测试程序,创建四个 通过使用这三个不同的构造函数的 Critter 实例 (两种方式的最后一种)。通过使用将他们的饥饿等级设置为 2 适当的方法和/或构造函数调用。小动物的属性 然后应该打印在屏幕上。

好的,所以首先我创建了一个类Critter,然后我在其中添加了 3 个构造函数,如第 1、2 和 3 点所述。然后我创建了一个对象或实例(它们是相同的东西,对吗?) .之后我创建了另一个对象并创建了另一个构造函数。问题是,我在最后 3 句话中迷失了:

编写一个测试程序,创建四个 通过使用这三个不同的构造函数的 Critter 实例 (两种方式的最后一种)。通过使用将他们的饥饿等级设置为 2 适当的方法和/或构造函数调用。小动物的属性 然后应该打印在屏幕上。

如何使用这三个不同的构造函数创建 4 个 Critter 实例?

这听起来像是一个愚蠢的问题,但我以前从未使用过课程。我是过程式编程的粉丝。


这是我的代码:

Critter.h

class Critter {

    // The following data members are private
    private:
        std::string name;
        int hunger, boredom;
        double height;

    public:

        Critter();
        Critter(std::string& newname);
        Critter(std::string& newname, int newhunger, int newboredom, double newheight);
        Critter(std::string& newname, int newhunger, int newboredom);

};

我在另一个文件中写道:

Critter.cpp

#include <iostream>
#include "Critter.h"

using namespace std;

Critter::Critter() {
    name = "default critter";
    height = 5.0;
    hunger = 0;
    boredom = 0;
    cout << "First with no properties." << endl;
}

Critter::Critter(string& newname) {
    name = newname;
    height = 5.0;
    hunger = 0;
    boredom = 0;
  cout << "Only name as a parameter." << endl;
}

Critter::Critter(string& newname, int newhunger, int newboredom, double newheight) {
    name = newname;
    height = newheight;
    hunger = newhunger;
    boredom = newboredom;
  cout << "All as parameters." << endl;
}

Critter::Critter(string& newname, int newhunger, int newboredom) {
    name = newname;
    height = 10.0;
    hunger = newhunger;
    boredom = newboredom;
  cout << "All as parameters." << endl;
}

和主文件:

#include <iostream>
#include "Critter.h"

using namespace std;

int main() {

    Critter first_instance, second_instance;

    string name;
    int hunger, boredom;
    double height;


    return 0;
}

期待您的建议/答案。

谢谢

【问题讨论】:

  • 示例:Critter second_instance{"bob", 0, 0, 3.0}; 。这将使用构造函数 Critter(string&amp; newname, int newhunger, int newboredom, double newheight)) 创建一个实例
  • Critter(std::string&amp; newname, int newhunger, int newboredom); 不是请求的一部分 - 应该总共有 3 个构造函数(你写的其他三个)
  • @M.M 这意味着我只需要创建一个对象并调用这些构造函数 4 次?谢谢
  • 不,您需要创建 4 个对象。每个对象只能调用一次构造函数:“调用构造函数”是“创建对象”的同义词。
  • @M.M 非常感谢!

标签: c++ class oop object constructor


【解决方案1】:

您已经创建了 4 个构造函数,但他们希望您拥有 3 个,其中最后一个应该以两种方式使用。前两个很好,他们想要你做的是通过使用default arguments 函数将你的第三个和第四个构造函数合并为一个。像这样:

Critter::Critter(string& newname, int newhunger, int newboredom, double newheight = 10.0) {
    name = newname;
    height = newheight;
    hunger = newhunger;
    boredom = newboredom;
  cout << "All as parameters." << endl;
}

这样它是一个构造函数,但你可以通过两种方式调用它:

  1. 包含所有参数,包括newheight,或
  2. 只提供前三个,然后newheight 将具有 默认值为 10.0。

编辑在评论中回答问题:

如何使用这 3 个构造函数创建 4 个实例?

线

Critter my_first_critter;

将创建一个名为 my_first_critter 的 Critter 类对象,不带任何参数,这意味着编译器将选择第一个构造函数,您将看到“First with no properties.”。相反,像

这样的一行
Critter my_second_critter("John");

将创建一个 Critter 类的新对象,称为my_second_critter,并且由于有一个字符串参数,编译器将选择第二个构造函数,因此您将阅读“仅将名称作为参数。”。然后,像

这样的一行
Critter my_third_critter("James", 2, 3, 5.5);

(注意第四个参数!)将再创建一个对象,同样属于 Critter 类并称为 my_third_critter,并且由于有四个参数,类型为:string、int、int、double,编译器将调用第三个构造函数,您将阅读“全部作为参数。”。最后,像

这样的一行
Critter my_fourth_critter("Peter", 2, 3);

(请注意缺少第四个参数!)将调用相同的函数,因为在这种情况下默认参数启动。编译器仍会将此调用匹配到第三个构造函数(因此您将再次阅读,“所有作为参数。”),但在这种情况下,高度将是默认值 10.0,因为创建对象的程序员(您)没有指定该值。

如果高度经常为 10.0 并且您不想在每次创建 Critter 时浪费时间指定此数字(这也是错误的),则将高度的默认值设置为 10.0 很有用容易:想想错别字或感到困惑......)。不过,如果您需要 10.0 以外的值,您可以轻松地提供它。而且由于它只是一个构造函数,而不是 2 个独立的构造函数,因此您需要管理的代码更少。当然,您可以避免使用默认参数并编写 2 个构造函数(就像您所做的那样),但是假设您在第三个构造函数中发现了一个错误:当然您也必须在第四个构造函数中修复它。如果你忘记了怎么办?如果你只有一个函数,你的代码更容易管理。此外,请考虑您可以为每个函数提供多个默认参数。如果您必须不使用默认参数,则必须创建大量副本。它不会很好地扩展。

【讨论】:

  • 感谢您的回答。我还有一个问题,问题是要创建四个实例,但我不明白为什么以及如何做到这一点;或者我不明白这个请求是什么意思。
  • @zeeks 我已经编辑了我的答案来解决你的问题。
  • @M.M 该死的,又是最令人头疼的解析。谢谢,我已经解决了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
相关资源
最近更新 更多