23 运算符重载(二)
来源:互联网 发布:淘宝查小号信誉 编辑:程序博客网 时间:2024/06/05 07:05
++运算符重载
//Integer.h#ifndef _INTEGER_H_#define _INTEGER_H_class Integer{public: Integer(int n); ~Integer(); Integer& operator++(); //friend Integer& operator++(Integer& i); Integer operator++(int n); //friend Integer operator++(Integer& i, int n); void Display() const;private: int n_;};#endif // _INTEGER_H_//Integer.cpp#include "Integer.h"#include <iostream>using namespace std;Integer::Integer(int n) : n_(n){}Integer::~Integer(){}Integer& Integer::operator ++(){ //cout<<"Integer& Integer::operator ++()"<<endl; ++n_; return *this;}//Integer& operator++(Integer& i)//{// //cout<<"Integer& operator++(Integer& i)"<<endl;// ++i.n_;// return i;//}Integer Integer::operator++(int n){ //cout<<"Integer& Integer::operator ++()"<<endl; //n_++; Integer tmp(n_); n_++;//n_本身++ return tmp;//返回一个临时对象,其没有++}//Integer operator++(Integer& i, int n)//{// Integer tmp(i.n_);// i.n_++;// return tmp;//}void Integer::Display() const{ cout<<n_<<endl;}//main.cpp#include "Integer.h"#include <iostream>using namespace std;int main(void){ Integer n(100); n.Display(); Integer n2 = ++n; n.Display(); n2.Display(); Integer n3 = n++; n.Display(); n3.Display(); return 0;}
//String.h#ifndef _STRING_H_#define _STRING_H_class String{public: explicit String(const char* str=""); String(const String& other); String& operator=(const String& other); String& operator=(const char* str); bool operator!() const; ~String(void); void Display() const;private: char* AllocAndCpy(const char* str); char* str_;};#endif // _STRING_H_//String.cpp#pragma warning(disable:4996)#include "String.h"#include <string.h>#include <iostream>using namespace std;String::String(const char* str){ str_ = AllocAndCpy(str);}String::String(const String& other){ str_ = AllocAndCpy(other.str_);}String& String::operator=(const String& other){ if (this == &other) return *this; delete[] str_; str_ = AllocAndCpy(other.str_); return *this;}String& String::operator=(const char* str){ delete[] str_; str_ = AllocAndCpy(str); return *this;}bool String::operator!() const{ return strlen(str_) != 0;}String::~String(){ delete[] str_;}char* String::AllocAndCpy(const char* str){ int len = strlen(str) + 1; char* newstr = new char[len]; memset(newstr, 0, len); strcpy(newstr, str); return newstr;}void String::Display() const{ cout<<str_<<endl;}//main.cpp#include "String.h"#include <iostream>using namespace std;int main(void){ String s1("abc"); String s2(s1); String s3; s3 = s1;//调用=运算符重载 String& operator=(const String& other); s3.Display(); s3 = "xxxx";//调用=运算符重载 String& operator=(const char* str); s3.Display(); return 0;}
0 0
- 23 运算符重载(二)
- 23 运算符重载(二)
- 运算符重载(二)
- 运算符重载(二)
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- partial_sort 二 重载cmp()运算符
- 运算符重载练习(二)
- 黑马程序员-------运算符重载(二)
- C++中的运算符重载(二)
- 多态性之运算符重载(二)
- C++ 运算符重载(二)
- C++运算符重载(二)
- 重载运算符之二
- 运算符重载问题(二)
- 第9周-运算符重载(二)-复数类中的运算符重载(续)
- Time类中的运算符重载(1)-二目的比较运算符重载
- Hadoop 2.6.0动态添加节点
- TOJ : 4014. Warm up 2
- 关于C# MSChart中如何添加右侧图例说明legend和生成百分比视图
- 命名空间 namespace
- 机器学习实战(5)--SVM(Support vector machine)(五)
- 23 运算符重载(二)
- Linux CPU频率控制
- OVS命令详解
- 8.ARM工作模式和寄存器及异常
- Hive数据导入方案—使用ORC格式存储hive数据
- lintcode: Longest Common Prefix
- eclipse和myeclipse编辑器中代码 “ import="java.sql.* " 报错的解决方案
- thread 退出状态如何获取
- windows(win7,win8,xp)hosts文件找不到原因分析及解决方法