Two derived classes having same logic derived from same class but different results.
I have a base class named account and two derived classes from account class named savings_account and checking_account. When I make savings_account object, everything works fine, but when i make an object of checking_account class, it also creates an empty object account class. By using overloaded operator <<, cout << checking_account_object, it gives me the balance of checking_account_object. but when i use get_balance method, it gives me the balance of account_object (I created checking_Account_object but automatically created account_object). Similarly works with deposit method, it deposits money in account_object, not in checking_account_object. I have been stuck in this code for 2 days.
Here is my code for account class 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;
}
And Here is my code for 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;
}
and Here is checing_account class
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() function
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 is working well. But checking_account is not giving me required output. I expected the output of checking_account_object to be 48.5 The name of account is tanzeel and current balance is 48.5 but the output is -51.5 The name of account is tanzeel and current balance is 100
Aucun commentaire:
Enregistrer un commentaire