您的位置:首页 > 其它

操作符重载:类成员函数VS友元函数

2012-06-27 12:21 197 查看
规则:

作为类成员函数:

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.ignore();

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 <iostream>

#include <cstdlib>

#include <iomanip>

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();

private:

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)

{

ptr = new int[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;

for(int i=0; i<size; i++)

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

{

assert(0<=subscript && subscript < size);

return ptr[subscript];

}

int Array::getArrayCount() {return arrayCount; }

istream &operator>>(istream &input, Array &a)

{

for(int i=0; i<a.size; i++) input >> a.ptr[i];

return input;

}

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;

}

return output;

}

int main()

{

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()

<< "\nArray after initialization:\n"

<< integer3 << endl ;

cout << "Assigning integer2 to integer1:\n" ;

integer1 = integer2;

cout << "integer1:\n" << integer1 << endl

<< "integer2:\n" << integer2 << 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  操作符重载