模板的继承参数设置

因此,我试图在Class Bank中添加带有vector的新帐户。对于从班级bankaccount派生的新帐户,例如班级检查和班级保存。

我无法弄清楚必须在void addaccount()参数中放入哪种类型的参数,因此可以在下面的main函数中使用它。是否有可能我不必使用模板?据我了解,这是唯一的选择,因为我正在使用它来调用多个类类型。

Bank b;
    b.addAccount(new CheckingAccount(12345, 18));
#include "pch.h"
#include <iostream>
#include <vector>

using namespace std;

class bankaccount {
    int acctnum;
    double balance;
public:
    bankaccount(double newbalance=0.00, int newacctnum=0) { balance = newbalance; acctnum = newacctnum; }
    int getacctnum() { return acctnum;}
    double getbalance() { return balance; }
    double setbalance(double newmoney) { balance += newmoney; }
    void deposit(double newdepo) {
        balance=getbalance() + newdepo;
    }
    void deposit(double newwith) {
        balance = getbalance() - newwith;
    }


};

class Savingaccount: public bankaccount
{
    Savingaccount(int newacctnum, double rate):bankaccount(){
        acctnum = newacctnum;
        balance = getbalance() * (1 + rate);
    };
    void deposit(double newdepo) {
        balance = getbalance() + newdepo;

    }
    void deposit(double newwith) {
        balance = getbalance() - newwith;
    }

};



class Checkingaccount : public bankaccount {

    Checkingaccount(int newacctnum, double rate);

    void deposit(double newdepo) {
        balance = getbalance() + newdepo;

    }
    void deposit(double newwith) {
        balance = getbalance() - newwith;
    }


};



template <class T>
class Bank {
    vector<T> x;
public:
    void addaccount(T *temp);
    void runmonthly();

};

int main() {
    Bank b;
    b.addAccount(new CheckingAccount(12345, 18)); //$18 monthly fee
    b.addAccount(new SavingsAccount(12346, 0.02)); // 2% per month interest!

}
评论