Skip to content

Commit a389af1

Browse files
committed
Add more examples
1 parent 65c4520 commit a389af1

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Classification/5-abstract.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Subtype polymorphism: abstract class
2+
3+
abstract class Account {
4+
constructor(protected readonly balance: number) {}
5+
6+
abstract describe(): string;
7+
}
8+
9+
class SavingsAccount extends Account {
10+
describe(): string {
11+
return `Savings -> Balance: ${this.balance}`;
12+
}
13+
}
14+
15+
class CreditAccount extends Account {
16+
describe(): string {
17+
return `Credit -> Balance: ${this.balance}`;
18+
}
19+
}
20+
21+
const accounts: Account[] = [new SavingsAccount(5000), new CreditAccount(-750)];
22+
23+
for (const account of accounts) {
24+
console.log(account.describe());
25+
}

Classification/6-protocol.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// Subtype polymorphism: protocol/duck typing
4+
5+
const describe = (entity) => {
6+
if (typeof entity.describe !== 'function') {
7+
throw new TypeError('Requires describe method');
8+
}
9+
entity.describe();
10+
};
11+
12+
class SavingsAccount {
13+
constructor(balance) {
14+
this.balance = balance;
15+
}
16+
17+
describe() {
18+
console.log(`Savings -> Balance: ${this.balance}`);
19+
}
20+
}
21+
22+
class CreditAccount {
23+
constructor(balance) {
24+
this.balance = balance;
25+
}
26+
27+
describe() {
28+
console.log(`Credit -> Balance: ${this.balance}`);
29+
}
30+
}
31+
32+
describe(new SavingsAccount(5000));
33+
describe(new CreditAccount(-750));

Classification/7-dynamic.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// Subtype polymorphism: dynamic dispatch
4+
5+
class Account {
6+
constructor(balance) {
7+
this.balance = balance;
8+
}
9+
10+
describe() {
11+
return `Balance: ${this.balance}`;
12+
}
13+
}
14+
15+
class SavingsAccount extends Account {
16+
describe() {
17+
return `Savings -> ${super.describe()}`;
18+
}
19+
}
20+
21+
class CreditAccount extends Account {
22+
describe() {
23+
return `Credit -> ${super.describe()}`;
24+
}
25+
}
26+
27+
const accounts = [new SavingsAccount(5000), new CreditAccount(-750)];
28+
29+
for (const account of accounts) {
30+
console.log(account.describe());
31+
}

Classification/8-virtual.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Subtype polymorphism: virtual functions and methods
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
7+
class Account {
8+
public:
9+
explicit Account(int balance) : balance(balance) {}
10+
virtual ~Account() = default;
11+
12+
virtual std::string describe() const {
13+
return "Balance: " + std::to_string(balance);
14+
}
15+
16+
protected:
17+
int balance;
18+
};
19+
20+
class SavingsAccount : public Account {
21+
public:
22+
using Account::Account;
23+
24+
std::string describe() const override {
25+
return "Savings -> " + Account::describe();
26+
}
27+
};
28+
29+
class CreditAccount : public Account {
30+
public:
31+
using Account::Account;
32+
33+
std::string describe() const override {
34+
return "Credit -> " + Account::describe();
35+
}
36+
};
37+
38+
int main() {
39+
std::vector<Account*> accounts;
40+
accounts.push_back(new SavingsAccount(5000));
41+
accounts.push_back(new CreditAccount(-750));
42+
43+
for (const auto& account : accounts) {
44+
std::cout << account->describe() << '\n';
45+
}
46+
}

0 commit comments

Comments
 (0)