无法将方法绑定到C++中的类

  • 本文关键字:C++ 绑定 方法 c++
  • 更新时间 :
  • 英文 :


编码器!我一直在完成我的c++作业,但却被一个名为BankAccount的类的方法所困扰。主要思想是创建一个名为BankAccount的类,它有自己的方法,如deposit((或addInterest((,函数与它们的名称和构造函数匹配。我已经声明了我的所有方法并编写了它们的代码,然而当涉及到名为";void BankAccount::addInterest(("以及";void BankAccout::deposit(("我收到错误消息,比如";不能胜任";。我将在这里分享我的代码:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class BankAccount
{
public:
double getBalance();
void deposit(double);
void witdraw(double);
void addInterest();
BankAccount();
BankAccount(double, double, int, string);
double displayAccountSummary();


private:
double interestRate;
int accountNumber;
double deposit;
double accountWithdraw;
double balance;
string ownerName;


};
int main()
{
BankAccount myAccount(1000.50, 0.05, 1111, "John Williamas");
myAccount.deposit(500);
myAccount.witdraw(200);
myAccount.addInterest();
myAccount.displayAccountSummary();
return 0;
}
void BankAccount::deposit(double depozit)
{
balance = depozit + balance;
}
void BankAccount::addInterest(double rate)
{
balance = balance * (1 + rate);
}
BankAccount::BankAccount()
{
balance = 0;
interestRate = 0;
accountNumber = 0;
ownerName = "";
}
BankAccount::BankAccount(double money, double rate, int accountnumber, string name)
{
balance = money;
interestRate = rate;
accountNumber=accountnumber;
ownerName = name;
}
double BankAccount::getBalance()
{
return balance;
}
void BankAccount::witdraw(double wizdraw)
{
if (wizdraw < 0||wizdraw>balance)
{
cout << "Witdraw amount can not be less than 0 or greater than balance. Try againn ";
cin >> wizdraw;
}
balance = balance - wizdraw;
}

double BankAccount::displayAccountSummary()
{
return balance;
return interestRate;
return accountNumber;
cout<< ownerName;
}

您的代码中存在命名冲突。例如,void deposit(double)成员函数和double deposit成员变量具有相同的名称。

固定的版本在这里,我对进行了很多改进:

#include <iostream>
#include <iomanip>
#include <string>
#include <tuple>

class BankAccount
{
public:
BankAccount( );
BankAccount( const std::string ownerName, const double balance, const double interestRate, const int accountNumber );
double getBalance( ) const;
std::tuple< double, double, int > getAccountSummary( ) const;
void displayAccountSummary( ) const;
void deposit( const double depositAmount );
bool withdraw( const double withdrawAmount );
void addInterest( const double rate );

private:
std::string m_ownerName;
double m_balance;
double m_interestRate;
int m_accountNumber;
double m_deposit;
double m_accountWithdraw;
};

int main( )
{
BankAccount myAccount( "John Williams", 1000.50, 0.05, 1111 );
std::cout << "Enter the deposit amount: ";
double depositAmount { };
std::cin >> depositAmount;
myAccount.deposit( depositAmount );
bool wasSuccessful { false };
do
{
std::cout << "Enter the withdraw amount: ";
double withdrawAmount { };
std::cin >> withdrawAmount;
wasSuccessful = myAccount.withdraw( withdrawAmount );
if ( !wasSuccessful )
{
std::cout << "Withdraw amount can not be less than 0 or greater than balance. Try againn ";
}
} while ( !wasSuccessful );
std::cout << "Enter the interest rate: ";
double rate { };
std::cin >> rate;
myAccount.addInterest( rate );
// use structured bindings like below to store the returned values
const auto [ balance, interestRate, accountNumber ] = myAccount.getAccountSummary( );
std::cout << 'n';
myAccount.displayAccountSummary( );
return 0;
}
BankAccount::BankAccount( )
: m_ownerName( "NO-NAME" ), m_balance( 0 ), m_interestRate( 0 ), m_accountNumber( 0 )
{
}
BankAccount::BankAccount( const std::string ownerName, const double balance, const double interestRate, const int accountNumber )
: m_ownerName( ownerName ), m_balance( balance ), m_interestRate( interestRate ), m_accountNumber( accountNumber )
{
}
double BankAccount::getBalance( ) const
{
return m_balance;
}
// return multiple values using a tuple and not by writing 3 return statements!!!
std::tuple< double, double, int > BankAccount::getAccountSummary( ) const
{
return std::tuple< double, double, int >( std::move( m_balance ),
std::move( m_interestRate ),
std::move( m_accountNumber ) );
}
void BankAccount::displayAccountSummary( ) const
{
std::cout << "Owner: " << m_ownerName << 'n';
std::cout << "Balance: " << m_balance << 'n';
std::cout << "Interest rate: " << m_interestRate << 'n';
std::cout << "Account number: " << m_accountNumber << 'n';
}
void BankAccount::deposit( const double depositAmount )
{
m_balance += depositAmount;
}
bool BankAccount::withdraw( const double withdrawAmount )
{
if ( withdrawAmount < 0 || withdrawAmount > m_balance )
{
return false;
}
m_balance -= withdrawAmount;
return true;
}
void BankAccount::addInterest( const double rate )
{
m_balance *= ( 1.0 + rate );
}

样本输入/输出:

Enter the deposit amount: 500
Enter the withdraw amount: 200
Enter the interest rate: 0.05
Owner: John Williams
Balance: 1365.53
Interest rate: 0.05
Account number: 1111

现在,您可以自己完成代码及其其余功能。

上述方法是可行的,您也可以添加this->,如this->deposit,以避免命名冲突。

最新更新