Skip to content

Commit 5cb5e7b

Browse files
committed
add: cpp-programming-questions 06_operator_overload_friend _fun 20
1 parent 9d5f802 commit 5cb5e7b

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

06_operator_overloading_and_friend_function/14_overload_new_del_friend.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,59 @@ class Student
88
std::string name;
99

1010
public:
11-
// Constructor
11+
// constructors
1212
Student(int studentId, const std::string &studentName) : id(studentId), name(studentName)
1313
{
1414
std::cout << "Constructor called for Student " << id << std::endl;
1515
}
1616

17-
// Destructor
17+
// destructor
1818
~Student()
1919
{
2020
std::cout << "Destructor called for Student " << id << std::endl;
2121
}
2222

23-
// Display function
23+
// display function
2424
void display() const
2525
{
2626
std::cout << "Student ID: " << id << ", Name: " << name << std::endl;
2727
}
2828

29-
// Friend function to overload new operator
29+
// friend function to overload new operator
3030
friend void *operator new(size_t size);
3131

32-
// Friend function to overload delete operator
32+
// friend function to overload delete operator
3333
friend void operator delete(void *ptr) noexcept;
3434

35-
// Friend function to overload new operator (nothrow version)
35+
// friend function to overload new operator (nothrow version)
3636
friend void *operator new(size_t size, const std::nothrow_t &nothrow_constant) noexcept;
3737

3838
// Friend function to overload delete operator (nothrow version)
3939
friend void operator delete(void *ptr, const std::nothrow_t &nothrow_constant) noexcept;
4040
};
4141

42-
// Overload new operator using friend function
42+
// overload new operator using friend function
4343
void *operator new(size_t size)
4444
{
4545
std::cout << "Custom new operator called with size: " << size << " bytes." << std::endl;
4646
return ::operator new(size); // Use global new for memory allocation
4747
}
4848

49-
// Overload delete operator using friend function
49+
// overload delete operator using friend function
5050
void operator delete(void *ptr) noexcept
5151
{
5252
std::cout << "Custom delete operator called." << std::endl;
5353
::operator delete(ptr); // Use global delete for memory deallocation
5454
}
5555

56-
// Overload new operator (nothrow version) using friend function
56+
// overload new operator (nothrow version) using friend function
5757
void *operator new(size_t size, const std::nothrow_t &nothrow_constant) noexcept
5858
{
5959
std::cout << "Custom new (nothrow) operator called with size: " << size << " bytes." << std::endl;
6060
return ::operator new(size, nothrow_constant); // Use global new (nothrow) for memory allocation
6161
}
6262

63-
// Overload delete operator (nothrow version) using friend function
63+
// overload delete operator (nothrow version) using friend function
6464
void operator delete(void *ptr, const std::nothrow_t &nothrow_constant) noexcept
6565
{
6666
std::cout << "Custom delete (nothrow) operator called." << std::endl;
@@ -69,13 +69,13 @@ void operator delete(void *ptr, const std::nothrow_t &nothrow_constant) noexcept
6969

7070
int main()
7171
{
72-
// Create a student using overloaded new operator
72+
// create a student using overloaded new operator
7373
Student *student1 = new (std::nothrow) Student(1, "John Doe");
7474

75-
// Display student information
75+
// display student information
7676
student1->display();
7777

78-
// Delete the student using overloaded delete operator
78+
// delete the student using overloaded delete operator
7979
delete student1;
8080

8181
return 0;

0 commit comments

Comments
 (0)