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

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

本文关键字:调用 默认值 对象 构造函数 复制 代码      更新时间:2023-10-16

我正在尝试做什么?

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

有什么问题?

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

我的代码:

#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: nn";
        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;
 }

您的复制构造函数不调用您的默认构造函数。

c 11介绍了委派构造函数的概念。构造函数可以在其自己的初始化列表中调用同一类的另一个构造函数。例如:

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

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

在较早的C 版本中,初始化列表只能调用直接基类(ES)和数据成员的构造函数,因此必须以单独的函数/方法执行构造函数主体可以根据需要调用的常见初始化。例如:

class Scooty {
    int cc;
    string model;
    string brand;
    void init() {
        cout << "n Enter the following scooty details:- nn";
        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;
}

在您的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: nn";
     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:- nn";
    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 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";
    }
}