好吊顶网站,企业端app下载,Wordpress报表主题,wordpress模板制作教程文章目录 运算符重载加号运算符重载成员函数实现运算符重载全局函数实现运算符重载全局函数实现函数重载 左移运算符重载递增运算符重载赋值运算符重载关系运算符重载函数调用运算符重载 运算符重载 对已有的运算符重新进行定义#xff0c;赋予其另一种功能#xff0c;以适应… 文章目录 运算符重载加号运算符重载成员函数实现运算符重载全局函数实现运算符重载全局函数实现函数重载 左移运算符重载递增运算符重载赋值运算符重载关系运算符重载函数调用运算符重载 运算符重载 对已有的运算符重新进行定义赋予其另一种功能以适应不同的数据类型 对于内置的数据类型的表达式的运算符是不可能改变的 不要滥用运算符重载
加号运算符重载
可以计算自定义数据类型
成员函数实现运算符重载
成员函数实现运算符重载的本质 p2.operator(p1)
#include iostreamusing namespace std;class Person{
public:Person(){}Person(int a, int b) {this-aa;this-bb;}// 成员函数实现运算符重载Person operator(const Person p) {Person temp;temp.athis-ap.a;temp.bthis-bp.b;return temp;}
public:int a;int b;
};void test() {Person p1(10,10);Person p2(10,10);// 成员函数实现运算符重载的本质 p2.operator(p1)Person p3 p1p2;cout p3.a: p3.a endl;cout p3.b: p3.b endl;
}int main(){test();return 0;
}全局函数实现运算符重载
全局函数实现运算符重载的本质是operator(p1,p2)
#include iostreamusing namespace std;class Person{
public:Person(){}Person(int a, int b) {this-aa;this-bb;}
public:int a;int b;
};// 全局函数实现运算符重载
Person operator(Person p1, Person p2) {Person temp;temp.ap1.ap2.a;temp.bp1.bp2.b;return temp;
}
// 全局函数实现函数重载void test() {Person p1(10,10);Person p2(10,10);// 全局函数实现运算符重载的本质是operator(p1,p2)Person p3 p1p2;cout p3.a: p3.a endl;cout p3.b: p3.b endl;
}int main(){test();return 0;
}全局函数实现函数重载
通过函数重载实现不同类型的加法运算
#include iostreamusing namespace std;class Person{
public:Person(){}Person(int a, int b) {this-aa;this-bb;}
public:int a;int b;
};// 全局函数实现函数重载
Person operator(Person p1, int num) {Person temp;temp.ap1.anum;temp.bp1.bnum;return temp;
}void test() {Person p1(10,10);int num 100;// 通过函数重载实现了Person和int类型的加法运算Person p3 p1num;cout p3.a: p3.a endl;cout p3.b: p3.b endl;
}int main(){test();return 0;
}左移运算符重载 作用可以输出自定义数据类型 重载左移运算符配合友元可以实现输出自定义数据类型
#include iostreamusing namespace std;class Person{friend ostream operator(ostream cout, Person p);
public:Person(int a, int b){this-a a;this-b b;}// 成员函数重载左移运算符的本质是pcout// 在C中一般不适用成员函数重载左移运算符,因为无法实现coutp的格式// operator(cout) {}
private:int a;int b;
};// ostream 返回cout这样可以在链式使用时继续追加
// 本质operator(cout, p),简化为coutp
ostream operator(ostream cout, Person p) {cout a p.a b p.b;return cout;
}void test(){Person p(1,1);cout p endl;
}int main(){test();return 0;
}递增运算符重载
通过重载自增运算符实现自己的整形数据
#include iostreamusing namespace std;class MyInt{friend ostream operator(ostream cout, MyInt i);
public:// 重载前置运算符返回引用为了一直对一个数据进行递增操作MyInt operator(){num;return *this;}// 重载后置运算符// void operator(int) int代表占位参数可以用于区分前置和后置递增MyInt operator(int){MyInt temp *this;num;return temp;}
private:int num;
};ostream operator(ostream cout, MyInt i){cout i.num;return cout;
}void test(){MyInt myint;cout (myint) endl;MyInt myint1;cout (myint) endl;
}int main(){test();return 0;
}赋值运算符重载
#include iostreamusing namespace std;class Person{
public:Person(int age) {this-agenew int(age);}Person operator(const Person p) {// 编译器提供的是浅拷贝// agep.age;// 先判断是否有属性在堆区如果有先释放,然后在深拷贝if (age ! NULL) {delete age;ageNULL;}// 深拷贝agenew int(*p.age);return *this;}~Person(){if (age NULL) {return;}delete age;ageNULL;}int* age;
};void test(){Person p1(18);Person p2(20);Person p3(30);p3p2p1;cout *p1.age endl;cout *p2.age endl;cout *p3.age endl;
}int main(){test();return 0;
}关系运算符重载
#include iostreamusing namespace std;class Person{friend ostream operator(ostream cout, Person p);
public:Person(int age) {this-ageage;}bool operator(const Person p) {return agep.age;}bool operator!(const Person p) {return age!p.age;}bool operator(const Person p) {return agep.age;}bool operator(const Person p) {return agep.age;}bool operator(const Person p) {return agep.age;}bool operator(const Person p) {return agep.age;}
private:int age;
};ostream operator(ostream cout, Person p){cout p.age endl;return cout;
}void test(){Person p1(18);Person p2(20);cout (p1 p2) endl;cout (p1! p2) endl;cout (p1 p2) endl;cout (p1 p2) endl;cout (p1 p2) endl;cout (p1 p2) endl;
}int main(){test();return 0;
}函数调用运算符重载 由于重载后的使用方式很像函数调用因此也称为仿函数 仿函数没有固定写法非常灵活
#include iostreamusing namespace std;class Person{
public:// 打印类仿函数void operator()(string text) {cout text endl;}
};class MyAdd{
public:// 加法类仿函数int operator()(int num1, int num2) {return num1num2;}
};void test(){Person p;// 由于使用起来非常类似于函数调用因此也称为仿函数p(123);
}void test1(){MyAdd myAdd;cout myAdd(1,2) endl;// 使用匿名函数调用cout MyAdd()(100,10) endl;
}int main(){test();test1();return 0;
}
文章转载自: http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.rytps.cn.gov.cn.rytps.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.qpqb.cn.gov.cn.qpqb.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.wnhml.cn.gov.cn.wnhml.cn http://www.morning.cjsrg.cn.gov.cn.cjsrg.cn http://www.morning.dpruuode.cn.gov.cn.dpruuode.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn