简体   繁体   中英

How to find which class object is sitting on a particular index of an array (Polymorphism )

I have a base class of Bank that is being inherited by classes "Saving", "Checking", "Current" and "Investment". I am practicing polymorphism for my school.

I have a code that that is pretty long that I can't share here. All I am doing here in this piece of code is allocating dynamic array(that I have separate function for, grow2D) at pointer this->bank and creating objects on the runtime by asking the user if he wants to create an object of "Saving", "Checking", "Current" or "Investment" account. my question is that, after I am done creating the objects by asking the user, how can I check which type("Saving", "Checking", "Current" or "Investment") of object is sitting on a particular index.

For instance the use creates an object of type "Savings" at bank[0]. How can I find what type of object is sitting on bank[0] later in my code when user is done adding all the accounts?

    string name;
    unsigned long long int accountNumber;
    unsigned long int balance;
    int option = 0;
    size++;
    this->bank = grow2D(bank, size);
    cout <<endl << "1 for Saving" << endl;
    cout << "2 for Checking" << endl;
    cout << "3 for Current" << endl;
    cout << "4 for Investment" << endl;
    cout << "Enter option";
    cin >> option;
    cin.ignore();
    cout << "Enter name";
    getline(cin, name);

    cout << "Enter Account Number";
    cin >> accountNumber;
    cout << "Enter Balance";
    cin >> balance;
    if(option==1)
    {
        int interestRate;
        cout << "Enter Interest Rate: ";
        cin >> interestRate;
        bank[size - 1] = new Saving(name,accountNumber,balance,interestRate);
    }
    else if (option == 2)
    {
        int fee;
        cout << "Enter Fee: ";
        cin >> fee;
        bank[size - 1] = new Checking(name, accountNumber, balance,fee);
    }
    else if (option == 3)
    {
        int fee;
        unsigned long int minimumBalance;
        cout << "Enter Fee: ";
        cin >> fee;
        cout << "Enter Minimum balance: ";
        cin >> minimumBalance;
        bank[size - 1] = new Current(name, accountNumber, balance,fee,minimumBalance);
    }
    else if (option == 4)
    {
        int fee;
        unsigned long int minimumBalance;
        int profit;
        cout << "Enter Fee: ";
        cin >> fee;
        cout << "Enter Minimum balance: ";
        cin >> minimumBalance;
        cout << "Enter Profit: ";
        cin >> profit;
        bank[size - 1] = new Investment(name, accountNumber, balance, fee, minimumBalance,profit);
    }

Assuming your classes contain at least one virtual method (at least the destructor should be virtual), eg:

class Saving {
public:
    virtual ~Saving() = default;
};

class Checking: public Saving {};
class Current: public Saving {};
class Investment: public Saving {};

Then you can use typeid() to query the runtime-type of the object.
Example:

#include <typeinfo>

Saving* s = new Current();

if(typeid(*s) == typeid(Current)) {
  std::cout << "It's a Current!" << std::endl;
}

if(typeid(*s) == typeid(Investment)) {
  std::cout << "It's an Investment!" << std::endl;
}

Alternatively you can also use dynamic_cast() and check if the cast was successful to the derived type:

Saving* s = new Investment();

Investment* investment = dynamic_cast<Investment*>(s);
if(investment != nullptr) {
  std::cout << "It's an Investment!" << std::endl;
}

Current* current = dynamic_cast<Current*>(s);
if(current != nullptr) {
  std::cout << "It's a Current!" << std::endl;
}

// 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