Vous êtes sur la page 1sur 10

operator

this
static

OVERLOADING

pksa, CSE dept of NIT Rourkela

Overloading Operators
C++ incorporates the option to use language standard
operators between classes in addition to between
fundamental types.
For example:
int a, b, c;
a = b + c;
is perfectly valid.

But
struct fruit{
char name[50];
float price;
}a, b, c;
a = b + c;
This operation is not a valid
operation.
pksa, CSE dept of NIT Rourkela

But C++ has the ability to overload operators.


Objects derived from composed types such as the
previous one can accept operators which would not
be accepted otherwise, and we can even modify the
effect of operators that they already admit.
To overload an operator we only need to write a
class member function whose name is operator
followed by the operator sign that we want to
overload, following this prototype:
type operator sign (parameters);
pksa, CSE dept of NIT Rourkela

#include <iostream>
using namespace std;

class Point{
int x,y;
public:
Point (int a, int b){
x = a; y = b;

distance
between
two points

float operator-(Point A){


float d;
d = sqrt((x-A.x)*(x-A.x)+(y-A.y)*(y-A.y));
return(d);
}
};

int main () {
Point p(3,1),q(1,2); float distance;
distance = p - q;
cout <<distance;
distance = p.operator-(q);
return 0;
}

pksa, CSE dept of NIT Rourkela

this
The keyword this represents within a class the
address in memory of the object of that class that is
being executed. It is a pointer whose value is
always the address of the object.
It can be used to check if a parameter passed to a
member function of an object is the object itself.

pksa, CSE dept of NIT Rourkela

Usage of this
The previous program to find distance between
two points may be written the following way.

float operator-(Point A){


float d;
d = sqrt((this->x - A.x) * (this->x - A.x)+
(this->y - A.y) * (this->y - A.y));
return(d);
}

pksa, CSE dept of NIT Rourkela

static

A class can contain static members, either data or


methods.

Static data members of a class are also known as


"class variables", because their content does not
depend on any object.

There is only one unique value for all the objects


of that same class.

pksa, CSE dept of NIT Rourkela

class Shared
int b;
static int a;
Instantiated
Object x
b

Object y
b
Class
Variable
a

Common to x & y

pksa, CSE dept of NIT Rourkela

#include <iostream>
using namespace std;
class Shared{

};

Sharing member variables


among objects using static

int b;
static int a;//no storage for a.
public:
void set(int i,int j){a = i; b = j;}
void show();

int Shared::a; //define a, allocate storage.


void Shared::show(){
cout<<(This is static) a: << a;
cout<<(This is non-static) b: << a <<endl;
}

int main () {
Shared x,y;
x.set(1,1);
y.set(2,2);
x.show();
return 0;

x.show();
y.show();
pksa, CSE dept of NIT Rourkela

Programs to be done using


operator overloading
1. Read date of birth of a person and todays
date and find age of the person.
2. Define a class with feet and inches as
member variables and write a program to
add two such objects.
3. Overload unary operators ++ and -- to
increment and decrement an object in
question # 3.
4. Multiply two matrices.
pksa, CSE dept of NIT Rourkela

Vous aimerez peut-être aussi