帐户是基类,检查和保存是我试图设置费率的派生类(这是一个在cheking和保存类中的方法)。我怎么能打电话给这样的方法。 Attched是代码的骨架。鉴于MY INEXPERIENCE,欢迎任何建议。
class Account {
public:
void setAccNum(long acct) {
}
void setBalance(double bal) {
}
string toString(){
}
double virtual computeIntr(int years) {
}
};
class Checking : public Account {
public:
Checking() {
}
Checking(long acct, double bal) : Account(acct, bal) {
}
void setMinIntrBalance(int minb) {
}
void setRate(int r) {
}
double computeIntr(int years) {
}
string toString() {
}
};
class Saving : public Account {
public:
Saving() {
}
Saving(long acct, double bal, double rate) : Account(acct, bal) {
}
void setRate(double rate) {
}
double computeIntr(int years) {
}
string toString() {
};
int main() {
Account **accountPtrs;
accountPtrs = new Account*[100];
for (int i = 0; i < 100; i++) {
accountPtrs[i] = nullptr;
}
accountPtrs[0] = new Checking(100, 1000 + 1000);
accountPtrs[1] = new Checking(101, 1000 + 1010);
accountPtrs[2] = new Checking(102, 1000 + 1020);
accountPtrs[3] = new Checking(103, 1000 + 1030);
accountPtrs[4] = new Checking(104, 1000 + 1040);
accountPtrs[5] = new Saving(200, 1000 + 2000, 3);
accountPtrs[6] = new Saving(201, 1000 + 2010, 3);
accountPtrs[7] = new Saving(202, 1000 + 2020, 3);
accountPtrs[8] = new Saving(203, 1000 + 2030, 3);
accountPtrs[9] = new Saving(204, 1000 + 2040, 3);
bool exit = true;
while (exit == true) {
cout << "5) Set Interest" << endl;
cout << " Please enter your choice: ";
int ch;
cin >> ch;
cout << endl;
switch (ch)
{
case 5:
{
bool a = true;
while (a == true) {
int h, b;
char t;
cout << "YOU WANT TO SET THE INTEREST OF AN ACCOUUNT." << endl;
cout << "Saving or checking: ";
cin >> t;
if (t == 'c' || t == 'C') {
cout << "Enter account number: ";
cin >> h;
h = h - 100;
cout << "ENTER THE INTEREST RATE:";
cin >> b;
accountPtrs[h]->setRate(); /////// THIS IS WHERE IT DOES NOT WORK , HOW CAN I CALL SUCH METHOD AND SET THE RATE
a = false;
}
else if (t == 's' || t == 'S') {
cout << "Enter account number: ";
cin >> h;
h = h - 100;
cout << "ENTER THE INTEREST RATE:";
cin >> b;
accountPtrs[h]-> setRate(b);
a = false;
}
else {
cout << "INVALID INPUT" << endl;
a = true;
}
}
break;
}
}
return 0;
}
答案 0 :(得分:0)
您的数组成员指向Account
- 对象,但Accout
未提供方法setRate
;只有子类才有。
您需要在setRate
级别提供Account
,并将其实现为空或将其声明为抽象。然后,accountPtrs[h]->setRate()
将变为有效并将调用相应子类的setRate
- 实现:
class Account {
public:
...
virtual void setRate(int) = 0;
...
};