規則:
作為類成員函數:
1.左操作數必須為該類的對象;
作為友元函數:
1.若左操作數不是該類的對象,操作符函數不能作該類的成員函數;
2.若想直接通路該類的private和protected成員,非成員操作符函數必須為友元函數。
case1:<<和>>的重載
代碼:
ostream &operator<<(ostream &output, const phoneNumber &num)
{
output <<"("<<num.areaCode<<") "
<<num.exchange<<"-"<<num.line;
return output;
}
istream &operator>>(istream &input, phoneNumber &num)
input.ignore();
input>>setw(4)>>num.areaCode;
input.ignore(2);
input>>setw(4)>>num.exchange;
input>>setw(5)>>num.line;
return input;
int main()
phoneNumber phone;
cout<<"Enter phone number in the form (123) 456-7890:\n";
cin>>phone;
cout<<"The phone number entered is: "<<phone<<endl;
return 0;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;
#include <iomanip>
using std::setw;
class phoneNumber
friend ostream& operator <<(ostream&, const phoneNumber&);
friend istream& operator >>(istream&, phoneNumber&);
private:
char areaCode[4];
char exchange[4];
char line[5];
};
Enter phone number in the form (123) 456-7890:
(800) 555-1212
The phone number entered is: (800) 555-1212
case2: 實作整型數組類的操作符重載:
#include <cassert>
#include <cstdlib>
using namespace std ;
class Array
friend ostream &operator<<(ostream&, const Array&);
friend istream &operator>>(istream&, Array&);
public:
Array(int=10);
Array(const Array&);
~Array();
int getSize() const;
const Array &operator=(const Array&);
bool operator==(const Array&) const;
bool operator!=(const Array &right) const
{ return !(*this==right); }
int &operator[](int);
const int &operator[](int) const;
static int getArrayCount();
int size; //array size
int *ptr; //ponter to first element
static int arrayCount; //# of arrays instantiated
int Array::arrayCount= 0; //no object yet
Array::Array(int arraySize)
size = (arraySize > 0 ? arraySize : 10);
ptr = new int[size];
assert(ptr!=0); //terminate if memory not allocated
++arrayCount;
for(int i=0; i<size; i++) ptr[i]=0;
Array::Array(const Array &init):size(init.size)
assert( ptr !=0);
++arrayCount;
for(int i=0; i<size; i++)
ptr[i]=init.ptr[i];
Array::~Array()
delete []ptr;
--arrayCount;
int Array::getSize() const {return size;}
//const return avoids:(a1=a2)=a3
const Array& Array::operator=(const Array &right)
if(&right != this) //check for self-assignment
{
if(size!=right.size)
{
delete []ptr;
size = right.size;
ptr = new int[size];
assert(ptr!=0);
}
for(int i=0; i<size; i++)
ptr[i]=right.ptr[i];
}
return *this;
bool Array::operator==(const Array &right) const
if(size!=right.size) return false;
if(ptr[i]!=right.ptr[i]) return false;
return true;
int &Array::operator[](int subscript)
assert(0<=subscript && subscript < size);
return ptr[subscript];
const int &Array::operator[](int subscript) const
int Array::getArrayCount() {return arrayCount; }
istream &operator>>(istream &input, Array &a)
for(int i=0; i<a.size; i++) input >> a.ptr[i];
ostream &operator<<(ostream &output, const Array &a)
for(int i=0; i<a.size; i++)
output << setw(12)<<a.ptr[i];
if((i+1)%4==0) output<<endl;
cout << "# of arrays instantiated = "
<< Array::getArrayCount()<<endl;
Array integer1(7), integer2;
cout << "# of arrays instantiated = "
<< Array::getArrayCount() << endl;
cout << "Size of array integer1 is "
<< integer1.getSize()
<< "\nArray after initialization:\n"
<< integer1 << endl ;
cout << "Size of array integer2 is "
<< integer2.getSize()
<< "\nArray after initiazation:\n"
<< integer2 << endl ;
cout << "Input integers: " << endl;
cin >> integer1 >> integer2 ;
cout << "After input, the arrays contain:\n"
<< "integer1:\n" << integer1 << endl
<< "integer2:\n" << integer2 << endl ;
cout << "Evaluating: integer1!=integer2\n";
if(integer1!=integer2)
cout << "They are not equal\n";
Array integer3(integer1) ;
cout << "\nSize of array integer3 is "
<< integer3.getSize()
<< integer3 << endl ;
cout << "Assigning integer2 to integer1:\n" ;
integer1 = integer2;
cout << "integer1:\n" << integer1 << endl
cout<< "Evaluating: integer1==integer2\n";
if(integer1==integer2)
cout << "They are equal" << endl;
cout << "integer1[5] is " << integer1[5] << endl;
cout << "Assgning 1000 to integer1[5]\n";
integer1[5] = 1000 ;
cout << "integer1:\n" << integer1 << endl;
return 0;