信管14:运算符重载示例四
2015-05-27 20:42
288 查看
/*运算符作为类成员 */
#include <iostream>
using namespace std;
class Increase
{
public:
Increase(int x):value(x){}
Increase & operator++(); // 前自增
Increase operator++(int); // 后自增
void display()
{ cout <<"the value is " <<value <<endl; }
private:
int value;
};
Increase & Increase::operator++()
{ value++; // 先进行自增运算
return *this; // 再返回原对象
}
Increase Increase::operator++(int)
{ Increase temp(*this); // 临时对象存放原有对象值
value++; // 原有对象自增修改
return temp; // 返回原有对象值
}
int main()
{ Increase n(20);
n.display(); //20
(n++).display(); // 显示20
n.display(); // 显示原有对象21
(++n).display(); //22
n.display(); //22
++(++n);
n.display(); //24
(n++)++;
// 第二次自增操作对临时对象进行
n.display();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Increase
{
public:
Increase(int x):value(x){}
Increase & operator++(); // 前自增
Increase operator++(int); // 后自增
void display()
{ cout <<"the value is " <<value <<endl; }
private:
int value;
};
Increase & Increase::operator++()
{ value++; // 先进行自增运算
return *this; // 再返回原对象
}
Increase Increase::operator++(int)
{ Increase temp(*this); // 临时对象存放原有对象值
value++; // 原有对象自增修改
return temp; // 返回原有对象值
}
int main()
{ Increase n(20);
n.display(); //20
(n++).display(); // 显示20
n.display(); // 显示原有对象21
(++n).display(); //22
n.display(); //22
++(++n);
n.display(); //24
(n++)++;
// 第二次自增操作对临时对象进行
n.display();
system("pause");
return 0;
}
相关文章推荐
- 信管14:运算符重载示例一
- 信管14:运算符重载示例三
- 信管14:运算符重载示例五
- 信管14:运算符重载作业7-9示例
- 信管14:运算符重载习题7-12示例
- 信管14:多重继承-虚基类示例代码?
- 信管14:前四章节课堂小测-编程示例代码
- 信管14:基类和派生类关系示例代码
- 信管14:函数模板作业8-15示例
- 信管14:友元函数示例
- 信管14:虚函数示例代码一
- 信管14:虚函数示例代码二
- 信管14:纯虚函数示例代码
- 信管14:函数重载示例
- 信管14:类的继承示例代码2
- 信管14:构造函数析构函数执行顺序示例
- 信管14:对象数组、指针示例分析例程
- 信管14:多重继承示例一
- 信管14:对象的成员访问示例
- 信管14:静态成员函数示例