Académique Documents
Professionnel Documents
Culture Documents
this
static
OVERLOADING
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
#include <iostream>
using namespace std;
class Point{
int x,y;
public:
Point (int a, int b){
x = a; y = b;
distance
between
two points
int main () {
Point p(3,1),q(1,2); float distance;
distance = p - q;
cout <<distance;
distance = p.operator-(q);
return 0;
}
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.
Usage of this
The previous program to find distance between
two points may be written the following way.
static
class Shared
int b;
static int a;
Instantiated
Object x
b
Object y
b
Class
Variable
a
Common to x & y
#include <iostream>
using namespace std;
class Shared{
};
int b;
static int a;//no storage for a.
public:
void set(int i,int j){a = i; b = j;}
void show();
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