Overloading

6. Assignment operators overloading
You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor.
Following example explains how an assignment operator can be overloaded.
#include < iostream >
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
void operator=(const Distance &D )
{
feet = D.feet;
inches = D.inches;
}
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2(5, 11);
cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
D1 = D2;
cout << "First Distance :";
D1.displayDistance();
return 0;
}
Output: -
First Distance : F: 11 I:10
Second Distance :F: 5 I:11
First Distance :F: 5 I:11
7.Function call () operator overloading
The function call operator () can be overloaded for objects of class type. When you overload ( ), you are not creating a new way to call a function. Rather, you are creating an operator function that can be passed an arbitrary number of parameters.
Following example explains how a function call operator () can be overloaded.
#include < iostream >
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
Distance operator()(int a, int b, int c)
{
Distance D;
D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2;
cout << "First Distance : ";
D1.displayDistance();
D2 = D1(10, 10, 10);
cout << "Second Distance :";
D2.displayDistance();
return 0;
}
8.Subscripting [] operator overloading
The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays.
Following example explains how a subscript operator [] can be overloaded.
#include < iostream >
using namespace std;
const int SIZE = 10;
class safearay
{
private:
int arr[SIZE];
public:
safearay()
{
register int i;
for(i = 0; i < SIZE; i++)
{
arr[i] = i;
}
}
int &operator[](int i)
{
if( i > SIZE )
{
cout << "Index out of bounds" < return arr[0];
}
return arr[i];
}
};
int main()
{
safearay A;
cout << "Value of A[2] : " << A[2] < cout << "Value of A[5] : " << A[5]< cout << "Value of A[12] : " << A[12]< return 0;
}
output : -
Value of A[2] : 2
Value of A[5] : 5
Index out of bounds
Value of A[12] : 0
9. Class member access operator -> overloading
The class member access operator (->) can be overloaded but it is bit trickier. It is defined to give a class type a "pointer-like" behavior. The operator -> must be a member function. If used, its return type must be a pointer or an object of a class to which you can apply.
The operator-> is used often in conjunction with the pointer-dereference operator * to implement "smart pointers." These pointers are objects that behave like normal pointers except they perform other tasks when you access an object through them, such as automatic object deletion either when the pointer is destroyed, or the pointer is used to point to another object.
The dereferencing operator-> can be defined as a unary postfix operator. That is, given a class:
#include < iostream >
#include
using namespace std;
class Obj {
static int i, j;
public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
};
int Obj::i = 10;
int Obj::j = 12;
class ObjContainer {
vector a;
public:
void add(Obj* obj)
{
a.push_back(obj);
}
friend class SmartPointer;
};
class SmartPointer {
ObjContainer oc;
int index;
public:
SmartPointer(ObjContainer& objc)
{
oc = objc;
index = 0;
}
bool operator++()
{
if(index >= oc.a.size()) return false;
if(oc.a[++index] == 0) return false;
return true;
}
bool operator++(int)
{
return operator++();
}
Obj* operator->() const
{
if(!oc.a[index])
{
cout << "Zero value";
return (Obj*)0;
}
return oc.a[index];
}
};
int main() {
const int sz = 10;
Obj o[sz];
ObjContainer oc;
for(int i = 0; i < sz; i++)
{
oc.add(&o[i]);
}
SmartPointer sp(oc);
do {
sp->f();
sp->g();
} while(sp++);
return 0;
}
Output
10
12
11
13
12
14
13
15
14
16
15
17
16
18
17
19
18
20
19
21
Restrictions on overloaded functions
You cannot overload the following function declarations if they appear in the same scope. Note that this list applies only to explicitly declared functions and those that have been introduced through using declarations:
* Function declarations that differ only by return type. For example, you cannot declare the following declarations:-
int f();
float f();
* Member function declarations that have the same name and the same parameter types, but one of these declarations is a static member function declaration. For example, you cannot declare the following two member function declarations of f():
struct A {
static int f();
int f();
};
* Member function template declarations that have the same name, the same parameter types, and the same template parameter lists, but one of these declarations is a static template member function declaration.
* Function declarations that have equivalent parameter declarations. These declarations are not allowed because they would be declaring the same function.
* Function declarations with parameters that differ only by the use of typedef names that represent the same type. Note that a typedef is a synonym for another type, not a separate type. For example, the following two declarations of f() are declarations of the same function:
typedef int I;
void f(float, int);
void f(float, I);
* Function declarations with parameters that differ only because one is a pointer and the other is an array. For example, the following are declarations of the same function:
f(char*);
f(char[10]);
* The first array dimension is insignificant when differentiating parameters; all other array dimensions are significant. For example, the following are declarations of the same function:
g(char(*)[20]);
g(char[5][20]);
The following two declarations are not equivalent:
g(char(*)[20]);
g(char(*)[40]);
* Function declarations with parameters that differ only because one is a function type and the other is a pointer to a function of the same type. For example, the following are declarations of the same function:
void f(int(float));
void f(int (*)(float));
* Function declarations with parameters that differ only because of cv-qualifiers const, volatile, and restrict. This restriction only applies if any of these qualifiers appears at the outermost level of a parameter type specification. For example, the following are declarations of the same function:
int f(int);
int f(const int);
int f(volatile int);
* Note that you can differentiate parameters with const, volatile and restrict qualifiers if you apply them within a parameter type specification. For example, the following declarations are not equivalent because const and volatile qualify int, rather than *, and thus are not at the outermost level of the parameter type specification.
void g(int*);
void g(const int*);
void g(volatile int*);
The following declarations are also not equivalent:
void g(float&);
void g(const float&);
void g(volatile float&);
* Function declarations with parameters that differ only because their default arguments differ. For example, the following are declarations of the same function:
void f(int);
void f(int i = 10);
* Multiple functions with extern "C" language-linkage and the same name, regardless of whether their parameter lists are different.


Free Web Hosting