简体   繁体   中英

inheritance parameter setup for template

So I am trying to append new accounts with vector which in Class Bank. For new account such as Class checking and Class saving that are derived from Class bankaccount.

I could not figure out what type of parameter I have to put in void addaccount() parameter so I can use it in main function below. Is it possible way I do not have to use template? From my understanding that is only option as I am using it to call multiple class type.

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!

}

In your example, there is no need for templates. You can use runtime polymorphism and you were already half way there. However, you code had a lot of errors and I fixed multiple of them. Nonetheless, I still might have missed something. Please, next time, make sure that there are no bugs in your code unrelated to your question here (eg wrong names, multiple functions with the same signature in a class, etc.). Here is a working code, see the annotations in the code for explanations ( live demo ):

#include <iostream>
#include <vector>
#include <string>
// We need this for std::unique_ptr
#include <memory>


using namespace std;

class bankaccount {

// To access the members from base classes, make them protected.
protected:
    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; }

    // The return type should be void.
    void setbalance(double newmoney) { balance += newmoney; }

    // Make this virtual, to be able to choose the correct function from derived
    // classes via runtime polymorphism.
    virtual void deposit(double newdepo) {
        balance=getbalance() + newdepo;
    }

    // Added this to demonstrate runtime polymorphism.
    virtual string accounttype () const {return "base";}

    // I removed the second deposit method, as you are not allowed to
    // have multiple functions with the same signature. Btw both functions did
    // the same.

    // When having virtual functions, you should also have a virtual destructor.
    virtual ~bankaccount() = default;
};

class Savingaccount: public bankaccount
{
// The functions above should be part of the public interface.
public:
    Savingaccount(int newacctnum, double rate):bankaccount(){
        acctnum = newacctnum;
        balance = getbalance() * (1 + rate);
    };

    // The override keyword specifies, that a virtual function of the base
    // class is going to be overwritten.
    virtual void deposit(double newdepo) override {
        balance = getbalance() + newdepo;
    }

    // Added this to demonstrate runtime polymorphism.
    virtual string accounttype () const {return "saving";}

    // When having virtual functions, you should also have a virtual destructor.
    virtual ~Savingaccount() = default;
};



class Checkingaccount : public bankaccount
{
// The functions above should be part of the public interface.
public:
    Checkingaccount(int newacctnum, double rate):bankaccount() {/*TODO*/};

    // Make this function virtual:
    virtual void deposit(double newdepo) override {
        balance = getbalance() + newdepo;
    }

    // Added this to demonstrate runtime polymorphism.
    virtual string accounttype () const override {return "checking";}

    // When having virtual functions, you should also have a virtual destructor.
    virtual ~Checkingaccount() = default;
};



class Bank {
    vector<unique_ptr<bankaccount>> x;
public:
    auto& getaccounts() const {return x;}
    // Now, pass a pointer to the base class. The correct functions deposit
    // and accounttype will be called as you marked them in the base class as
    // virtual.
    void addaccount(bankaccount *temp) {x.emplace_back(temp);}
    void runmonthly() {/*TODO*/};
};

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

    for (const auto& account : b.getaccounts())
        std::cout << account->accounttype() << std::endl;

}

Note that there is still a lot of room for improvement. For instance, you should check if an account with a certain number exists before adding it. If you don't intend to further derive from Checkingaccount or Savingaccount you can mark them as final , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM