-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathtest.cpp
More file actions
34 lines (28 loc) · 738 Bytes
/
test.cpp
File metadata and controls
34 lines (28 loc) · 738 Bytes
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
#include <iostream>
#include <string>
<memory>
// Abstract base class
class Greeter {
public:
virtual ~Greeter() = default;
virtual std::string greet(const std::string& name) const = 0;
};
// Class implementation
class Greeting : public Greeter {
private:
std::string prefix;
public:
Greeting(const std::string& prefix) : prefix(prefix) {}
std::string greet(const std::string& name) const override {
return prefix + ", " + name + "!";
}
};
// Function
void printGreeting(const Greeter& greeter, const std::string& name) {
std::cout << greeter.greet(name) << std::endl;
}
int main() {
auto greeting = std::make_unique<Greeting>("Hello");
printGreeting(*greeting, "World");
return 0;
}