Search⌘ K

- Examples

Explore how multiple inheritance allows classes to inherit from more than one base class and how virtual inheritance resolves method ambiguity issues. Learn to use scope resolution for method access and understand key concepts to manage complex inheritance scenarios effectively in C++.

Example 1: Multiple inheritance #

C++
#include <iostream>
class Account{
public:
Account(double amt):amount(amt){}
double getBalance() const {
return amount;
}
private:
double amount;
};
class BankAccount: public Account{
public:
BankAccount(double amt): Account(amt){}
};
class WireAccount: public Account{
public:
WireAccount(double amt): Account(amt){}
};
class CheckingAccount: public BankAccount, public WireAccount{
public:
CheckingAccount(double amt): BankAccount(amt), WireAccount(amt){}
};
int main(){
std::cout << std::endl;
CheckingAccount checkAccount(100.0);
// checkAccount.getBalance(); // ERROR
std::cout << "checkAccount.BankAccount::getBalance(): " << checkAccount.BankAccount::getBalance() << std::endl;
std::cout << "checkAccount.WireAccount::getBalance(): " << checkAccount.WireAccount::getBalance() << std::endl;
std::cout << std::endl;
}

Explanation #

  • In the example above, we have created an Account class with a ...