操作符重载:类成员函数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;
}
作为类成员函数:
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;
}
相关文章推荐
- 操作符重载为成员函数还是友元函数?
- Day41、静态成员变量、静态成员函数、单例模式、成员/成员函数指针、操作符重载、友元函数
- 操作符重载为成员函数、非成员函数与友元函数的区别
- C++运算符重载三种形式(成员函数,友元函数,普通函数)详解
- 友元简介:友元函数,友元类和友元成员函数
- Effective c++之Item 23: 用非成员非友元函数取代成员函数
- 成员函数、友元函数和一般函数的区别-时间
- 一般函数-求点类中距离-区别成员函数、友元函数和一般函数
- 第六周 项目4 成员函数、友元函数和一般函数有区别(1)
- 从零开始学C++之运算符重载(一):以成员函数方式重载、以友元函数方式重载
- 友元函数和成员函数
- 第七周 项目1-成员函数、友元函数和一般函数有区别(1)外部
- 运算符-重载为成员函数 和友元函数的区别.
- 成员函数重载运算符和友元函数重载运算符的比较
- 成员函数、友元函数和一般函数的区别(两点间距离)
- C++ VS C#(12):函数的重载,类成员的static修饰,属性
- 类成员函数作为其他类的友元函数的实现(c++语言)
- 第七周 项目三 体会成员函数 友元函数 一般函数 分别求两点间的距离
- 第7周项目1-成员函数、友元函数和一般函数有区别(2)
- 重运算符用成员函数还是友元函数