如何在以下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: 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";
}
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 具有默认值的引用获取函数
- 当给定默认值时,为什么此模板参数推导失败
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 格式化浮点值:返回默认值
- 如何将数组部分初始化为某个默认值?
- 如何在C++中调用默认模板功能参数?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 创建一个包含 c++ 默认值的环境文件
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- Makefile g++ 使用命令行中的 -D 变量进行编译,默认值
- Switch 语句(字符串)一直选择默认值,除非其为零
- 作为参数的空初始值设定项列表不调用默认构造函数
- 调用类型的标记构造函数(如果可用),否则为默认值
- 如何在以下C 代码中在同一对象上调用默认值和复制构造函数
- 为模棱两可的超载功能调用创建默认值
- 为什么 C++ Map 的 [] 运算符调用映射值的默认构造函数
- 即使类没有定义构造函数,也不能在类上调用c++默认的move赋值操作符
- C++从重载的复制赋值运算符中调用默认的复制赋值操作符
- 如何处理(深度)嵌套函数调用中的默认值