C++ 从同一基类派生的两个派生类具有不同的结果?
两个派生类,具有从同一类派生的相同逻辑,但结果不同C++ 从同一基类派生的两个派生类具有不同的结果?,c++,c++11,C++,C++11,两个派生类,具有从同一类派生的相同逻辑,但结果不同 我有一个名为account的基类和两个来自account的派生类,分别名为savings\u account和checking\u account。当我创建savings\u account对象时,一切正常,但当我创建checking\u account类的对象时,它也会创建一个空对象account类。通过使用重载运算符,看起来您在派生类中重新定义了balance(和name),这会将名称隐藏在基类中(现在您有两个balance变量),只需使用
我有一个名为
account
的基类和两个来自account
的派生类,分别名为savings\u account
和checking\u account
。当我创建savings\u account
对象时,一切正常,但当我创建checking\u account
类的对象时,它也会创建一个空对象account
类。通过使用重载运算符,看起来您在派生类中重新定义了balance
(和name
),这会将名称隐藏在基类中(现在您有两个balance
变量),只需使用基类中的一个变量即可。是的,它成功了!!谢谢
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;
}
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;
}
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;
}
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;
}
48.5
The name of account is tanzeel and current balance is 48.5