-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmemberFunctionVirtual.cpp
56 lines (38 loc) · 1017 Bytes
/
memberFunctionVirtual.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
class Account{
public:
Account(double amt): balance(amt){}
virtual void withdraw(double amt){
balance -= amt;
}
double getBalance() const {
return balance;
}
protected:
double balance;
};
class BankAccount: public Account{
public:
BankAccount(double amt): Account(amt){}
virtual void withdraw(double amt){
if ((balance - amt) > 0.0) balance -= amt;
}
};
int main(){
std::cout << '\n';
BankAccount bankAccount(100.0);
Account * aPtr = &bankAccount;
aPtr->withdraw(50);
std::cout << "aPtr->getBalance(): " << aPtr->getBalance() << '\n';
std::cout << '\n';
BankAccount * bankAccount2 = new BankAccount(100.0);
Account * aPtr2 = bankAccount2;
aPtr2->withdraw(50);
std::cout << "aPtr2->getBalance(): " << aPtr2->getBalance() << '\n';
std::cout << '\n';
BankAccount bankAccount3(100.0);
Account& aRef = bankAccount3;
aRef.withdraw(150);
std::cout << "aRef.getBalance(): " << aRef.getBalance() << '\n';
std::cout << '\n';
}