大一的时候学习C++,用的谭浩强那本红皮书,学到运算符重载的时候,心里很没底,学了也就忘了。这些天使用某个类库代码,N多运算符重载,只好硬着头皮学习了。笔记如下:
本节目的:练习使用 加减乘除运算符、自增自减、以及输入输出运算符的重载。
先看某个类的头文件代码:
#ifndef CLASS_HEADER_H
#define CLASS_HEADER_H
#include <iostream>
using namespace std;
class CExample
{
public:
CExample();
// CExample(int _value = 0);
~CExample();
void SetValue(int _value);
CExample operator + (CExample other);
CExample operator + (int _add);
CExample operator * (CExample other);
CExample operator * (const int times);
// CExample operator ++ ();
// CExample operator ++ (int);
void operator <<(int& _value);
void operator >>(int& _value);
friend CExample operator + (const int, const CExample& ex);
friend CExample operator + ( const CExample& ex, const int _add);
friend CExample operator * (const int times, const CExample& ex);
friend CExample operator * (const CExample& ex, const int times);
friend CExample& operator ++ (CExample& ex);
friend CExample& operator ++ (CExample& ex, int a);
// friend bool operator == (CExample ex1, CExample ex2);
friend ostream& operator << (ostream& out, CExample& ex);
void display();
protected:
private:
int value;
};
#endif
下面是其cpp实现文件代码:
///*********************************************************
// * ClassSource.cpp
// * [9/8/2012 Administrator]
// *
// * 说明:
//*********************************************************
#include "stdafx.h"
#include "ClassHeader.h"
CExample::CExample()
{
}
// CExample::CExample(int _value)
// {
// value = _value;
// }
CExample::~CExample()
{
}
void CExample::SetValue(int _value)
{
value = _value;
}
CExample CExample::operator +(CExample other)
{
//return CExample(value + other.value);
CExample ex;
ex.SetValue(this->value + other.value);
return ex;
}
CExample CExample::operator +(int _add)
{
//return CExample(value + _add);
CExample ex;
ex.value = this->value + _add;
return ex;
}
CExample CExample::operator *(CExample other)
{
//return CExample(value * other.value);
CExample ex;
ex.value = this->value * other.value;
return ex;
}
CExample CExample::operator * (const int times)
{
//return CExample(value + _add);
CExample ex;
ex.value = this->value * times;
return ex;
}
// CExample CExample::operator ++()
// {
// //return CExample(value+1);
//
// this->value++ ;
// return *this;
// }
// CExample CExample::operator ++(int a)
// {
// //return CExample(value+1);
//
// this->value++ ;
// return *this;
// }
// bool CExample::operator ==(CExample other)
// {
// return value == other.value;
// }
void CExample::operator <<(int& _value)
{
_value = this->value;
}
void CExample::operator >>(int& _value)
{
this->value = _value;
}
void CExample::display()
{
cout<<"value: "<<value<<endl;
}
// 在CExample类中声明过的友元函数
CExample operator + (const int _add, const CExample& ex)
{
CExample ex1;
ex1.value = ex.value + _add;
return ex1;
}
CExample operator + ( const CExample& ex,const int _add)
{
CExample ex1;
ex1.value = ex.value + _add;
return ex1;
}
CExample operator * (const int times, const CExample& ex)
{
CExample ex1;
ex1.value = ex.value * times;
return ex1;
}
CExample operator * (const CExample& ex, const int times)
{
CExample ex1;
ex1.value = ex.value * times;
return ex1;
}
CExample& operator ++ (CExample& ex)
{
ex.value++;
return ex;
}
CExample& operator ++ (CExample& ex, int a)
{
ex.value++;
return ex;
}
ostream& operator << (ostream& out, CExample& ex)
{
out<<"value: "<<ex.value<<endl;
return out;
}
接下来在main函数中测试:
//*********************************************************
// * MainTest.cpp
// * [9/8/2012 Administrator]
// *
// * 说明:
//*********************************************************
#include <iostream>
#include "ClassHeader.h"
using namespace std;
int main()
{
CExample ex1,ex2, ex3;
ex1.SetValue(1);
ex2.SetValue(2);
ex3 = ex1 + ex2;
cout<<"1.测试 CExample + CExample:"<<endl;
ex3.display();
ex3 = ex1 + 5;
cout<<"2.测试 CExample + int:"<<endl;
ex3.display();
ex3 = 4 + ex1;
cout<<"3.测试 int + CExample:"<<endl;
ex3.display();
ex3 = ex1 * ex2;
cout<<"4.测试 CExample * CExample:"<<endl;
ex3.display();
ex3 = 6 * ex1;
cout<<"5.测试 int * CExample:"<<endl;
ex3.display();
ex3 = ex1 * 6;
cout<<"6.测试 CExample * int:"<<endl;
ex3.display();
ex3.SetValue(8);
++ex3;
cout<<"7.测试 ++CExample:"<<endl;
ex3.display();
ex3.SetValue(10);
ex3++;
cout<<"8.测试 CExample++:"<<endl;
ex3.display();
ex3.SetValue(10);
cout<<"9.测试 cout<<CExample:"<<endl;
cout<<ex3;
ex3.SetValue(10);
int a = 0;
ex3<<a;
cout<<"10.测试 CExample<<:"<<endl;
cout<<"a = "<<a<<endl;
int b = 5;
ex3>>b;
cout<<"11.测试 CExample>>:"<<endl;
cout<<ex3;
return 0;
}