Polymorphism
Polymorphism means "many forms".
int main() {
std::vector<Account> my_accounts;
// this is sort of OK (except: "slicing" will occur)
my_accounts.push_back(CheckingAccount(2000.0));
std::cout << my_accounts.back().type() << std::endl;
return 0;
}
- We have an array of
Account
and we pushed theCheckingAccount
back into it. It's acceptable to treat thisCheckingAccount
as anAccount
becauseCheckingAccount
is a kind ofAccount
and has everything thatAccount
has because it inherits from it. - Each class needs the function
type()
for different outputs.
Functions
void print_account_type(const Account& acct) {
std::cout << acct.type() << std::endl;
}
int main() {
...
CheckingAccount checking(1000.0, 2.00);
...
print_account_type(checking); // Prints "Account"
...
}
-
In main,
checking
has typeCheckingAccount
. -
Passed to
print_account_type()
asconst Account&
- This is allowed because
CheckingAccout
is derived fromAccount
and inherits everything fromAccount
.
- This is allowed because
-
Prints
Account
becauseacct
is treated as anAccount
. -
Can we force
print_account_type()
to call the function corresponding to the actual type (CheckingAccount
) rather than the locally declared base type (Account
)?- Yes, requires dynamic binding.
Usually you may use a variable of a derived type as though it has the base type because
CheckingAccount
is-an Account
.
Dynamic Binding
To use it, we declare relevant member functions as virtual
.
class Account{
public:
...
virtual std::string type() const { return "Account"; }
};
class CheckingAccount : public Account {
public:
...
virtual std::string type() const { return "CheckingAccount"; }
};
virtual
means that the function is essentially virtual and
can be replaced by something else.
- If the derived class implements the same function, then the derived class will override the base class.