Chap-6 (C )
Chap-6 (C )
Chap-6 (C )
We know that operators such are +, -, *, /, <=, >= are work with a built in data type (int,
float, double), but these operators are not work directly with user defined data types
(objects).
For example, suppose a, b and c are built in data types like int and float, and if we are
doing c = a + b ; then it works perfectly. But if when a, b and c are objects of a user
defined class then it will gives errors when compile.
So using overloading an operator, you can make above statements valid even if a, b and c
are objects (user defined).
So the term operator overloading refers to giving the basic C++ operators provide
additional or special meanings when they are applied to user defined data types.
So, the mechanism of giving such special meaning to an operator is known is known as
operator overloading.
The operator overloading can be defined using special function, called operator function.
The general form (or syntax) of an operator functions are,
In above syntax, the return type is the type of value returned by this operator function, the
operator is a keyword for defining operator function. The OP is an operator (+, - *, <, >)
being overloaded.
1. An operator function can be either a non static member function or a non member
function with at least on parameter that has class, reference to class, enumeration or
reference to enumeration type.
2. You cannot change the precedence, or the number of operands of an operator.
3. An overloaded operator cannot have default arguments in the argument list.
4. Only existing operators can be overloaded, new operators cannot be created.
5. We cannot change the basic meaning of an operator, it means we cannot redefine the plus
(+) operator to subtract one value from other.
6. In case of unary operators member functions have no arguments and for Binary operators
member function have one argument.
7. In case of unary operator friend function have one argument and for Binary operators
friend function have two arguments.
8. The binary arithmetic operators such are +, -, * must be return a value.
9. There are some operators that cannot be overloaded such are,
= : Assignment operator
() : function call operator
[] : subscripting operator
-> : class member access operator
An operator which work with only single operand is called unary operators, such are +, -,
++, -- operators.
Let us consider to overload increment operator ++, which will increment value of object
in same way of basic data type.
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
test()
{
x=0;
}
void operator ++() // ++ operator function
{
++x;
}
void show()
{
cout << "The value is: " << x << endl ;
}
};
main()
{
clrscr();
test t1,t2;
++t1; // call operator function for t1 object
t1.show();
++t2; // call operator function for t2 object
++t2;
++t2;
t2.show();
getch();
return 0;
}
Example 2 (To overload an Unary ++ Operator, which will return value form the
operator function)
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
test()
{
x=0;
}
void show()
{
cout << "The value is: " << x << endl;
}
test operator ++() // ++ operator function with
// return type
{
test temp;
temp.x = ++x;
return temp;
}
};
Output:
The value is: 1
The value is: 1
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void getdata(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void operator -() // - operator function
{
x = -x;
y = -y;
z = -z;
}
void show()
{
cout<<x<<" "<<y<<" "<<z << endl;
}
};
Output:
Original values are:
10 -20 30
After invoke operator values are:
-10 20 -30
An operator which work with two operands are called binary operators, such are +, -, *,
= =, >=, etc.
Let us consider to overload binary + operator, which will doing addition of two objects
object1 + object2 in same way of basic data types such as a + b.
Example 1 (To overload Binary + Operator to perform the operation Obj1 + Obj2)
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
void getdata()
{
cout<<"Enter Value: ";
cin>>x;
}
In above program, if we write the statement t1 + t2 in the main ( ) function, then it will
call the operator function void operator + (test p) and perform the addition of two
objects values and stores in the t1 object.
So t1 + t2 is same as the t1 = t1 + t2.
Obj2 )
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
void getdata()
{
cout << "Enter Value: " ;
cin >> x ;
}
void show()
{
cout<<"The sum of value is: = "<<x<<endl;
}
};
void main()
{
clrscr();
test t1,t2,t3 ;
t1.getdata();
t2.getdata();
t3.show();
getch();
}
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
test() // default constructor
{
x=0;
}
test(int a) // parameterized constructor
{
x=a;
}
test operator + (test p) // + operator function
{
test temp1;
temp1.x = x + p.x;
return temp1;
}
test operator - (test q) // - Operator function
{
test temp2;
temp2.x = x - q.x;
return temp2;
}
void show()
{
cout<<"Value is: "<<x<<endl;
}
};
Output:
Value is: 5
Value is: 4
Value is: 3
Value is: 2
Answer of Value is: 8
In case of Unary Operators such are (+, -, ++, --), friend function have one argument and
for Binary operator such are (+, -, *, = =), friend function have two arguments
Let us see the example of overload binary + operator using friend function.
Example (To overload Binary + Operator using Friend Function to perform the
#include<iostream.h>
#include<conio.h>
class test
{
int x;
public:
void getdata()
{
cout << "Enter Value: " ;
cin >> x ;
}
void show()
{
cout<<"The sum of value is: = "<<x<<endl;
}
};
test operator +(test p, test q)//For binary operator
//friend function use 2 arguments
{
test temp ;
temp.x = p.x + q.x ;
return temp ;
}
void main()
{
clrscr();
t1.getdata();
t2.getdata();
t3.show();
getch();
}
Output:
Enter Value: 9
Enter Value: 3
The sum of value is: = 12
In the above program, we use friend function, so an operator function has two arguments
which are object type and return type is also object type.
In operator function we define three objects p, q and temp which are dummy object
parameters of actual object parameter t1, t2 and t3.
The operator function returns a value to the object t3.
We know that string is nothing but a character array, it contains number of characters.
Basic string manipulation operations are:
1. String Concatenation (Combine two string together)
2. String Comparison
We can perform operation like c = a + b if a, b and c are built in data types like integer
type. But if the data type is string and we are doing same operation then compiler gives
error.
It is not possible two add two string, but if we define user defined data type (objects) and
use the concept of operator overloading and perform same operation, it will gives the
result which will combined two string together.
The string manipulation operation performed by using new operator which will save the
space for entering exact characters in the character string.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *s;
int len;
public:
string() // default constructor for null string
{
s="";
len=0;
}
string (char *s1)
{
len = strlen(s1);
s = new char[len+1]; // provide exact location
// to string s
strcpy(s,s1);
}
Output:
String is: Comp
String is: uter
String is: Computer
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *s;
int len;
public:
string() // default constructor for null string
{
s="";
len=0;
}
string (char *s1)
{
len = strlen(s1);
s = new char[len+1]; // provide exact location
// to string s
strcpy(s,s1);
}
int operator >= (string s5)
{
int a = strlen(s) ;
int b = strlen(s5.s) ;
if (a >= b)
return (1);
else
return (0);
}
void show()
{
cout<<"String is: "<<s<<endl;
}
};
main()
{
clrscr();
string str1 = "Computer";
string str2 = "Application" ;
Output:
String is: Computer
String is: Application
String 1 is less than string 2
Example 3 (To Overload Binary = = Operator for sting to perform the string
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *s;
int len;
public:
string() // default constructor for null string
{
s="";
len=0;
}
string (char *s1)
{
len = strlen(s1);
s = new char[len+1]; // provide exact location
// to string s
strcpy(s,s1);
}
if (strcmp(s,s5.s)==0)
return 1;
else
return 0;
}
void show()
{
cout<<"String is: "<<s<<endl;
}
};
main()
{
clrscr();
string str1 = "Computer";
string str2 = "Computer" ;
str1.show();
str2.show();
if(str1 == str2)
cout<<"String 1 is equal to String 2";
else
cout<<"String 1 is not equal to String 2";
getch();
return 0;
}
Output:
String is: Computer
String is: Computer
String 1 is equal to String 2
We know that the = operator assign the value from one variable from right side to another
on left side in a statement, for example
int a, b ;
a = b ;
If the variables are built in data type then no problem, compilers cant give any errors.
In case of class objects, the value of all the data members of the right hand objects are
simply copied into the corresponding members of the objects on the left side.
For example, a class student has two objects s1 and s2, and it have two data members
name and age.
Then we write a statement s1 = s2 ; the value of two data members of the right hand
objects s2 are simply copied into members of the objects on the left hand s1. This
operation is possible only if two objects are same class.
But if one operand (variable) is object type and another operand is built in type variable?
or if they are object of different class then compilers complaints and give errors and
above operation are not possible.
So, for user defined data types do not support automatic type conversion, compiler
complains errors, so a conversion routine must be design for such type of operation.
Three types of situation may be arise in the data conversion, the compiler does not
compile, so we must be write a routine or operator function to perform these operation.
The situation was:
But if we have to perform these three operations, we must be write a conversion routine
as describe below.
The conversion from basic type to class type can be performed by using only one argument
constructor write in a class and this is called conversion constructor. Let us see the example
of convert basic type to class type.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class test
{
int len;
char *s;
public:
test()
{
len=0;
s=NULL;
}
test(char *s1) // called conversion constructor which
{ // will convert basic type to class type
len = strlen(s1);
s = new char(len+1);
strcpy(s,s1);
}
void show()
{
cout<<"String is: "<<s<<endl;
}
};
Output:
String is: Kishan
In above program, the statement str1 = name ; is convert a name (character type data)
to str1 a class type (user defined).
The constructor function does not support this type of conversion, for this type of conversion
we must be writing an overloaded casting operator.
An overloaded casting operator convert Class to Basic type conversion, it is also called
conversion function.
The syntax of conversion function is,
For example, the overloaded cast operator operator int ( ) convert a class type object to int
type.
The overloaded cast operator operator float ( ) convert a class type object to float type.
The overloaded cast operator operator double ( ) convert a class type object to double type.
Example (To demonstrate Class to Basic type conversion using conversion function)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class test
{
int len;
char *s;
public:
test()
{
len=0;
s=NULL;
}
test(char *s1)
{
len = strlen(s1);
s = new char(len+1);// provide exact location to string s
strcpy(s,s1);
}
operator char *()
{
return s;
}
void show()
{
cout<<"String is: "<<s<<endl;
}
};
str1.show();
cout<<endl<<name;
getch();
return 0;
}
Output:
String is: Rajesh
Rajesh
If a class name test1 have object t1 and class test2 have object t2, then we can write a
statement like,
t1 = t2 ; // two different class objects
The class test2 type data is converted to the class test1 type data and converted value is
assigned to the object t1. In above case we can say that conversion takes place from class
test2 to test1, where test2 is called source class and class test1 is called destination class.
If we have to perform above conversion, we must be write an either conversion constructor
or conversion function.
So there are two methods used for class to class type conversion, which is depending upon
the conversion routine write in either source class or destination class.
If conversion routine writes in a source class, then we use a cast operator that will write in
a source class to perform above operation.
If conversion routine writes in a destination class then we use a conversion constructor that
will write in a destination class to perform above operation.
#include<iostream.h>
#include<conio.h>
class test1
{
int x;
public:
test1()
{
x = 0 ;
}
test1(int a)
{
x = a ;
}
void show()
{
cout<<"Value of x is: "<< x ;
}
};
class test2
{
int y;
public:
test2()
{
y = 0 ;
}
test2(int b)
{
y = b ;
}
Output:
Value of y is: 15
Value of x is: 15
In above program, we write casting operator in a test2 class which is source class, that
will convert a class destination object (t1) to source class object (t2).
So a statement t1 = t2 ; will indicate the conversion of one class object to another.
#include<iostream.h>
#include<conio.h>
class test2
{
int y;
public:
test2()
{
y = 0 ;
}
test2(int b)
{
y = b ;
}
void show()
{
cout<<"Value of y is: "<< y << endl ;
}
int getdata()
{
return y;
}
};
class test1
{
int x;
public:
test1()
{
x = 0 ;
}
test1(int a)
{
x = a ;
}
void show()
{
cout<<"Value of x is: "<< x ;
}
x = t.getdata();
}
};
main()
{
clrscr();
test1 t1;
test2 t2(10);
t2.show();
t1 = t2 ; // call conversion constructor
t1.show();
getch();
return 0;
}
Output:
Value of y is: 10
Value of x is: 10