Operator Overloading Code

Download Report

Transcript Operator Overloading Code

Operator Overloading
C++ Programming Certificate
University of Washington
Cliff Green
© Bruce M. Reynolds &
Cliff Green, 2002
1
Operator Overloading
// overloaded binary operator (member function)
class integer {
long i;
// class defaults to private access
public:
integer(int val) : i(val) { }
integer const operator+( integer const & rhs ) const {
return integer( i + rhs.i );
}
integer& operator+=( integer const & rhs ) {
i += rhs.i;
return *this;
}
int print() const { return i; }
};
#include <iostream>
using std::cout;
using std::endl;
int main() {
integer I(1), J(2), K(3);
K += I + J;
cout << K.print() << endl;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
2
Operator Overloading
// overloaded binary operator, non-member friend function
class integer {
private:
friend integer const operator+( integer const & left,
integer const & right );
long i;
public:
integer(int val) : i(val) { }
integer& operator+=( integer const & rv ) {
i += rv.i;
return *this;
}
int print() const { return i; }
};
integer const operator+(integer const & left,
integer const & right ) {
return integer( left.i + right.i );
}
#include <iostream>
using std::cout; using std::endl;
int main() {
integer I(1), J(2), K(3);
K += I + J;
cout << K.print() << endl;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
3
Operator Overloading
// copy constructor
#include <iostream>
using std::cout; using std::endl;
class Date {
public:
Date( char const * aStrDate ) : strDate( aStrDate ) { }
Date( Date const & date );
void print() const { cout << strDate << endl; }
private:
char const * strDate;
};
Date::Date(Date const & date ) : strDate(date.strDate) { }
int main() {
Date d1( "1/1/1970" );
Date d2 = d1;
Date d3( d1 );
d2.print();
d3.print();
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
// copy constructor
// copy constructor
4
Operator Overloading
// assignment operator
#include <iostream>
using std::cout; using std::endl;
class Date {
public:
Date( char const * aStrDate ) : strDate( aStrDate ) { }
Date() : strDate(0) { }
Date & operator=(Date const & date );
void print() const { cout << strDate << endl; }
private:
char const * strDate;
};
Date& Date::operator=(Date const & date ) {
strDate = date.strDate; // correct logic? Why?
return *this;
}
int main() {
Date d1( "1/1/1970" );
Date d2;
d2 = d1;
d2.print();
}
© Bruce M. Reynolds &
Cliff Green, 2002
// assignment operator
5
Operator Overloading
// smart pointer style overloading (this is not a true smart pointer)
#include <iostream>
using std::cout; using std::endl;
class Obj {
public:
Obj( int aValue = 10 ) : value( aValue ) {}
void f() const { cout << "f called, value is " << value << endl; }
private:
int value;
};
class ObjPtr {
public:
ObjPtr( Obj* aPtr = 0 ) : objPtr(aPtr) { }
Obj* operator->() const;
private:
Obj *objPtr;
};
Obj* ObjPtr::operator->() const {
static Obj dummy(20); // for example use only
return (objPtr == 0 ? &dummy : objPtr);
}
© Bruce M. Reynolds &
Cliff Green, 2002
6
Operator Overloading
// smart pointers
int main() {
ObjPtr op;
op->f();
Obj obj(30);
op = &obj;
op->f();
// f called, value is 20
// f called, value is 30
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
7
Operator Overloading
// HexValue operators - header file HexValue.h
// don’t forget typical header guard
#include <iosfwd>
#include <string>
using std::ostream; using std::istream; using std::string;
class HexValue {
public:
HexValue ();
HexValue ( int );
HexValue ( string const &);
~HexValue ();
HexValue const & operator=( int );
HexValue const & operator=( string const & );
HexValue const & operator=( HexValue const );
operator int() const;
operator string() const;
bool operator==( int ) const;
bool operator!=( int ) const;
bool operator==( string const & ) const;
bool operator!=( string const & ) const;
bool operator==( HexValue const & ) const;
bool operator!=( HexValue const & ) const;
ostream& streamOut (ostream& ) const;
istream& streamIn (istream& );
private:
static int Value( string const &strValue );
int m_nValue;
friend void Test();
};
© Bruce M. Reynolds &
Cliff Green, 2002
8
Operator Overloading
// HexValue operators – header file (continued)
ostream & operator<<( ostream &os, HexValue const & hvValue );
istream & operator>>( istream & is, HexValue & hvValue );
void Test();
© Bruce M. Reynolds &
Cliff Green, 2002
9
Operator Overloading
// HexValue operators - implementation file HexValue.cpp
#include <sstream>
#include <iostream>
#include <iomanip>
using std::istringstream; using std::ostringstream;
using std::cout; using std::endl; using std::hex;
// private helper function for converting from a string to a value
int HexValue::Value( string const & strValue ) {
if ( strValue == "" ) {
return 0;
}
istringstream is( strValue );
// not very robust, but works for example
int nValue;
is >> hex >> nValue;
return nValue;
}
© Bruce M. Reynolds &
Cliff Green, 2002
10
Operator Overloading
// HexValue operators – implementation file (continued)
// Default Ctor
HexValue :: HexValue () : m_nValue(0) { }
// Ctor taking int
HexValue :: HexValue ( const int nValue ) :
m_nValue (nValue) { }
// Ctor taking string
HexValue :: HexValue ( const string &strValue ) :
m_nValue (Value( strValue ) ) { }
// Dtor
HexValue ::~ HexValue () { }
// Assignment operator taking int
HexValue const & HexValue ::operator=( int nValue ) {
m_nValue = nValue;
return *this;
}
// Assignment operator taking string
HexValue const & HexValue ::operator=( string const & strValue ) {
m_nValue = Value( strValue );
return *this;
}
© Bruce M. Reynolds &
Cliff Green, 2002
11
Operator Overloading
// HexValue operators – implementation file (continued)
// Conversion operator, to int
HexValue ::operator int() const {
return m_nValue;
}
// Conversion operator, to string
HexValue ::operator string() const {
ostringstream tmp;
tmp << "0x" << hex << m_nValue;
return tmp.str();
}
// Relational operator, taking int
bool HexValue ::operator==( int nValue ) const {
return m_nValue == nValue;
}
// Relational operator, taking string
bool HexValue ::operator==( string const &strValue ) const {
return m_nValue == Value( strValue );
}
// Relational operator, taking another HexValue
bool HexValue ::operator==( HexValue const & hvValue ) const {
return m_nValue == hvValue.m_nValue;
}
© Bruce M. Reynolds &
Cliff Green, 2002
12
Operator Overloading
// HexValue operators – implementation file (continued)
// Relational operator, != taking int
bool HexValue ::operator!=(int nValue ) const {
return !(m_nValue == nValue);
}
// Relational operator, != taking string
bool HexValue ::operator!=(string const & strValue ) const {
return ! ( *this == strValue );
}
// Relational operator, != taking another HexValue
bool HexValue ::operator!=( const HexValue &hvValue ) const {
return !(m_nValue == hvValue.m_nValue);
}
// stream insertion
ostream & HexValue::streamOut( ostream & os) const {
return os << "0x" << hex << m_nValue << endl;
}
ostream & operator<<( ostream &os, HexValue const & hvValue ) {
return hvValue.streamOut(os);
}
// stream extraction
istream & HexValue::streamIn( istream & is) {
string in;
is >> in;
m_nValue = Value( in );
return is;
}
istream &operator>>( istream &is, HexValue &hvValue ) {
return hvValue.streamIn(is);
}
© Bruce M. Reynolds &
Cliff Green, 2002
13
Operator Overloading
// HexValue operators
// routine to test the functionality of HexValue
#include <assert.h>
void Test() {
// constructor: ()
HexValue hvTest0;
assert( hvTest0.m_nValue == 0 );
// constructor: int
HexValue hvTest1(20);
assert( hvTest1.m_nValue == 20 );
// constructor: int 0
HexValue hvTest2( 0 );
assert( hvTest2.m_nValue == 0 );
// constructor: string
HexValue hvTest3c( string(“0x89”) );
assert( hvTest3c.m_nValue == 0x89 );
© Bruce M. Reynolds &
Cliff Green, 2002
14
Operator Overloading
// HexValue operators
// constructor: char const *
HexValue hvTest3a("56");
assert( hvTest3a.m_nValue == 0x56 );
HexValue hvTest3("0x56");
assert( hvTest3.m_nValue == 0x56 );
HexValue hvTest3b("");
assert( hvTest3b.m_nValue == 0 );
// assignment: int
HexValue hvTest4;
hvTest4 = 0x37;
assert( hvTest4.m_nValue == 0x37 );
// assigment: string
HexValue hvTest5;
hvTest5 = "0x43";
assert( hvTest5.m_nValue == 0x43 );
HexValue hvTest5b;
hvTest5b = "";
assert( hvTest5b.m_nValue == 0 );
© Bruce M. Reynolds &
Cliff Green, 2002
15
Operator Overloading
// HexValue operators
// conversion: int
HexValue hvTest6( 0x23 );
int nValue (hvTest6);
assert( nValue == 0x23 );
// conversion: string
HexValue hvTest7( 0x23 );
string strValue (hvTest7);
assert( strValue == "0x23" );
// typical usage: string to int
string strValue2 ("0x89“);
int nValue2 = HexValue ( strValue2 );
assert( nValue2 == 0x89 );
// typical usage: int to string
int nValue3 (0x58);
string strValue3 (HexValue ( nValue3 ));
assert( strValue3 == "0x58" );
© Bruce M. Reynolds &
Cliff Green, 2002
16
Operator Overloading
// HexValue operators
// equality operator: int
assert( hvTest7 == 0x23 );
assert( hvTest7 != 0x24 );
// equality operator: char *
assert( hvTest7 == "0x23" );
assert( hvTest7 != "0x24" );
// equality operator: string
assert( hvTest7 == string("0x23") );
assert( hvTest7 != string("0x24") );
// equality operator: HexValue
assert( hvTest7 == hvTest6 );
assert( hvTest7 != hvTest5 );
}
// end of Test()
© Bruce M. Reynolds &
Cliff Green, 2002
17
Operator Overloading
// HexValue operators – main function
#include <iostream>
#include <iomanip>
using std::cout; using std::cin; using std::endl;
using std::boolalpha;
#include "HexValue.h"
int main() {
Test();
// test stream insertion
HexValue hv1 ( "0x123" );
cout << hv1 << endl;
// test stream extraction
HexValue hv2;
cin >> hv2;
cout << hv2 << endl;
cout << "hv1 == hv2: " << boolalpha << (hv1 == hv2) << endl;
return 0;
}
© Bruce M. Reynolds &
Cliff Green, 2002
18