J
Jeff0803
Guest
Here is a sample code.
#include <iostream>
#include <string>
using namespace std;
class Account {
public:
Account(double d) { _balance = d; }
virtual ~Account() {}
virtual double GetBalance() { return _balance; }
virtual void PrintBalance() { cerr << "Error. Balance not available for base type." << endl; }
private:
double _balance;
};
class CheckingAccount : public Account {
public:
CheckingAccount(double d) : Account(d) {}
void PrintBalance() override { cout << "Checking account balance: " << GetBalance() << endl; }
};
class SavingsAccount : public Account {
public:
SavingsAccount(double d) : Account(d) {}
void PrintBalance() override { cout << "Savings account balance: " << GetBalance(); }
};
int main()
{
// Create objects of type CheckingAccount and SavingsAccount.
CheckingAccount checking(100.00);
SavingsAccount savings(1000.00);
// Call PrintBalance using a pointer to Account.
Account* pAccount = &checking;
pAccount->PrintBalance();
// Call PrintBalance using a pointer to Account.
pAccount = &savings;
pAccount->PrintBalance();
}
This result is like following.
Checking account balance: 100
Savings account balance: 1000
If the virtual function is overriden like following,
void PrintBalance() override { cout << "Checking account balance: " << GetBalance() << endl; }
void PrintBalance() override { cout << "Savings account balance: " << GetBalance(); }
The result is like following which is the same as previous result.
Checking account balance: 100
Savings account balance: 1000
Does this mean override is default?
Continue reading...
#include <iostream>
#include <string>
using namespace std;
class Account {
public:
Account(double d) { _balance = d; }
virtual ~Account() {}
virtual double GetBalance() { return _balance; }
virtual void PrintBalance() { cerr << "Error. Balance not available for base type." << endl; }
private:
double _balance;
};
class CheckingAccount : public Account {
public:
CheckingAccount(double d) : Account(d) {}
void PrintBalance() override { cout << "Checking account balance: " << GetBalance() << endl; }
};
class SavingsAccount : public Account {
public:
SavingsAccount(double d) : Account(d) {}
void PrintBalance() override { cout << "Savings account balance: " << GetBalance(); }
};
int main()
{
// Create objects of type CheckingAccount and SavingsAccount.
CheckingAccount checking(100.00);
SavingsAccount savings(1000.00);
// Call PrintBalance using a pointer to Account.
Account* pAccount = &checking;
pAccount->PrintBalance();
// Call PrintBalance using a pointer to Account.
pAccount = &savings;
pAccount->PrintBalance();
}
This result is like following.
Checking account balance: 100
Savings account balance: 1000
If the virtual function is overriden like following,
void PrintBalance() override { cout << "Checking account balance: " << GetBalance() << endl; }
void PrintBalance() override { cout << "Savings account balance: " << GetBalance(); }
The result is like following which is the same as previous result.
Checking account balance: 100
Savings account balance: 1000
Does this mean override is default?
Continue reading...