0% found this document useful (0 votes)
2 views

Operator Overloading

Uploaded by

stunnerboy360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Operator Overloading

Uploaded by

stunnerboy360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Operator Overloading

C++ Program to Demonstrate OperatorOverloading

#include <iostream> void print()


{
class Complex { cout << real << " + i" << imag <<
private: '\n';
}
int real, imag;
};

public: int main()


{
Complex(int r = 0, int i = Complex c1(10, 5), c2(2, 4);
0) Complex c3 = c1 + c2;
{ c3.print();
return 0;
real = r;
}
imag = i;
Overloading New and Delete
operator in c++
The new and delete operators can also be
overloaded like other operators in C++. New and
Delete operators can be overloaded globally or
they can be overloaded for specific classes.
• If these operators are overloaded using
member function for a class, it means that
these operators are overloaded only for that
specific class.
• If overloading is done outside a class (i.e. it
is not a member function of a class), the
overloaded ‘new’ and ‘delete’ will be called
The syntax for overloading the new operator :
void* operator new(size_t size);
The overloaded new operator receives size of
type size_t, which specifies the number of bytes
of memory to be allocated. The return type of the
overloaded new must be void*.The overloaded
function returns a pointer to the beginning of the
block of memory allocated.
Syntax for overloading the delete operator :
void operator delete(void*);

The function receives a parameter of type void*


CPP program to demonstrate Overloading new and delete operator
for a specific class

#include<iostream> void * operator new(size_t size)


#include<stdlib.h> {
class student cout<< "Overloading new operator with size: " << size <<
{ endl;

string name; void * p = ::operator new(size);

int age; //void * p = malloc(size); will also work fine

public: return p;

student() }

{ void operator delete(void * p)


cout<< "Constructor is called\n" ; {
} cout<< "Overloading delete operator " << endl;
student(string name, int age) free(p);
{ }
this->name = name; };
this->age = age; int main()
} {
void display() student * p = new student("Yash", 24);
{
p->display();
cout<< "Name:" << name << endl;
delete p;
cout<< "Age:" << age << endl;
}
}
Overloading the Comma Operator
The comma operator (, ) is used to isolate two or
more expressions that are included where only
one expression is expected. When the set of
expressions has to be solved for operands, only
the rightmost expression is considered.
Examples:

Input: x = (y = 5, y + 2)
Output: x = 7, y = 5
Explanation:
C++ program to illustrate the
overloading the comma operator
#include <iostream> comma operator+(comma
ob2);
class comma { comma operator,
int x, y; (comma ob2);
};
public:
// Default Constructor // Function to overload the
+ operator
comma() {}
comma comma::operator+
(comma ob2)
// Parameterized
{
Constructor
comma temp;
// Function to overload // Driver code
the, operator int main()
comma comma::operator, {
(comma ob2) // Initialize objects
{ comma ob1(10, 20),
comma temp; ob2(5, 30), ob3(1, 1);

// Update the value temp ob1.display();


temp.x = ob2.x; ob2.display();
temp.y = ob2.y; ob3.display();

You might also like