摘要:
/*对自定义类型的对象,使用运算符时总是调用相应的运算符函数*/#includeusing namespace std;class A{ int data;public: A(int d=0):data(d){} operator int () { return (int)data; } operator bool(){ return data!=0; } operator char(){ return (char )data; }};int main(){ A a1(65),a2(200),... 阅读全文
posted @ 2014-03-26 22:01
Flyzhcong
阅读(236)
评论(0)
推荐(0)
摘要:
http://blog.csdn.net/zuheyawen/article/details/7340717一、双目运算符运算符重载就是自己写运算符函数,来规定运算符如何工作,有以下两种方法友元:1、在类中声明friend 2、定义:返回类型 operator +、-、*(形参1,形参2) obj1+obj2=> operator+(obj1,obj2) 返回值作为计算结果成员: 直接在类中定义 operator+(形参)//友员不是成员,友员的应用,没有this //写运算符函数 ,为了让程序员更方便,重新定义新的+ - * /#include using namespace... 阅读全文
posted @ 2014-03-26 13:58
Flyzhcong
阅读(277)
评论(0)
推荐(0)
摘要:
//随时调整长短是数组,数据不会丢//拷贝构造函数/*例子函数头的表示方法@函数名称: strcpy 函数原型: char* strcpy(char* str1,char* str2); 函数功能: 把str2指向的字符串拷贝到str1中去 函数返回: 返回str1,即指向str1的指针 参数说明: */#includeusing namespace std;class Array{ char *p; int len;public: Array(int n):len(0),p(NULL){ resize(n); } void resize(int n){ char* q=new c... 阅读全文
posted @ 2014-03-26 09:44
Flyzhcong
阅读(158)
评论(0)
推荐(0)
摘要:
#includeusing namespace std;class A{int data;public:A():data(100){cout<<"A()"<<endl;}A(const A& o)/*:data(o.data)*/{cout<<"A(const A&)"<<endl;}//A(const A& o):data(o.data){cout<<"A(const A&)"<<endl;} 不传进去data就是垃圾数据void 阅读全文
posted @ 2014-03-26 09:36
Flyzhcong
阅读(113)
评论(0)
推荐(0)

浙公网安备 33010602011771号