c++ 重载操作符

/******************************赋值(=)、下标(【】)、调用(())、成员访问->必须使用成员函数******/
/****************************输入输出流必须使用全局函数法(友元函数)实现重载**********************/
/*****************************复合赋值符、自增、自减、解引用、算数操作付、关系操作符、相等操作符、位操作符通常使用普通非成员函数*****/
/*****************************(::)(.)、(.*)、(sizeof)(?,))不能重载*****************/
//逻辑与&&和逻辑或||可以实现实现操作符重载,但是无法短路规则,所以一般不用这两者的操作符重载
#include<iostream>
#include<vector>
#include<string>
#include<utility>
#include<algorithm>
using namespace std;
class Sales_item
{


friend  istream& operator>>(istream&, Sales_item &);
friend  ostream& operator<<(ostream&, Sales_item&);
friend  bool operator==(Sales_item& s1, Sales_item& s2);
public:
double price;
double revnue;
int unit_sold;
Sales_item& operator+=(const Sales_item&);
Sales_item(double i, int j):price(i), unit_sold(j){}
vector<int>ivec;




};
Sales_item operator+(const Sales_item&, const Sales_item&);
bool operator==(Sales_item& s1, Sales_item& s2)
{
return ((s1.price == s2.price) && (s1.unit_sold == s2.unit_sold));


}
//14.7 和14.
class checkoutrecord
{
friend ostream& operator<<(ostream&out, const checkoutrecord&s);
friend istream& operator>>(istream& in, checkoutrecord &s);
private:
double book_id;
int num;
string title;
pair<string, string>borrwer;
vector<pair<string, string>*>wait_list;


};     //不明白这样定义的意义,如果不定义重载操作符照样也可以实现输出输入字符啊!!!
//输入输出流的重载如果用成员函数实现,需要在类ostream 和istream中添加重载函数operator<<和oper没有ator>>, 而实际上我们没有类ostream 和istream的原源代码,所以重载输入输出流只能用友元函数来实现
//函数返回值当左值时要返回引用
ostream& operator<<(ostream&out,  const checkoutrecord&s)
{
out << s.num <<endl;

for (vector<pair<string, string>*> ::const_iterator iter = s.wait_list.begin(); iter != s.wait_list.end(); iter++)
out << (*iter)->first << (*iter)->second << endl;
return out;
}
istream& operator>>(istream& in, checkoutrecord &s)
{
in >> s.book_id >> s.num >> s.title >> s.borrwer.first >> s.borrwer.second;
if (!in)
{
s = checkoutrecord();
return in;
}
s.wait_list.clear();//清空容器中元素
while (in)
{
pair<string, string>* ppa = new pair<string, string>;//因为vector 类型是pair类型的指针,所以要创建pair对象
in >> ppa->first >> ppa->second;
if (!in)return in;
s.wait_list.push_back(ppa);
}


return in;




}
class Complex
{
int a;
int b;
public:
int c;
int d;
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}


int geta()
{
return a;
}
int getb()
{
return b;
}
friend Complex operator- (const Complex& c1, const Complex& c2);
friend Complex& operator++ (Complex& c1);//前置++
friend Complex operator++ (Complex& c1,int);//后置++,参数多一个占位符  一定要注标点符号一定在英文状态下
Complex operator+ (const Complex& c2);
Complex operator--();
Complex operator--(int);
int operator()(int a,int b,int c);
friend ostream& operator<<(ostream& out, const Complex& c);
};
Complex Complex::operator+ (const Complex& c2)
{
Complex ret(0, 0);
ret.a = a + c2.a;
ret.b = b + c2.b;
return ret;




}
int Complex::operator()(int a, int b, int c)
{
return ((a > 0 )? b: c);
}
Complex Complex::operator--()
{
this->a--;
this->b--;
return *this;
}
Complex Complex::operator--(int)
{
Complex temp = *this;
this->a = a--;
this->b = b--;
return temp;
}
Complex& operator++ (Complex& c1)
{
c1.a++;


c1.b++;
return c1;


}
Complex operator++ (Complex& c1, int)
{
Complex temp = c1;
c1.a++;
c1.b++;
return temp;
}
Complex operator- (const Complex& c1, const Complex& c2)
{
Complex ret(0,0);
ret.a = c1.a - c2.a;
ret.b = c1.b - c2.b;
return ret;




}
ostream& operator<<(ostream& out, const Complex& c)//全局函数实现重载左移运算符
{
out << c.a << "+" << c.b << "i";
return out;
}
//14.33将函数对象应用于标准库算法     有点不懂????为什么count_if每次调用它的形参时,会自动的调用类的函数调用符
class GT_cls
{
public:
  GT_cls(size_t val) :bound(val){}
  bool operator()(string& s)
  {
  return s.size() > bound;
  }


private:
string::size_type bound;




};
int main01()
{
int i = 3, j = 4, z = 7;


    /*Complex c1(5,6);
Complex c2(3,4);
int x = c1(i,j,z);
cout << x << endl;*/
vector<string>ivec;
string str;
while (cin >> str)
ivec.push_back(str);
GT_cls words(6);
for (int i = 0; i < 6; i++)


cout << count_if(ivec.begin(), ivec.end(), GT_cls(i))
<< "words" << i << "longer" << endl;




/*Complex c3 = c1+c2;
Complex c4 = c1 - c2;
cout << c3 << endl;
cout << c4 << endl;
cout << c1 << endl;
cout << c2 << endl;
Complex c5(10,12);
++c5;
cout << c5 << endl;
--c5;
cout << c5 << endl;
c2--;
cout << c2 << endl;
c2++;
cout << c2 << endl;
cout << "press any data";*/
/*checkoutrecord rd1;
while (cin >> rd1)
{
if (!cin)break;
}
cout << rd1;*/
/*Sales_item item1(10,20); 
Sales_item item2(20,30);
item1.ivec.push_back(3);
cout << item1.ivec[0] << endl;
cout << item1.price << item1.unit_sold << "\n"<<endl;
cout << item2.price << item2.unit_sold << "\n" << endl;
     if(item1 == item2)
 cout <<1 << endl;
else
cout << 0 << endl;*/
system("pause");
cin.get();
return 0;

#include<algorithm>
#include<iostream>
using namespace std;
int arr[] = { 1, 2, 3, 4 };
int main()
{
/*auto myfunc = [](int n)->void{ cout << n << endl; };
std::for_each(arr, arr + 4, myfunc);*/
int a=4,b=4,c=4;


a += (a++);
printf("%d\n", a);
b += (++b);
printf("%d\n", b);
/*return 0;*/
(c++) += c;
(++c) += (c++);
system("pause");








}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值