0% found this document useful (0 votes)
5 views3 pages

Notes

The document explains unary and binary operators in C++, providing examples of operator overloading for both types, including incrementing a value and adding two objects. It also covers the assignment operator and how to overload it to copy object data. Additionally, it discusses overloading the new and delete operators to customize memory allocation and deallocation for class objects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Notes

The document explains unary and binary operators in C++, providing examples of operator overloading for both types, including incrementing a value and adding two objects. It also covers the assignment operator and how to overload it to copy object data. Additionally, it discusses overloading the new and delete operators to customize memory allocation and deallocation for class objects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1)What is a Unary Operator?

A Unary operator works on only one operand (one object).

Examples in normal C++:

++a; // Increment

--a; // Decrement

-a; // Negative

These use only one variable, so they are called Unary Operators.

Simple Example: Overloading ++ Operator

👉 Program: Increment Object Value

#include <iostream>

using namespace std;

class Number {

public:

int value;

Number(int v) {

value = v;

// Unary Operator Overloading

void operator ++ () {

value = value + 1;

};

int main() {

Number n1(10);

++n1; // Unary operator call

cout << "Value after increment: " << [Link];

return 0;

o/p:11

2) What is a Binary Operator?

A Binary operator works on two operands (two objects).

Examples in normal C++:

a +b
a -b
a *b
a /b

👉 These use two variables, so they are called Binary Operators.

Binary Operator Overloading Meaning

Binary operator overloading means:

Giving special meaning to operators like +, -, *, etc. for class objects.

💻 Simple Example: Overloading + Operator

👉 Program: Add Two Objects


#include <iostream>
using namespace std;
class Number {
public:
int value;
// Constructor
Number(int v) {
value = v;
}
// Binary Operator Overloading
Number operator + (Number obj) {
Number temp(0);
[Link] = value + [Link];
return temp;
}
};

int main() {
Number n1(10);
Number n2(20);
Number n3 = n1 + n2; // Binary operator call
cout << "Addition: " << [Link];
return 0;
}

o/p:30

What is Assignment Operator?

The assignment operator (=) is used to copy one object’s data into another.

Normal example:

a = b;

For class objects:

obj1 = obj2;

Simple Example: Overloading = Operator

👉 Program: Copy One Object to Another

#include <iostream>
using namespace std;

class Number {
public:
int value;

// Constructor
Number(int v) {
value = v;
}

// Assignment Operator Overloading


void operator = (Number obj) {
value = [Link];
}
};

int main() {
Number n1(10);
Number n2(20);

n1 = n2; // Assignment operator call

cout << "Value of n1: " << [Link];

return 0;
} o/p: 20

What is Overloading new and delete?

Normally:

int *ptr = new int;


delete ptr;
C++ automatically allocates and frees memory.

But…

👉 We can customize how memory is allocated and deallocated


👉 That is called overloading new and delete operators

So simply:

Overloading new and delete means defining our own memory allocation and deallocation method for a class.

Simple Example Program

👉 Overloading new and delete

#include <iostream>

#include <cstdlib>

using namespace std;

class Sample {

public:

int x;

// Overloading new operator

void* operator new(size_t size) {

cout << "Memory Allocation using overloaded new\n";

void* ptr = malloc(size);

return ptr;

// Overloading delete operator

void operator delete(void* ptr) {

cout << "Memory Deallocation using overloaded delete\n";

free(ptr);

};

int main() {

Sample* obj = new Sample; // Calls overloaded new

delete obj; // Calls overloaded delete

return 0;

o/p: Memory Allocation using overloaded new

Memory Deallocation using overloaded delete

You might also like