如何在以下 C++ 代码中对同一对象调用默认构造函数和复制构造函数?

How to call default and copy constructor on the same object in the following C++ code?

我在做什么?

我正在尝试在对象的默认构造函数中获取用户输入,如果前一个对象和当前对象具有相同的品牌名称,则在复制构造函数中进行比较。

问题是什么?

我无法调用同一对象的默认构造函数和复制构造函数。

我的代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

class Scooty {

    int cc ;
    string model, brand;
    bool goodBrands();

  public:

    Scooty () : cc(0), model(""), brand("") {

        cout << "\n Enter the following scooty details: \n\n";

        cout << "\n \t Brand: ";
        cin >> brand;
        transform(brand.begin(), brand.end(), brand.begin(), :: tolower);

        cout << "\n \t Model: ";
        cin >> model;
        transform(model.begin(), model.end(), model.begin(), :: tolower);

        cout << "\n \t CC: ";
        cin >> cc;
    }

    Scooty (Scooty &s) { if (brand == s.brand) cout << "You will get a discount!\n"; }

    void computeDataAndPrint ();
 };

 bool Scooty :: goodBrands() {

    if (brand == "honda" || brand == "tvs" || brand == "yamaha")
        return true;
    return false;
 }

 void Scooty :: computeDataAndPrint () {

    if (cc > 109 && goodBrands())
        cout << "\n Good Choice!\n";
    else
        cout << "\n Its okay!\n";
 }

 int main() {

    Scooty s;
    Scooty s1, s1(s)  // This gives error

    s.computeDataAndPrint();

    return 0;
 }

在您的 main() 函数中:

`Scooty s;  `   you create a variable `s` <br/>

Scooty s1, s1(s) // 你创建了一个变量,s1,现在你正试图创建另一个同名变量。这是一个错误

尝试:

Scooty s, s1;
S1 = Scooty(s)

我希望你知道如何去做。

正确的程序或至少工作...

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

class Scooty {
    int cc ;
    string model, brand;
    bool goodBrands();

public:

Scooty () {
     cc = 0, brand = "", model = "";
     cout << "\n Enter the following scooty details: \n\n";
     cout << "\n \t Brand: ";
     cin >> brand;
     transform(brand.begin(), brand.end(), brand.begin(), :: tolower); //STRING LOWER FUNCTION ..

     cout << "\n \t Model: ";
     cin >> model;
     transform(model.begin(), model.end(), model.begin(), :: tolower); //STRING LOWER FUNCTION ..

     cout << "\n \t CC: ";
     cin >> cc;
}
Scooty (Scooty &s) : Scooty() { //Initializing the object first then comparing, [using two constructors]
     if (brand == s.brand) cout << "You will get a discount!\n";
}
void computeDataAndPrint ();
};

bool Scooty :: goodBrands() {

   if (brand == "honda" || brand == "tvs" || brand == "yamaha")
     return true;
   return false;
}

void Scooty :: computeDataAndPrint () {
   if (cc > 109 && goodBrands())
       cout << "\n Good Choice!\n";
   else
       cout << "\n Its okay!\n";
}

int main() {
   Scooty s;               // Calling default constructor for constructor of S.
   //    s.computeDataAndPrint();
   Scooty s1(s);          // Calling copy constructor which firstly calls the default constructor then executes the copy constructors body.
   return 0;
  }

这个程序在逻辑上是不正确的,因为它违反了复制构造函数的用途规则。复制构造函数应该用于将一个对象复制到另一个对象,对于这样的操作,您应该重载 == 运算符。

构造函数只是将实例初始化为有效状态。首先,你没有问这个,但我会修改你的默认构造函数来简单地获取 cc、模型和品牌,并将所有 I/O 移动到一个单独的函数中:

class Scooty {
    // etc...

  public:
    // default constructor
    Scooty (int cc, string model, string brand) : cc(cc), model(model), brand(brand) {}

    // a correctly-written copy constructor (not necessary since the implicit copy constructor does exactly this)
    Scooty (const Scooty &s) : cc(s.cc), model(s.model), brand(s.brand) {}

    // etc...
 };


Scooty getScootyFromUserInput() {
    int cc;
    string brand, model;

    cout << "\n Enter the following scooty details:- \n\n";

    cout << "\n \t Brand:";
    cin >> brand;
    transform(brand.begin(),brand.end(), brand.begin(), :: tolower);

    cout << "\n \t Model:";
    cin >> model;
    transform(model.begin(),model.end(), model.begin(), :: tolower);

    cout << "\n \t CC:";
    cin >> cc;

    return Scooty(cc, brand, model);
}

您的 if (brand == s.brand) 语句当然不属于复制构造函数 - 当您创建副本时,品牌当然总是相同的。你应该做的是把它变成一个辅助函数,或者一个 class 方法来检查折扣是否适用:

class Scooty {
  public:
    bool checkDiscount(const Scooty &) const;
}

bool Scooty::checkDiscout(const Scooty &scooty) const {
    return brand == scooty2.brand;
}

然后像这样写你的主要功能:

int main() {
    Scooty s = getScootyFromUserInput();
    Scooty s1 = getScootyFromUserInput();

    if (s.checkDiscout(s1)) {
        cout << "You will get a discount!\n";
    }
}

你的复制构造函数没有调用你的默认构造函数。

C++11 引入了委托构造函数 的概念。一个构造函数可以在它自己的初始化列表中调用同一个 class 的另一个构造函数。例如:

Scooty (const Scooty &s)
    : Scooty() // <-- add this
{
    if (brand == s.brand)
        cout << "You will get a discount!\n";
}

这允许构造函数利用由相同 class 的另一个构造函数执行的初始化。

在早期版本的C++中,初始化列表只能调用直接基class(es)和数据成员的构造函数,所以公共初始化必须在单独的function/method中进行,构造函数体可以根据需要调用。例如:

class Scooty {
    int cc;
    string model;
    string brand;

    void init() {
        cout << "\n Enter the following scooty details:- \n\n";
        cout << "\n \t Brand:";
        cin >> brand;
        transform(brand.begin(), brand.end(), brand.begin(), ::tolower);
        cout << "\n \t Model:";
        cin >> model;
        transform(model.begin(), model.end(), model.begin(), ::tolower);
        cout << "\n \t CC:";
        cin >> cc;
    }

    ...

public:
    Scooty () : cc(0) {
        init(); // <-- here
    }

    Scooty (const Scooty &s) : cc(0) {
        init(); // <-- here
        if (brand == s.brand)
            cout << "You will get a discount!\n";
    }

    ...
};

int main() {
    Scooty s;
    Scooty s1(s);
    ...
    return 0;
}