c++重载<, +, +=, =, ==, <<, >>等符号
2014-12-31 14:29
239 查看
来源:blog.sina.com.cn/s/blog_6b21f9ce0100lln1.html
以下示例中定义了一个class test, 重载了<, +, +=, =, ==, <<, >>等符号:
#include<iostream>
#include<vector>
using namespace std;
class test{
public:
int v;
test():v(0){}
test(const int &a):v(a){}
test(const test &t1):v(t1.v){}
//比较两个对象的大小
bool operator<(const test &t1) const{
return (v < t1.v);
}
//比较对象和int的大小
bool operator<(const int &t1) const{
return (v < t1);
}
//友元函数,比较int和对象的大小
friend inline bool operator<(const int &a, const test & t1){
return (a < t1.v);
}
//对象间赋值
test & operator=(const test &t1){
v = t1.v;
return *this;
}
//int赋值给对象
test & operator=(const int &t1){
v = t1;
return *this;
}
//对象加上 int
test operator+(const int & a){
test t1;
t1.v = v + a;
return t1;
}
//对象加对象
test operator+(test &t1){
test t2;
t2.v = v + t1.v;
return t2;
}
//对象加上对象
test &operator+=(const test &t1){
v += t1.v;
return *this;
}
//对象加上int
test &operator+=(const int &a){
v += a;
return *this;
}
//对象==对象
bool operator==(const test &t1)const{
return (v == t1.v);
}
//对象==int
bool operator==(const int &t1)const{
return (v == t1);
}
friend inline ostream & operator << (ostream & os, test &t1){
cout << "class t(" << t1.v << ")" << endl;
return os;
}
friend inline istream & operator >> (istream & is, test &t1){
cin >> t1.v;
return is;
}
};
int main(){
test t0, t1(3);
test t2(t1);
cout << t0 << t1 << t2;
cin >> t1;
t2 = t1;
t2 += t1;
t1 += 10;
cout << t2;
if(t1 < t2) cout << "t1 < t2";
else if(t1 == t2) cout << "t1 = t2";
else cout << "t1 > t2";
cout <<endl;
system("pause");
return 0;
}
以下示例中定义了一个class test, 重载了<, +, +=, =, ==, <<, >>等符号:
#include<iostream>
#include<vector>
using namespace std;
class test{
public:
int v;
test():v(0){}
test(const int &a):v(a){}
test(const test &t1):v(t1.v){}
//比较两个对象的大小
bool operator<(const test &t1) const{
return (v < t1.v);
}
//比较对象和int的大小
bool operator<(const int &t1) const{
return (v < t1);
}
//友元函数,比较int和对象的大小
friend inline bool operator<(const int &a, const test & t1){
return (a < t1.v);
}
//对象间赋值
test & operator=(const test &t1){
v = t1.v;
return *this;
}
//int赋值给对象
test & operator=(const int &t1){
v = t1;
return *this;
}
//对象加上 int
test operator+(const int & a){
test t1;
t1.v = v + a;
return t1;
}
//对象加对象
test operator+(test &t1){
test t2;
t2.v = v + t1.v;
return t2;
}
//对象加上对象
test &operator+=(const test &t1){
v += t1.v;
return *this;
}
//对象加上int
test &operator+=(const int &a){
v += a;
return *this;
}
//对象==对象
bool operator==(const test &t1)const{
return (v == t1.v);
}
//对象==int
bool operator==(const int &t1)const{
return (v == t1);
}
friend inline ostream & operator << (ostream & os, test &t1){
cout << "class t(" << t1.v << ")" << endl;
return os;
}
friend inline istream & operator >> (istream & is, test &t1){
cin >> t1.v;
return is;
}
};
int main(){
test t0, t1(3);
test t2(t1);
cout << t0 << t1 << t2;
cin >> t1;
t2 = t1;
t2 += t1;
t1 += 10;
cout << t2;
if(t1 < t2) cout << "t1 < t2";
else if(t1 == t2) cout << "t1 = t2";
else cout << "t1 > t2";
cout <<endl;
system("pause");
return 0;
}
相关文章推荐
- c++重载<, +, +=, =, ==, <<, >>等符号
- C++实现类String--含构造函数以及重载>>,<<,>,<,==,=
- &lt;转载自刘佳ID:freedom0203和waret&gt; C++中成员初始化列表的使用
- <转> 模板类 声明和定义 编译 c++
- c++中<array>的使用
- 《C++第九周实验报告1-1》--------接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出
- <C++>error LNK2001错误
- <kingofark关于学习C++和编程的50个观点> 详解 - 预览版
- <<c++ primer>> 函数指针的笔记
- <转载> 设计模式精解c++-GoF 23 种设计模式解析
- 复制控制(学习<C++Primer>)
- <The C++ Programming Language> 读书笔记
- C++ 中dynamic_cast<>的使用方法
- Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法
- 《C++第九周实验报告3-1》----接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出
- C++中指针和引用的区别-转载fu_jiangtao<chinaunix>
- C++之<<每天一点点>>
- lua <--> c++
- Asp.Net中几种相似的标记符号: <%=...%>< %#...%>< %%>< %@ %>解释及用法
- <C++学习笔记>预处理功能