Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label Operator Overloading. Show all posts
Showing posts with label Operator Overloading. Show all posts

Operator Overloading: Overload << and >> operator


Overload << and >> operator program is given below:

// operator overloading
// overloading << and >> operator
#include "stdafx.h" //Delete this line if it creates error
#include <iostream>
using namespace std;
const int LEN = 80;           //maximum length of names
////////////////////////////////////////////////////////////////
class employee                        //employee class
{
private:
char name[LEN];                 //employee name
unsigned long number;
public:
friend istream& operator >> (istream& s, employee& e);
friend ostream& operator << (ostream& s, employee& e);
};
//-------------------------------------------------------------
istream& operator >> (istream& s, employee& e){
cout << "Name: "; s.getline(e.name,LEN,'\n');
cout << "Number: "; s >> e.number;
return s;
}
//-------------------------------------------------------------
ostream& operator << (ostream& s, employee& e){
cout << "Name: " << e.name << endl
<< "Number: " << e.number << endl;
return s;
}
///////////////////////////////////////////////////////////////
int main(){
employee oe; // oe => object of class employee.
cin >> oe;
cout << oe;
return 0;
}

Overloading Subscript operator [] C++


#include <iostream>
using namespace std;

class a{
public:
double& operator[](int index);
double get(int index);//returns value at required index.
private:
double array[20];
};

double& a::operator[](int index){
return array[index];
}

double a::get(int index){
return array[index];
}

void main(){
a obj;
obj[5] = 4;

cout << "obj[5] = " << obj.get(5) << endl;

double var = obj[5];
cout << "var = " << var << endl;
}

Overload << operator C++

Overload << Operator in C++.

#include <iostream>
using namespace std;

class a{
friend ostream& operator<<(ostream out, a& obj);

int x; //private
public:
int getX(){return x;}
void setX(int a){
x = a;
}
};

ostream& operator << (ostream& out, a& obj){
out << "obj.x = " << obj.x << endl;
return out;
}

int main(){
a obj1, obj2;
obj1.setX(5);//Functions are called over objects.
cout << obj1;
obj2.setX(10);
cout << obj2;
return 0;
}

//If this Code doesn't work in some compiler, use following code:


Operator Overloading C++

//Adding two objects of different classes.

//This example of operator overloading uses friend functions and friend class to set and get the value of 'x', a private member.
//Before this example, see an easier example.

#include <iostream>
using namespace std;

class b{
int x;
public:
friend void setX(b& obj_b, int value);
friend int getX(b& obj_b);
friend class a;
};
class a{
int x;
public:
friend void setX(a& obj_a, int value);
friend int getX(a& obj_a);
int operator+(b &obj);
};

int main(){
a obj_a;
b obj_b;
setX(obj_a,5);
setX(obj_b,10);
cout << "obj_a = " << getX(obj_a) << endl;
cout << "obj_b = " << getX(obj_b) << endl;
cout << "\nSUM of objects of different classes: " << obj_a + obj_b << endl;
return 0;
}

int a::operator+(b &obj){
return x + obj.x;
}

void setX(a& obj_a, int value){
obj_a.x = value;
}
void setX(b& obj_b, int value){
obj_b.x = value;
}
int getX(a& obj_a){
return obj_a.x;
}
int getX(b& obj_b){
return obj_b.x;
}

Operator Overloading C++

Operator Overloading C++
Adding two objects of same class using + sign.


#include <iostream>
using namespace std;
class a{
int x;
public:
void setX(int);
int getX();
int operator+ (a &obj);
};
int main(){
a obj1, obj2;
obj1.setX(5);
cout << "Obj1.x = " << obj1.getX() << endl;
obj2.setX(10);
cout << "Obj2.x = " << obj2.getX() << endl;
  cout <<"\nObj1+Obj2 = " << obj1+obj2 << endl;
return 0;
}

int a::operator+ (a& obj){
return (x + obj.x);
}
void a::setX(int value){
x = value;
}
int a::getX(){
return x;
}

Another Example.
"Don't let anyone ever make you feel like you don't deserve what you want."