<<,>>,++,--运算符重载
来源:互联网 发布:unity3d 人物模型真人 编辑:程序博客网 时间:2024/06/02 21:49
class person
{
public:
int age;
string name;
public:
person()
{
age = 30;
name = "vinny";
}
friend ostream& operator<<(ostream& os, person& per);//<<运算符重载
friend istream& operator>>(istream& is, person& per);//>>运算符重载
person& operator++()//重载前置++
{
age++;
return *this;
}
person& operator++(int)//重载后置++
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age++;
return tmp;
}
person& operator--()//前置--重载
{
age--;
return *this;
}
person& operator--(int)//后置--重载
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age--;
return tmp;
}
person& operator=(const person& per)//=运算符重载
{
age = per.age;
name = per.name;
return *this;
}
};
ostream& operator<<(ostream& os, person& per)//<<运算符重载
{
os << "age:" << per.age << "name:" << per.name;
return os;
}
istream& operator >> (istream& is, person& per)//>>运算符重载
{
is >> per.age >> per.name;
return is;
}
void test()
{
person per;
cin >> per;//>>运算符重载
cout << per.age << endl;//<<运算符重载
++per;//前置运算符重载
per++;//后置运算符重载
cout << per<<endl;
per--;//后置--重载
cout << per<<endl;
--per;//前置--重载
cout << per<<endl;
}
{
public:
int age;
string name;
public:
person()
{
age = 30;
name = "vinny";
}
friend ostream& operator<<(ostream& os, person& per);//<<运算符重载
friend istream& operator>>(istream& is, person& per);//>>运算符重载
person& operator++()//重载前置++
{
age++;
return *this;
}
person& operator++(int)//重载后置++
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age++;
return tmp;
}
person& operator--()//前置--重载
{
age--;
return *this;
}
person& operator--(int)//后置--重载
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age--;
return tmp;
}
person& operator=(const person& per)//=运算符重载
{
age = per.age;
name = per.name;
return *this;
}
};
ostream& operator<<(ostream& os, person& per)//<<运算符重载
{
os << "age:" << per.age << "name:" << per.name;
return os;
}
istream& operator >> (istream& is, person& per)//>>运算符重载
{
is >> per.age >> per.name;
return is;
}
void test()
{
person per;
cin >> per;//>>运算符重载
cout << per.age << endl;//<<运算符重载
++per;//前置运算符重载
per++;//后置运算符重载
cout << per<<endl;
per--;//后置--重载
cout << per<<endl;
--per;//前置--重载
cout << per<<endl;
}
0 0
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- 运算符重载
- 有关运算符重载
- 入门spring-data-JPA以及测试类的编写
- hibernate反向工程(外键约束)
- 文件 No.1 db_init.txt
- 临江仙
- 湖北民族学院oj 1776(贪心) 之 迷瘴
- <<,>>,++,--运算符重载
- Linux中线程同步:互斥锁/信号量/条件变量+无锁队列
- java 简单的链表和二叉树添加元素
- JNI入门案例
- MySQL多表查询笔记
- 监听器相关知识汇总
- Spring Boot不允许加载iframe问题解决
- Tomcat9 配置HTTPS连接
- Add Two Numbers