Skip to content

Commit 893b358

Browse files
Use unique_ptr instead of raw pointers for memory safety
Replaced raw pointers with unique_ptr for better memory management in main functions. Since this is a teaching tool, I think it's best to teach beginners good modern C++ code.
1 parent 5fe5537 commit 893b358

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

oop/cpp/abstraction/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ public:
5454
};
5555

5656
int main() {
57-
Vehicle* myCar = new Car("Toyota");
57+
unique_ptr<Vehicle> myCar = make_unique<Car>("Toyota");
5858
myCar->displayBrand();
5959
myCar->start();
60-
delete myCar;
6160
return 0;
6261
}
6362
```
@@ -108,14 +107,12 @@ public:
108107
};
109108
110109
int main() {
111-
Animal* myDog = new Dog();
110+
unique_ptr<Animal> myDog = make_unique<Dog>();
112111
myDog->makeSound();
113112
114-
Animal* myCat = new Cat();
113+
unique_ptr<Animal> myCat = make_unique<Cat>();
115114
myCat->makeSound();
116115
117-
delete myDog;
118-
delete myCat;
119116
return 0;
120117
}
121118
```
@@ -182,15 +179,14 @@ public:
182179
};
183180

184181
int main() {
185-
Payment* payment;
182+
unique_ptr<Payment> payment;
186183

187-
payment = new CreditCardPayment(150.75);
184+
payment = make_unique<CreditCardPayment>(150.75);
188185
payment->pay();
189186

190-
payment = new PayPalPayment(200.50);
187+
payment = make_unique<PayPalPayment>(200.50);
191188
payment->pay();
192189

193-
delete payment;
194190
return 0;
195191
}
196192
```
@@ -204,4 +200,4 @@ Paid 200.50 using PayPal
204200
**Why Use Abstraction in Payment Systems?**
205201
- Allows multiple payment methods without modifying existing code.
206202
- Improves maintainability and scalability.
207-
- Provides a **common contract** for different payment types.
203+
- Provides a **common contract** for different payment types.

0 commit comments

Comments
 (0)