Type of Operator | No. of Arguments |
Unary | 0 |
Binary | 1 |
return-type class-name :: operator OP(argument list)
{
Operations that overloaded operator performs
}
2. Write a program in C++ to overload unary minus(-).Solution :#include<iostream> using namespace std ; class Example { int x,y,z; public:
Example(int p,int q,int r) { x=p; y=q; z=r; } void display() { cout<<x<<” ”<<y<<” ”<<z; } void operator-() { x=-x; y=-y; z=-z; } }; int main() { Example obj (10,-20,30); cout<<”Original numbers are” obj.display(); - obj; obj.display(); return 0; } |
//Defines class Example
//Parameterized constructor
// Step 1 Operator function to overload minus
// Step 2 Invokes operator function
cout<<”Numbers after applying unary minus” |
#include<iostream> using namespace std ; class Example { int x,y,z; public: Example(int p,int q,int r) { x=p; y=q; z=r; } void display() { cout<<x<<” ”<<y<<” ”<<z; } friend void operator-(Example &O); friend void operator++(Example &O); friend void operator--(Example &O); }; void operator-(Example & O) { O.x=-O.x; O.y=-O.y; O.z=-O.z;
} void operator++(Example & O) { O.x=O.x++; O.y=O.y++; O.z=O.z++; }
void operator--(Example & O) { O.x=O.x--; O.y=O.y--; O.z=O.z--; } }; int main() { Example O(10,-20,30); cout<<”Original numbers are”; O.display(); - O; cout<<”Numbers after applying unary minus”; O.display(); ++ O; cout<<”Numbers after applying ++”; O.display(); -- O; cout<<”Numbers after applying --”; O.display(); return 0 ; } |
//Defines class Example
//Parameterized constructor
//Operator functions to overload
//Invokes operator function for –
//Invokes operator function for ++
//Invokes operator function for -- |
#include<iostream>
using namespace std;
class Employee
{
int id;
int salary;
public:
Employee(int x, int y)
{
id=x;
salary=y;
}
void display()
{
cout<<”Employee id:”<<id<<endl;
cout<<”Employee salary:”<<salary<<endl;
}
void *operator new(size_t size );
void operator delete(void *p);
};
void *Employee::operator new(size_t size )
{
void *p;
cout<<”Overloading new”<<endl;
p=malloc(size);
if(!p)
{
bad_alloc b;
throw b;
}
return p;
}
void Employee:: operator delete(void *p)
{
cout<<”Overloading delete”<<endl;
free(p);
}
int main()
{
Employee *E1,*E2;
try
{
E1= new Employee(10,50000);
}catch(bad_alloc ba)
{
cout<<”Allocation error for E1”;
return 1;
}
try
{
E2= new Employee(11,60000);
}catch(bad_alloc ba)
{
cout<<”Allocation error for E2”;
return 1;
}
E1->display();
E2->display();
delete E1;
delete E2;
return 0;
}
OutputOverloading new
Overloading new
10 50000
11 60000
Overloading delete
Overloading delete
8. Explain the rules for operator overloading? Operators which cannot be overloaded All C++ operators can be overloaded except followingFollowing operators cannot be overloaded using friends
int add(int x, int y);
float add(int x, int y);
Here add functions have different return types but they have same argument list. So they are not overloaded functions. Thus return type does not play any role in function overloading.Sr. No. | Function Protoypes | Function calls |
1. | void accept(int x); | accept(10); |
2. | void accept(float y); | accept(1.5); |
3. | void accept(int x, int y); | accept(10,20); |
#include<iostream> using namespace std; template <class T> void bubblesort(T A[], int n) { for(int i=0;i<=n-1;i++) for(int j=n-1;i<j;j--) if(A[j]<A[j-1]) { swap (A[j], A[j-1]) } template <class X> void swap(X &p, X &q) { X temp=p; p=q; q=temp; }
int main() { int array[5]; cout<<“Enter elements of array”; for(int i=0;i<5;i++) { cin>>a[i]; } bubblesort(array,5); cout<<“Sorted array”; for(int i=0;i<5;i++) { cout<<array[i]<<“”; return 0; } |
//template function for bubble sort
// calls template function for swap
// template function for swap
//accept array elements for sorting
//calls template function for sorting |
Enter elements of array
25 15 30 10 50
Sorted array 10 15 25 30 50