【发布时间】:2020-10-25 00:30:34
【问题描述】:
我是一名编程专业的学生,但我的教授拒绝解释任何事情,在这个作业中她介绍了课程,而我正在努力理解构造函数的概念/语法。我的两行代码出现“匹配构造函数”错误(我已经用 cmets 标记了它们,问题代码行都在 main 中。)代码是银行账户 sim,它需要取款、存款和计算帐户上的利率。谁能解释一下是什么问题?
main.cpp
#include <iostream>
#include "Account.h"
#include "SavingsAcc.h"
#include <iomanip>
using namespace std;
int main() {
//savings account information
SavingsAccount savAcc, *savingsAcc = &savAcc;//savAcc CONSTRUCTOR ERROR "no matching constructor for initialization of SavingsAccount"
savAcc.setAccNum(1234);
savAcc.setAccBal(322.75);
savAcc.setNumWithdrawal(2);
savAcc.setNumDep(3);
savAcc.setAnnIntRate(0.06);
savAcc.setMonthServCharg(2.50);
//misc declarations
int option;
do
{
cout << "Logged in successfully...\nSAVINGS ACCOUNT\n1. Desposit\n2. Withdraw\n3. Logout\nEnter option: " << endl;
cin >> option;
switch (option)
{
case 1:
SavingsAccount savAccObj; //CONSTRUCTOR ERROR "no matching constructor for initialization of SavingsAccount"
savAccObj.deposit;
case 2:
savAccObj.withdraw;
}
}
while (option != 3);
}
帐户.cpp
#include "Account.h"
#include <iostream>
#include <iomanip>
using namespace std;
Account::Account(){};//constructor
//overloaded constructor
Account::Account(double accBal, double annIntRate){
accBal = accBal;
annIntRate = 0;
};
//deposit base class function
int Account::deposit(double accBal, int numDepos)
{
double depos;
cout << "Enter deposit: "<< endl;
cin >> depos;
accBal += depos;
numDepos ++;
return accBal;
}
//withdraw base class function
void Account::withdraw(double accBal, int numWithdrawal)
{
double amount;
cin >> amount;
accBal -= amount;
numWithdrawal ++;
}
//interest rate calculation
void Account::calcInt(double accBal, double annIntRate)
{
double monthlyIntRate = annIntRate/2;
double monthlyInt = accBal * monthlyIntRate;
accBal += monthlyInt;
}
帐户.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
//variable declarations
private:
int accNum;
double accBal;
int numWithdrawal;
int numDepos;
double annIntRate;
double monthServCharg;
public:
Account();
Account(double, double); //overloaded constructor
virtual int deposit(double, int); //base class deposit declaration
virtual void withdraw(double, int); //base class withdraw declaration
virtual void calcInt(double, double); //updates interest rate
void setAccNum(int); //setter
int getAccNum(){return accNum;} //getter
void setAccBal(double);//setter
double getAccBal(){return accBal;}//getter
void setNumWithdrawal(int);//setter
int getNumWithdrawal(){return numWithdrawal;}//getter
void setNumDep(int);//setter
int getNumDep(){return numDepos;}//getter
void setAnnIntRate(double);//setter
double getAnnIntRate(){return annIntRate;}//getter
void setMonthServCharg(double); //setter
double getMonthServCharg(){return monthServCharg;}//getter
};
#endif
SavingsAcc.cpp
#include "SavingsAcc.h"
#include "Account.h"
#include <iostream>
using namespace std;
//status constructor
SavingsAccount::SavingsAccount(bool status)
{
status = false;
}
//deposit money function
void SavingsAccount::deposit(bool status, double accBal, int numDepos)
{
Account obj;
obj.deposit(accBal,0);
if (accBal > 25.00)
status = true;
else
status = false;
}
//withdraw money child function, updates account status, service charges, interest rate etc
void SavingsAccount::withdraw(double accBal, int numWithdrawal, double annIntRate, double monthServCharg)
{
double amount;
if (accBal <= 25.00)
cout << "Balance too low, withdrawals cannot be made at this time"<<endl;
else
{
do
{
Account obj;
obj.withdraw(numWithdrawal, 0); // parent deposit function
if (numWithdrawal > 4)
monthServCharg += 1;
if (accBal < 25.00)
{annIntRate += 0.01;}
}
while (amount > accBal);
cout << "Insufficient funds!";
}
}
//outputs the report of account
void SavingsAccount::accountReport(int accNum, bool status, int numWithdrawal, int numDepos, double monthServeCharg, double accBal)
{
cout << "====ACCOUNT STATUS===="<<endl;
cout << "Account number: "<< accNum <<endl;
if (status == true) //status
cout << "Satus: Active" << endl;
else
cout <<"Status: Inactive" << endl;
cout << "Total deposits: "<< numDepos << endl;
cout << "Total withdrawals" << numWithdrawal << endl;
cout << "Service charges: "<< monthServeCharg << endl;
cout << "Balance: "<<accBal<<endl;
}
SavingsAcc.h
#ifndef SAVINGSACC_H
#define SAVINGSACC_H
#include <iostream>
class SavingsAccount: public Account
{
private:
bool status; //status variable
public:
SavingsAccount(bool); // constuctor
void deposit(bool, double, int); // deposit function
void withdraw(double, int, double, double); //withdraw function
void accountReport(int, bool, int, int, double, double ); // full report of account
};
#endif
【问题讨论】:
-
如果你想让
SavingsAccount savAccObj;工作,你需要为SavingsAccount提供一个零参数构造函数。
标签: c++ class constructor