Operations like arithmetic and
comparisons can be done only on basic data types and not on derived or
user-defined data types.
The ability to create new data
types, on which direct operations can be performed is called as extensibility
and ability to associate an existing operator with a member function and use it
with the objects of its class as its operands is called as operator
overloading, also called as polymorphism which means that one thing having many
forms. Polymorphism facilitates creating
multiple definitions for operators and functions in operator overloading.
The actual instructions of the
operation are written in a special member function defined with the keyword
operator, followed by operator, it self which is to be overloaded. Such a member function is called as operator
function. The general format for
operator overloading is as:
Return
type operator op(argument list);
Where op is the symbol for the
operator being overloaded. Op has to be
valid c++ operator, new symbol can not be used.
Example 1
class temp
{
private:
int
x, y;
public:
void
operator++(void);
};
void temp::operator++(void)
{
x++;
y++;
}
Overloading Unary Arithmetic
Operator:
Unary operators have only one
operand. There are three operators
increment operator (++), decrement operator (--), and minus operator (-).
Example 2.
class counter{
private:
int count;
public:
counter(void)
{
count=0;
};
void operator++(void)
{
++count;
};
int main (void)
{
counter cl;
cl++;
++cl;
}
With the increment or decrement
operators overloaded, operator function is executed first, regardless of
whether operator is postfix or prefix.
To use the increment operator with an object in an expression, the
operator function will have to return the incremented object.
Example 3
class counter{
private:
int count;
public:
counter(void){
count =0;
};
counter operator
++(void)
};
counter
counter::operator++(void){
counter temp; // create
temporary object
temp.count +=
++count // assigned
incremented value
return(temp); //return
the new object
};
void main(void){
counter c1,c2;
c2 =c1++ //
first increment to 1 and then assigns
}
In the above example the operator
function creates a new object temp of class counter, assigns incremented value
of count to the data member temp and returns new object. Another way is to create a nameless temporary
object and return int.
Another way of returning an
object from a member function is by using this pointer. this is the special pointer which points to
the object that invoked the member function.
E.g.
counter counter ::opertor++(void)
{
count ++;
return(*this);
}
Overloading Binary Arithmetic
Operators:
Binary operators having two
operators they can also be overloaded like unary operators.
Example 4
#include<string.h>
#include<stdio.h>
class string{
private:
char
s[100];
public:
string(void)
{
s[0]
=0;
}
string (Char str[])
{
strcpy(s,str);
}
void putstr(void)
{
printf(“%s”,&str);
}
string operator+(string s2){
string temp;
strcpy (temp.s,s);
strcat(temp.s,s2.s);.
return (temp);
}
};
void main(void){
string s1 = “Welcome”;
string s2 = “To World of Joy”;
string s3;
printf ( “\n s1 = ” );
s1.putstr( );
printf(“\n s2 = ”);
s2.putstr( );
s3= s1+s2; //concatenates two strings
printf(“\n s3 = ”);
s3.putstr( );
}
Overloading Compound
Assignment Operators:
Can be also be overloaded like
binary arithmetic operators.
Example 5
#include<string.h>
#include<string.h>
class string
{
private:
char
s[100];
public:
string(void)
{
s[0] =0;
};
string (char
str[])
{
strcpy (s,str);
};
void
putstr(void)
{
printf(“%s”,s);
};
string operator
+= (string s2);
};
string string::
operator +=(string s2)
{
strcat (s,s2.s);
}
return (*this);
};
void main(void)
{
string s1 =
“Mother”;
string s2 =
“money is very funny”;
string s3;
s3 = s1+s2;
printf(“s3:”);
s3.putstr( );
}
Overloading Comparison
Operators:
All comparison operators can be
overloaded to perform special operations on objects of a class.
Example 6
#include<stdio.h>
#include<string.h>
class string
{
private:
char
s[100];
public:
string(void)
{
s [0] = 0;
}
string (char
str[])
{
strcpy (s, str);
}
void
putstr(void)
{
printf(“%s”, s);
}
int operator
< (string s2)
{
return(strcmp
(s,s2.s)<0);
}
int
operator>(String s2)
{
return(strcmp(s,s2.s)>0);
}
int(operator=
=(string s2)
{
return(strcmp
(s,s2.s)= =0);
}
};
void main(void)
{
string s1 =
“Azad”;
string s2 =
“Bharat ki Dastaan”;
if(s1= = s2)
{
s1.putstr( );
printf(“=”);
s2.putstr( );
}
else if
(s1<s2)
{
s1.putstr( );
printf(“<”);
s2.putstr( );
}
else if
(s1>s2)
{
s1.putstr( );
printf(“>”);
s2.putstr( );
}
Pointers in
Operator Overloading:
Can be done as
following example:
Example
7
#include<stdio.h>
#include<conio.h>
#include<string.h>
class string{
char *cptr;
int size;
public:
string(char *s= “ ”){
size
= strlen(s);
cptr
= new char[size+1];
ctrcpy
= (cptr, s);
};
~string(void)
{
delete
cptr;
}
void
putstr(void)
{
printf(cptr);
}
};
void
main(void){
string
s1 (“Hello world …..\n”);
string
s2;
s2
= s1;
s1.putstr(
);
s2.putstr(
);
}
This program will return a null
pointer at the end. Hence the problem
can be solved by including operator function:
string operator =string (s2){
delete cptr;
size =strlen (s2.cptr);
cptr = new char [size.1];
strcpy (cptr,s2.cptr);
return(*this );
}
If there is any problem of
assignment, it was solved with an operator function for assignment
operator. Problem of initialization is
solved by defining a constructor function with an object as its argument. This constructor is called as copy
constructor. It can be called in three
context :-
1..When an object of a class is
initialized to another object of the same class
2.. when an object is passed by
value as an argument to a function.
3.. when a function returns an
object.
All three situations result in a
copy of one object to another. Hence a
copy constructor is very important if the class contains pointers as data
members. Copy constructor can defined
as:
string ::string(string s2){
size = s2.size;
cptr = new char [size +1];
strcpy = (cptr,s2.cptr);
};
Conversion Functions:
Are member functions used to
convert objects to or from basic data types and for conversions between objects
of different classes. Variables of basic
data types can be converted using built in conversion routines. However since compiler does not know anything
about user defined types program has to define conversion functions.
If the assignment operator is not
overloaded then it uses the constructor to the conversion. Conversion of one basic data type to another
is automatically done by the compiler using its own built in routines.
Eg. Ivar = Fvar;
Since compiler does not know
anything about user-defined type, it has to be instructed if an object has to
be converted to a basic data type. These
instructions are coded in a conversion function and defined as a member of that
class.
Example 8
#include<stdio.h>
class distance{
private:
int
feet;
float inches;
public:
distance(void){
feet = 0;
inches = 0.0;
}
distance (float
meters)
{
float f;
f = 3.28 *
meters;
feet = int(f);
inches =
12.*(f-feet);
}
void
showdist(void){
printf(“%d feet
and %f Inches \n”, feet, inches);
}
operator
float(void){
float f;
f = inches /12;
f += float(feet);
return( f/3.28);
}
};
void
main (void){
distance d1 =
1.25; // Uses
second constructor
distance d2; // uses
first constructor
float m;
d2 = 2.0;
printf (“1.25
meters is”);
d1.showdist( );
printf(“2.0
meters is”);
d2.showdist( );
printf(“Converted back to
meters”);
m
= float(d1); //calls
conversion function
printf(“\n
d1 = % 2f\n”,m);
m
= d2 ; //calls
conversion function
printf(“\n
d2 = % 2f\n”,m);
}
The conversion function contains
operator keyword and instead of operator symbol it contains data type. First compiler checks for an operator
function for assignment operator and if not found it uses conversion
function. The conversion function must
not define a return nor should it have any arguments.
Conversion between objects of
Different classes:
Conversion of an object of one
class to an object of another class can be done using assignment operator, but
since compiler does not know anything about a user-defined type, conversion
instructions are to be specified in a function.
This function can be a member function of the source class; or of
destination class
e.g.
Object
a = object b;
Conversion of objects of two
different classes can be achieved with two methods by giving or using one argument constructor or with conversion
function. The conversion function are
typically defined in source class and one argument constructors are defined in
destination class.
Conversion function in Source
Class :
Can be explained with the
following example:
Example 9
# include<stdio.h>
class distfeet{
private:
int feet;
float inches;
public:
distfeet (void); //constructor
1
feet = 0;
inches = 0.0;
distfeet (int f, float i); // constructor 2
{
inches = I;
};
void showfeet (void){
printf (“%d feet and %f inches =
\n”, feet, inches)
};
};
class distmeters{
private:
float meters;
public :
distmeters (void) // constructor 1
{
meters = 0;
};
distmeters (float m) // constructor 2
{
meters = m;
};
void showmeters (void){
printf(“%f meters”,meters);
};
operator distfeet(void){ //
conversion function
float ffeet, inches;
int ifeet;
ffeet = 3.28* meters;
ifeet = int (ffeet);
inches = 12 *(ffeet-ifeet);
return(distfeet(inches,ifeet));
};
};
void main(void){
distmeters dm1 = 1.0;
distfeet df1;
df1 = dm1;
dm1 .showmeters( );
df1.showfeet( );
}
Restriction in Operator
overloading:
1. following
operators can not be overloaded:
·
sizeof( ) operator.
·
Dot operator(.).
·
Scope resolution (::)operator.
·
Conditional operator(?.).
2.
Binary operator when overloaded should have two operands
and unary operator function should have only one. Syntax rules of original operator cannot be
violated. Only data types of operands
can be changed.
3.
Already existing operators of C++ can be used. New operator symbols can not be created.
Practical
Session 3
Example 1
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class p{
private:
int x,y;
public:
p(int a,int b)
{
x=a;
y=b;
};
void print(void){
printf("\n %d",x);
printf("\n %d",y);
}
void operator++(void){
x++;
y++;
}
};
void main(void) {
p abc(20,30);
clrscr( );
abc.print( );
++abc;
getch( );
}
Example 2
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class temp
{
private:
int x;
public:
void operator++(int);
};
void temp::operator++(int x)
{
x++;
printf("\n\n\t\t%d",x);
}
void main(void){
temp t1;
clrscr( );
t1.operator ++(1);
getch( );
}
Example 3
#include<stdio.h>
#include<string.h>
#include<conio.h>
class string{
private:
char s[100];
public:
string(void)
{
s[0]=0;
}
string(char str[]){
strcpy(s,str);
}
void putstr(void){
printf("%s",s);
}
string operator+(string s2){
string temp;
strcpy(temp.s,s);
strcat(temp.s2,s);
return(temp);
}
};
void main(void){
clrscr( );
string s1="Welcome";
string s2="To Sandhyashree
Sports Group";
string s3;
printf("\n s1 = ");
s1.putstr( );
printf("\n s2 = ");
s2.putstr( );
s3=s1+s2;
printf("\n s3 = ");
s3.putstr( );
getch( );
}
Example 4
#include<iostream.h>
#include<conio.h>
class myclass{
static int count;
int number;
public:
number=num;
++count;
}
void
show( ){
cout<<"\n
Number of calls made to set by any object:
"<<count;
}
};
int
myclass::count=0;
void main( ){
clrscr( );
myclass obj1;
obj1.show( );
obj1.set(100);
obj1.show( );
myclass obj2,obj3;
obj2.set(200);
obj2.show( );
obj3.set(500);
obj3.show( );
getch( );
}
Example 5
class counter{
private:
int
count;
public:
counter(void)
{
count = 0;
};
counter (int c)
{
count =c;
};
counter
operator++(void)
};
counter counter
:: operator++(void){
++count;
return
counter(count);
};
void main(void)
{
counter c1,c2;
c2=c1++
}
Example 6 (Conversion Function)
#include<stdio.h>
class distance{
private:
int
feet;
float inches;
public:
distance(void){
feet = 0;
inches = 0.0;
};
distance (float
meters){
float f;
f = 3.28 *
meters;
feet = 12 *(f
–feet);
};
void show
dist(void)
{
printf(“%d feet
2 %f Inches \n”,feet, inches);
};
};
void
main (void){
distance d1 =
1.25;
distance d2;
float m;
d2 = 2.0;
printf (“1.25
meters is”);
d1.showdist( );
printf(“2.0
meters is”);
d2.showdist( );
}
Example 7(Conversion function in destination class)
# include<stdio.h>
class distmetrs{
private:
float meters;
public:
distmeters(void){
meters = 0;
};
distmeters(float m){
meters = m;
};
void showmeters(void){
printf(“%f meters”,meters);
};
float getmeters(void){
return(meters);
};
};
class distfeet{
private:
int feet;
float inches;
public:
disfeet(void){
feet = 0;
inches = 0.0;
};
distfeet (int f, float i){
feet =f ;
inches = I;
};
distfeet(distmeters dm){
float ffeet;
ffeet = 3.28 * dm.getmeters( );
feet = int(ffeet);
inches = 12 *(ffeet – feet);
};
void showfeet(void){
printf(“%d feet & %d inches
\n”,feet, inches);
};
};
void main(void){
distmeters dm1 = 1.0;
distfeet df1;
df1 = dm1;
dm1.showmeters( );
df1.showfeet( );
}
Example 8
#include<iostream.h>
#include<conio.h>
class index{
private:
int value;
public:
index( ){
value=0;
}
index(int val){
value=val;
}
int getindex( ){
return value;
}
index operator -( ){
return index(-value);
}
index operator++( ){
++value;
return index(value);
}
index operator--( ) {
--value;
return index(value);
}
};
void main( ){
clrscr( );
index in1,in2;
cout<<"\n Index 1 =
"<<in1.getindex( );
cout<<"\n Index 2 =
"<<in2.getindex( );
in1= --in2;
++in2;
++in2;
cout<<"\n\n Index 1 =
"<<in1.getindex( );
cout<<"\n\n Index 2 =
"<<in2.getindex( );
getch( );
}
Example 9
#include<iostream.h>
#include<conio.h>
class index{
private:
int value;
public:
index( ){
value=0;
}
getindex( ){
return value;
}
void operator++( ){
value=value+1;
}
};
void main( ){
clrscr( );
index in1,in2;
cout<<"\n Index 1
"<<in1.getindex( );
cout<<"\n Index 2
"<<in2.getindex( );
++in1;
in2++;
in2++;
cout<<"\n Index 1
"<<in1.getindex( );
cout<<"\n Index 2
"<<in2.getindex( );
getch( );
}
Top
No comments:
Post a Comment