@@ -30,6 +30,7 @@ An **abstract class** in C++ is a class that **cannot be instantiated**. It is u
3030
3131``` cpp
3232#include < iostream>
33+ #include < memory>
3334using namespace std ;
3435
3536// Abstract class
@@ -39,6 +40,7 @@ protected:
3940public:
4041 Vehicle(string b) : brand(b) {}
4142 virtual void start() = 0; // Pure virtual function
43+ virtual ~ Vehicle() = default; // virtual destructor
4244 void displayBrand() {
4345 cout << "Brand: " << brand << endl;
4446 }
@@ -54,10 +56,9 @@ public:
5456};
5557
5658int main() {
57- Vehicle* myCar = new Car("Toyota");
59+ unique_ptr< Vehicle > myCar = make_unique< Car > ("Toyota");
5860 myCar->displayBrand();
5961 myCar->start();
60- delete myCar;
6162 return 0;
6263}
6364```
@@ -83,11 +84,13 @@ An **interface** in C++ is created using a class that contains **only pure virtu
8384
8485```cpp
8586#include <iostream>
87+ #include <memory>
8688using namespace std;
8789
8890// Defining an interface
8991class Animal {
9092public:
93+ virtual ~Animal() = default; // virtual destructor
9194 virtual void makeSound() = 0; // Pure virtual function
9295};
9396
@@ -108,14 +111,12 @@ public:
108111};
109112
110113int main() {
111- Animal* myDog = new Dog();
114+ unique_ptr< Animal> myDog = make_unique< Dog> ();
112115 myDog->makeSound();
113116
114- Animal* myCat = new Cat();
117+ unique_ptr< Animal> myCat = make_unique< Cat> ();
115118 myCat->makeSound();
116119
117- delete myDog;
118- delete myCat;
119120 return 0;
120121}
121122```
@@ -153,6 +154,7 @@ Abstraction is widely used in real-world applications, such as payment processin
153154
154155``` cpp
155156#include < iostream>
157+ #include < memory>
156158using namespace std ;
157159
158160// Abstract class for Payment
@@ -161,6 +163,7 @@ protected:
161163 double amount;
162164public:
163165 Payment(double amt) : amount(amt) {}
166+ virtual ~ Payment() = default; // virtual destructor
164167 virtual void pay() = 0; // Abstract method
165168};
166169
@@ -182,15 +185,14 @@ public:
182185};
183186
184187int main() {
185- Payment* payment;
188+ unique_ptr< Payment > payment;
186189
187- payment = new CreditCardPayment(150.75);
190+ payment = make_unique< CreditCardPayment > (150.75);
188191 payment->pay();
189192
190- payment = new PayPalPayment(200.50);
193+ payment = make_unique< PayPalPayment > (200.50);
191194 payment->pay();
192195
193- delete payment;
194196 return 0;
195197}
196198```
@@ -204,4 +206,4 @@ Paid 200.50 using PayPal
204206**Why Use Abstraction in Payment Systems?**
205207- Allows multiple payment methods without modifying existing code.
206208- Improves maintainability and scalability.
207- - Provides a **common contract** for different payment types.
209+ - Provides a **common contract** for different payment types.
0 commit comments