Week 10
Week 10
For example:
void operator + ()
class Counter
// overloading unary ++ operator
{ private:
// (prefix ++) class member function
unsigned int count;
public:
Counter(): count(0)
{
}
unsigned int get_count()
{return count; }
void operator ++ ()
{++count;}
};
int main()
{Counter c1, c2;
cout << c1 = << c1.get_count();
cout << \nc2 = << c2.get_count();
++c1; ++c2; ++c2;
Output:
cout << \nc1 = << c1.get_count();
c1 = 0
cout << \nc2 = << c2.get_count();
c2 = 0
}
c1 = 1
c2 = 2
class Counter
// overloading unary ++ operator
{ private:
// (prefix ++) global function
unsigned int count;
public:
Counter(): count(0)
{
}
unsigned int get_count()
{return count; }
friend void operator ++ (Counter &);
};
Output:
c1 = 0
c2 = 0
c1 = 1
c2 = 2
// error
class Counter
// overloading unary ++ operator
{ private:
// (an object of the class is returned)
unsigned int count;
public:
Counter(): count(0)
{
}
Counter(int c): count(c)
{
}
unsigned int get_count()
{return count; }
Counter operator ++ ()
{++count;
return Counter (count);
// a nameless object of
}
// Counter is returned
};
int main()
{Counter c1, c2;
cout << c1 =
cout << \nc2 =
++c1;
c2 = ++c1;
cout << \nc1 =
cout << \nc2 =
}
<< c1.get_count();
<< c2.get_count();
<< c1.get_count();
<< c2.get_count();
Output:
c1 = 0
c2 = 0
c1 = 2
c2 = 2
class Distance
// overloaded function +
{ private:
int feet; float inches;
public:
Distance(): feet(0), inches(0.0)
{ }
Distance(int ft, float in): feet(ft), inches(in)
{ }
void getdist()
{cout << "Enter feet " ; cin >> feet;
cout << "Enter inches "; cin >> inches; }
void showdist()
{cout << feet <<"\' - " << inches << '\"' << endl;}
Distance operator + (Distance) const;
};
Distance Distance::operator + (Distance dist2)const
{ int f = feet + dist2.feet;
float i = inches + dist2.inches;
if(i >= 12.0) {i -= 12.0; f++; }
return Distance(f, i); // returns a nameless object
}
int main()
{ Distance d1(10, 5.5);
Distance d2(8, 9.7);
Distance d3 = d1 + d2;
Distance d4 = d1 + d2 + d3;
cout << "d1 = " ; d1.showdist();
cout << "d2 = " ; d2.showdist();
cout << "d3 = " ; d3.showdist();
cout << "d4 = " ; d4.showdist();
}
Output:
d1 = 10' - 5.5"
d2 = 8' - 9.7"
d3 = 19' - 3.2"
d4 = 38' - 6.4
class PhoneNumber
// overloaded operators << and >>
{friend ostream & operator <<(ostream &, const PhoneNumber &);
friend istream & operator >>(istream &, PhoneNumber &);
private:
string areaCode, exchange, line;
};
ostream & operator <<(ostream & output, const PhoneNumber
&number)
{output << ( << number.areaCode << ) << number.exchange <<
- << number.line;
return output; }
ostream & operator >>(istream &input, PhoneNumber &number)
{input.ignore;
// skip (
input >> setw(3) >> number.areaCode;
input.ignore(2)
// skip ) and space
input >> setw(3) >> number.exchange;
input.ignore()
// skip (-)
input >> setw(4) >> number.line;
return input; }
int main()
{PhpneNumber phone;
cout << Enter phone number in the form (123) 456-7890: <<
endl;
cout << The phone number entered was:
cout << phone << endl;
}
Run:
Enter phone number in the form (123) 456-7890:
(800) 555-1212
Enter phone number entered was:
(800) 555-1212
Data Conversion:
It is common to represent data in different forms. It involves
the conversion of data from one form to another; for
example, conversion from radians to degrees, etc.
class Meter
{private: float length;
Meter() length(0.0)
{
}
Meter(float initlength)
{ length = initlength/100;
};
class Meter
// conversion from basic-type to objects
{private: float length;
// and vice-versa
public:
Meter() length(0.0)
{
}
Meter(float initlength)
// basic-type to object
{length = initlength/100;}
operator float()
// object to basic-type
{ float lengthcms;
lengthcms = length*100.0;
return lenghtcms;}
void getLength()
{cout << Enter length in meters << endl;
cin >> length;}
void showLength()
{cout << Length in meters = << length << endl;}
};
int main()
{Meter meter1, meter2;
float length1, length2;
cout << Enter length in cms << endl;
cin >> length1;
meter1 = length1;
//basic type to object
meter1.showLength();
meter2.getLength();
length2 = meter2;
//object to basic type
cout << Length in cms = << length2 << endl;
}
Run
Enter length in cms
220
Length in meters = 2.2
Enter length in meters
1.30
Length in cms = 130
class String
// conversion between C-type string
{private: char str[50];
// and string objects
public:
String()
{str[0] = \0; }
String(char s[])
// C-string to string
{ strcpy(str, s); }
void display()
{cout << str << endl;}
operator char*()
{ return str;}
};
// String to C-string
int main()
{String s1; char str1[] = Raj Kumar;
s1 = str1;
s1.display();
String s2 = Ravi S.;
char *str2;
str2 = s2;
cout << s2 << endl;
}
destination object.
The conversion function may be defined in the source
objects class as an operator function or as a one-argument
constructor in the destination objects class.
Degree(Radian radian)
//conversion at destination object
{degree = radian.getradian()*180/PI;}
float getdegree()
{return degree;}
operator Radian()
//conversion at source object
{ return (Radian(degree*PI/180.0));}
void input() {cout << "Enter degree: "; cin >> degree;}
void display() {cout << "Degree = " << degree << endl;}
};
int main()
{Degree deg1, deg2; Radian rad1, rad2;
deg1.input();
rad1 = deg1;
// uses operator Radian()
rad1.display();
rad2.input();
deg2 = rad2;
// uses Degree(Radian rad)
deg2.display();}
Run
Enter degree: 120
Radian = 2.09433
Enter radian: 1.20
Degree = 68.757
class Safearray
// overloading []
{private: int a[50];
public:
int & operator[](int n)
{if(n < 0 || n >= 50)
cout <<Index out of bounds; exit(1);}
return a[n];}
};
int main()
{Safearray sa;
for(int i=0; i<50; i++)
sa[i] = i*10;
for(int j=0; j<50; j++)
{int temp = sa[j];
cout << Element << j << is << temp << endl;
}
}