|
| 1 | +#include <iostream> |
| 2 | +#include <new> |
| 3 | + |
| 4 | +class Student |
| 5 | +{ |
| 6 | +private: |
| 7 | + int id; |
| 8 | + std::string name; |
| 9 | + |
| 10 | +public: |
| 11 | + // Constructor |
| 12 | + Student(int studentId, const std::string &studentName) : id(studentId), name(studentName) |
| 13 | + { |
| 14 | + std::cout << "Constructor called for Student " << id << std::endl; |
| 15 | + } |
| 16 | + |
| 17 | + // Destructor |
| 18 | + ~Student() |
| 19 | + { |
| 20 | + std::cout << "Destructor called for Student " << id << std::endl; |
| 21 | + } |
| 22 | + |
| 23 | + // Display function |
| 24 | + void display() const |
| 25 | + { |
| 26 | + std::cout << "Student ID: " << id << ", Name: " << name << std::endl; |
| 27 | + } |
| 28 | + |
| 29 | + // Friend function to overload new operator |
| 30 | + friend void *operator new(size_t size); |
| 31 | + |
| 32 | + // Friend function to overload delete operator |
| 33 | + friend void operator delete(void *ptr) noexcept; |
| 34 | + |
| 35 | + // Friend function to overload new operator (nothrow version) |
| 36 | + friend void *operator new(size_t size, const std::nothrow_t ¬hrow_constant) noexcept; |
| 37 | + |
| 38 | + // Friend function to overload delete operator (nothrow version) |
| 39 | + friend void operator delete(void *ptr, const std::nothrow_t ¬hrow_constant) noexcept; |
| 40 | +}; |
| 41 | + |
| 42 | +// Overload new operator using friend function |
| 43 | +void *operator new(size_t size) |
| 44 | +{ |
| 45 | + std::cout << "Custom new operator called with size: " << size << " bytes." << std::endl; |
| 46 | + return ::operator new(size); // Use global new for memory allocation |
| 47 | +} |
| 48 | + |
| 49 | +// Overload delete operator using friend function |
| 50 | +void operator delete(void *ptr) noexcept |
| 51 | +{ |
| 52 | + std::cout << "Custom delete operator called." << std::endl; |
| 53 | + ::operator delete(ptr); // Use global delete for memory deallocation |
| 54 | +} |
| 55 | + |
| 56 | +// Overload new operator (nothrow version) using friend function |
| 57 | +void *operator new(size_t size, const std::nothrow_t ¬hrow_constant) noexcept |
| 58 | +{ |
| 59 | + std::cout << "Custom new (nothrow) operator called with size: " << size << " bytes." << std::endl; |
| 60 | + return ::operator new(size, nothrow_constant); // Use global new (nothrow) for memory allocation |
| 61 | +} |
| 62 | + |
| 63 | +// Overload delete operator (nothrow version) using friend function |
| 64 | +void operator delete(void *ptr, const std::nothrow_t ¬hrow_constant) noexcept |
| 65 | +{ |
| 66 | + std::cout << "Custom delete (nothrow) operator called." << std::endl; |
| 67 | + ::operator delete(ptr, nothrow_constant); // Use global delete (nothrow) for memory deallocation |
| 68 | +} |
| 69 | + |
| 70 | +int main() |
| 71 | +{ |
| 72 | + // Create a student using overloaded new operator |
| 73 | + Student *student1 = new (std::nothrow) Student(1, "John Doe"); |
| 74 | + |
| 75 | + // Display student information |
| 76 | + student1->display(); |
| 77 | + |
| 78 | + // Delete the student using overloaded delete operator |
| 79 | + delete student1; |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
0 commit comments