未调用继承的重写方法
Inherited overridden method not being called
为什么账户class的操作方法中的depositmoney()
方法调用账户class的depositmoney()
而不是[=13]的depositmoney()
=] class?直接调用 depositmoney()
方法会调用子 class 的方法,这是显而易见的。但是无法理解为什么 operations()
的间接调用没有给出预期的结果。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int temp = 0;
class account
{
protected:
string name;
double balance;
int AccNo;
public:
void operations()
{
int r = 0;
do{
cout << "1.DEPOSIT" << endl;
cout << "2.WITHDRAW" << endl;
cout << "3.CHECK BALANCE" << endl;
cout << "0.EXIT" << endl;
cin >> r;
switch(r)
{
case 1:this->depositmoney();
break;
case 2:this->withdraw();
break;
case 3:this->displaybalance();
break;
case 0:cout << "Exiting" << endl;
break;
default:
break;
}
}
while(r != 0);
}
account(string Name, double bal)
{
name = Name;
balance = bal;
AccNo = temp++;
}
void displaybalance()
{
cout << "name :" << name << endl;
cout << "A/C" << AccNo << endl;
cout << "your balance is " << balance << endl;
}
void depositmoney()
{
float deposit;
cout << "enter deposit" << endl;
cin >> deposit;
balance += deposit;
}
protected:
void withdraw()
{
float withdrawal;
cout << "enter witdrawal amount" << endl;
cin >> withdrawal;
if(balance >= withdrawal)
{
balance -= withdrawal;
}
else
{
cout << "insufficient funds" << endl;
}
}
};
class Sav_account :public account
{
private:
double const CI = 5;
void depositinterest()
{
balance += balance*CI / 100;
}
public:
Sav_account(string Name, double bal) :account(Name, bal)
{
AccType = 0;
}
void depositmoney()
{
account::depositmoney();
depositinterest();
}
};
void main()
{
Sav_account *account1 = new Sav_account("Shaw", 50000);
account1->operations();// DEPOSIT RESULTS IN NO INTEREST
account1->displaybalance();
account1->depositmoney(); // **DEPOSIT FUNCTION OF Sav_account CALLS interest function**
account1->displaybalance();
_getch();
}
在您的 类 中 没有 任何覆盖的方法;你一个都没有做 virtual
.
您的 class 不是多态的。您没有多态函数,也没有覆盖任何东西。 (你有隐藏的功能)。因此,从 account
的函数调用 depositmoney();
将调用 account::depositmoney()
.
要使 class 多态,您需要使用虚函数。任何想要具有多态行为的函数都需要在基 class 中声明为 virtual
,例如在 account
此处:
virtual void depositmoney() {
在派生的class中,自C++11起,你可以这样写:
void depositmoney() override
^^^^^^^^
如果您在尝试重写某个函数时不小心将其隐藏,将会导致编译器错误。 (虽然我猜你正在使用不支持 C++11 的编译器,因为其中大多数也会拒绝 void main
)。
此外,任何多态基 class 都应该有一个虚拟析构函数,即 virtual ~account() {}
否则代码 account *x = new Sav_account("Shaw", 50000); delete x;
会导致未定义的行为。
void main
是非法的,main
必须有一个return类型的int
。
为什么账户class的操作方法中的depositmoney()
方法调用账户class的depositmoney()
而不是[=13]的depositmoney()
=] class?直接调用 depositmoney()
方法会调用子 class 的方法,这是显而易见的。但是无法理解为什么 operations()
的间接调用没有给出预期的结果。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int temp = 0;
class account
{
protected:
string name;
double balance;
int AccNo;
public:
void operations()
{
int r = 0;
do{
cout << "1.DEPOSIT" << endl;
cout << "2.WITHDRAW" << endl;
cout << "3.CHECK BALANCE" << endl;
cout << "0.EXIT" << endl;
cin >> r;
switch(r)
{
case 1:this->depositmoney();
break;
case 2:this->withdraw();
break;
case 3:this->displaybalance();
break;
case 0:cout << "Exiting" << endl;
break;
default:
break;
}
}
while(r != 0);
}
account(string Name, double bal)
{
name = Name;
balance = bal;
AccNo = temp++;
}
void displaybalance()
{
cout << "name :" << name << endl;
cout << "A/C" << AccNo << endl;
cout << "your balance is " << balance << endl;
}
void depositmoney()
{
float deposit;
cout << "enter deposit" << endl;
cin >> deposit;
balance += deposit;
}
protected:
void withdraw()
{
float withdrawal;
cout << "enter witdrawal amount" << endl;
cin >> withdrawal;
if(balance >= withdrawal)
{
balance -= withdrawal;
}
else
{
cout << "insufficient funds" << endl;
}
}
};
class Sav_account :public account
{
private:
double const CI = 5;
void depositinterest()
{
balance += balance*CI / 100;
}
public:
Sav_account(string Name, double bal) :account(Name, bal)
{
AccType = 0;
}
void depositmoney()
{
account::depositmoney();
depositinterest();
}
};
void main()
{
Sav_account *account1 = new Sav_account("Shaw", 50000);
account1->operations();// DEPOSIT RESULTS IN NO INTEREST
account1->displaybalance();
account1->depositmoney(); // **DEPOSIT FUNCTION OF Sav_account CALLS interest function**
account1->displaybalance();
_getch();
}
在您的 类 中 没有 任何覆盖的方法;你一个都没有做 virtual
.
您的 class 不是多态的。您没有多态函数,也没有覆盖任何东西。 (你有隐藏的功能)。因此,从 account
的函数调用 depositmoney();
将调用 account::depositmoney()
.
要使 class 多态,您需要使用虚函数。任何想要具有多态行为的函数都需要在基 class 中声明为 virtual
,例如在 account
此处:
virtual void depositmoney() {
在派生的class中,自C++11起,你可以这样写:
void depositmoney() override
^^^^^^^^
如果您在尝试重写某个函数时不小心将其隐藏,将会导致编译器错误。 (虽然我猜你正在使用不支持 C++11 的编译器,因为其中大多数也会拒绝 void main
)。
此外,任何多态基 class 都应该有一个虚拟析构函数,即 virtual ~account() {}
否则代码 account *x = new Sav_account("Shaw", 50000); delete x;
会导致未定义的行为。
void main
是非法的,main
必须有一个return类型的int
。