两个派生类,它们具有从同一个类派生的相同逻辑,但结果不同。
我有一个名为account
的基类,还有两个从account
类派生的名为savings_account
和checking_account
的派生类。当我制作savings_account
对象时,一切正常,但是当我制作checking_account
类的对象时,它也会创建一个空的对象account
类。通过使用重载运算符<<,cout << checking_account_object
,它为我带来了checking_account_object
的余额。但是当我使用get_balance
方法时,它给了我account_object
余额(我创建了checking_Account_object
但自动创建了account_object
)。与deposit
方法类似,它以account_object
而不是checking_account_object
存钱。我在此代码中停留了2天。
这是我的account
类的代码:
class account
{
protected:
string name;
double balance;
public:
friend ostream & operator << (ostream &out, account &bank);
account(){
name = "unknown";
balance = 0.00;
}
account (const string &person_name, const double &person_balance){
name = person_name;
balance = person_balance;
}
bool deposit(const double &amount){
if(amount <= 0)
return false;
else{
balance += amount;
return true;
}
}
bool withdraw(const double &amount){
if(amount <= 0 )
return false;
else{
balance -= amount;
return true;
}
}
double getbalance(){
return balance;
}
};
ostream & operator << (ostream &out, account &bank){
out << "The back balance of " << bank.name << " is " << bank.balance << endl;
return out;
}
这是我的savings_account
代码:
class savings_account : public account
{
protected:
string name;
double balance;
double int_rate;
public:
savings_account(){
int_rate = 0;
name = "unknown";
balance = 0;
}
savings_account(const string &person_name, const double &person_balance, const double peronal_int_rate){
int_rate = peronal_int_rate;
name = person_name;
balance = person_balance;
}
bool deposit(double amount){
amount += (amount*int_rate)/100;
return account::deposit(amount);
}
friend ostream & operator << (ostream &out, savings_account &bank);
};
ostream & operator << (ostream &out, savings_account &bank){
out << "The bank balance of " << bank.name << " is " << bank.balance << endl;
return out;
}
这是checing_account
类:
class checking_account : public account{
protected:
string name;
double balance;
const double fee = 1.5;
public:
checking_account(){
name = "unknown";
balance = 0;
}
checking_account(const string &person_name, const double &personal_balance){
name = person_name;
balance = personal_balance;
}
bool withdraw(double amount){
amount = amount + fee;
return account::withdraw(amount);
}
friend ostream & operator << (ostream &out, checking_account &bank);
};
ostream & operator << (ostream &out, checking_account &bank){
out << "The name of account is " << bank.name << " and current balance is " << bank.balance << endl;
return out;
}
main()
功能:
int main(){
savings_account savings_account("tanzeel", 2000, 2);
savings_account.deposit(1000);
cout << savings_account.getbalance() << endl;
savings_account.withdraw(100);
cout << savings_account.getbalance() << endl;
cout << savings_account;
checking_account checking_account_object("tanzeel", 100.0);
checking_account_object.deposit(50);;
checking_account_object.withdraw(100);
cout << checking_account_object.getbalance() << endl;
cout << checking_account_object;
return 0;
}
Savings_account
运行良好。但是checking_account
没有给我所需的输出。
我期望checking_account_object
的输出是
48.5
The name of account is tanzeel and current balance is 48.5
但是输出是 -51.5 帐户名为tanzeel,当前余额为100