为什么删除默认构造函数会在 C++ 代码编译中出错?
Why Removing the default constructor is giving error in the compilation of code in c++?
#include <iostream>
using namespace std;
class BankDeposit {
int principal;
int years;
float interestRate;
float returnValue;
public:
BankDeposit() { } //This is line number 12
BankDeposit(int p, int y, float r); // r can be a value like 0.04
BankDeposit(int p, int y, int r); // r can be a value like 14
void show();
};
BankDeposit::BankDeposit(int p, int y, float r) {
principal = p;
years = y;
interestRate = r;
returnValue = principal;
for (int i = 0; i < y; i++) {
returnValue = returnValue * (1 + interestRate);
}
}
BankDeposit::BankDeposit(int p, int y, int r) {
principal = p;
years = y;
interestRate = float(r)/100;
returnValue = principal;
for (int i = 0; i < y; i++) {
returnValue = returnValue * (1+interestRate);
}
}
void BankDeposit::show() {
cout << endl << "Principal amount was " << principal
<< ". Return value after " << years
<< " years is "<<returnValue << endl;
}
int main() {
BankDeposit bd1, bd2, bd3;
int p, y;
float r;
int R;
// bd1 = BankDeposit(1, 2, 3);
// bd1.show();
cout << "Enter the value of p y and R" << endl;
cin >> p >> y >> R;
bd2 = BankDeposit(p, y, R);
bd2.show();
return 0;
}
为什么删除或注释掉第 12 行中的代码会导致 运行 代码出错?
但据我所知,我们正在制作自己的构造函数,那么在代码中使用默认构造函数有什么必要呢?同样不包括代码中的默认构造函数是为什么给出错误?
BankDeposit bd1, bd2, bd3;
创建 class 对象的这一行需要一个构造函数,该构造函数应该是默认构造函数,因为您没有传递任何参数。
编辑:刚刚看到问题下的评论已经解释过了,而且这个问题可以通过阅读错误信息轻松解决。你可以在这里了解构造函数:here
如果您在 class 中实现参数化构造函数,规则是您还必须在 class 中具有默认构造函数。这就是您的代码失败的原因。
如果除了默认构造函数之外没有任何构造函数,即使代码中没有默认构造函数,代码也不会失败。
问题在于,首先您要通过创建参数化构造函数来覆盖编译器,但随后您要尝试创建不带参数的对象,为此您需要默认构造函数,因此您必须实现它。
#include <iostream>
using namespace std;
class BankDeposit {
int principal;
int years;
float interestRate;
float returnValue;
public:
BankDeposit() { } //This is line number 12
BankDeposit(int p, int y, float r); // r can be a value like 0.04
BankDeposit(int p, int y, int r); // r can be a value like 14
void show();
};
BankDeposit::BankDeposit(int p, int y, float r) {
principal = p;
years = y;
interestRate = r;
returnValue = principal;
for (int i = 0; i < y; i++) {
returnValue = returnValue * (1 + interestRate);
}
}
BankDeposit::BankDeposit(int p, int y, int r) {
principal = p;
years = y;
interestRate = float(r)/100;
returnValue = principal;
for (int i = 0; i < y; i++) {
returnValue = returnValue * (1+interestRate);
}
}
void BankDeposit::show() {
cout << endl << "Principal amount was " << principal
<< ". Return value after " << years
<< " years is "<<returnValue << endl;
}
int main() {
BankDeposit bd1, bd2, bd3;
int p, y;
float r;
int R;
// bd1 = BankDeposit(1, 2, 3);
// bd1.show();
cout << "Enter the value of p y and R" << endl;
cin >> p >> y >> R;
bd2 = BankDeposit(p, y, R);
bd2.show();
return 0;
}
为什么删除或注释掉第 12 行中的代码会导致 运行 代码出错? 但据我所知,我们正在制作自己的构造函数,那么在代码中使用默认构造函数有什么必要呢?同样不包括代码中的默认构造函数是为什么给出错误?
BankDeposit bd1, bd2, bd3;
创建 class 对象的这一行需要一个构造函数,该构造函数应该是默认构造函数,因为您没有传递任何参数。
编辑:刚刚看到问题下的评论已经解释过了,而且这个问题可以通过阅读错误信息轻松解决。你可以在这里了解构造函数:here
如果您在 class 中实现参数化构造函数,规则是您还必须在 class 中具有默认构造函数。这就是您的代码失败的原因。 如果除了默认构造函数之外没有任何构造函数,即使代码中没有默认构造函数,代码也不会失败。
问题在于,首先您要通过创建参数化构造函数来覆盖编译器,但随后您要尝试创建不带参数的对象,为此您需要默认构造函数,因此您必须实现它。