Professional Documents
Culture Documents
Overloading operators can be classified into Only member functions can access the
–(1) overloading member functions private data of one class
〈datatype〉 〈class_name〉::operator〈operator〉 You may want to allow a nonmember function
(〈parameter_list〉) { //functional body; } to have access to a private data
–typically, for binary operators a friend function is a nonmember function
Ex: assignment/subscript([])/function(()) that can access the non-public members of
–(2) overloading friend functions a class
〈datatype〉 operator〈operator〉 –should be used only when absolute
(〈parameter_list〉) { //functional body; } necessary
–typically, for unary operators –avoid them simply to overcome
Ex: input insertion(<<) & output extraction(>>) encapsulation
Hung-Pin(Charles) Wen UEE1303(1070) L05 9 Hung-Pin(Charles) Wen UEE1303(1070) L05 10
Enable input and output of our objects Enable input and output of our objects
–similar to other operator overloads –similar to other operator overloads
–new subtleties –new subtleties
Format of overloading operator >> and << Format of overloading operator >> and <<
istream& operator>>(istream&,〈CNAME〉&); istream&
CComplex operator>>(istream&,〈CNAME〉&);
c1(2,5), c2(-3,-2), c3;
ostream& operator<<(ostream&,〈CNAME〉&); ostream&
c3=c1+c2;operator<<(ostream&,〈CNAME〉&);
cout<<c3;//what if cout<<c3<<c2;
class CComplex { class CComplex {
friend ostream& operator<<( friend ostream& operator<<(
ostream& out, CComplex& c) { ostream& out, CComplex& c) {
out << “(” << c.real << “,” out << “(” << c.real << “,”
<< c.imag << “i)” << endl; << c.imag << “i)” << endl;
return out; } return out; }
}; };
Hung-Pin(Charles) Wen UEE1303(1070) L05 35 Hung-Pin(Charles) Wen UEE1303(1070) L05 36
Example of Overloading >> Overload Array Operator []
C++ provides explicit type conversion What if converting a CComplex into a double?
– 〈datatype〉(〈data〉), ex: int(82.7) –need a type-conversion function
– (〈datatype〉)〈data〉, ex: (double)49
–format: 〈cname〉::operator〈datatype〉 ()
Conversion constructor casts the data of one { //functional body; }
Example
type into an object of another class, ex:
class CComplex {
–can have only one parameter why? operator double() { return real; }
class CComplex { };
CComplex(double r) {
real = r; imag = 0; }
–cannot assign the returned datatype
}; –cannot have any parameter
CComplex o1(4.2); CComplex o1(4.2); double d2 = 12;
CComplex o2 = o1 + CComplex(2.5); double d3 = d2 + o1; //double operator +
Hung-Pin(Charles) Wen UEE1303(1070) L05 41 Hung-Pin(Charles) Wen UEE1303(1070) L05 42
C++ built-in operators can be overloaded friend functions add efficiency only
–to work with objects of your class –not required if sufficient accessors and
mutators are available
Operators are really just functions
Reference "names" a variable with an alias
friend functions have direct private
References and more overloading
member access
–operators: = , [], ++, --
Operators can be overloaded as Can overload << and >>
member functions where
–return type is a reference to stream type
–the first operand is the calling object