C++读书笔记之 赋值运算符= 重载 前缀自减运算符--重载 求负运算符- 重载
来源:互联网 发布:航海家炒股软件 公式 编辑:程序博客网 时间:2024/06/05 18:45
/**重载赋值运算符 = **/
void operator=(const Distance &D )
{
feet = D.feet;
inches = D.inches;
}
/**重载负号运算符 - **/
Distance operator-()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
/**重载前缀自减运算符 -- **/
Distance operator--()
{
--feet;
--inches;
return Distance(feet, inches);
}
/**重载后缀自减运算符 -- **/
Distance operator--(int)
{
Distance Temp(feet,inches);
feet--;
inches--;
return Temp;
void operator=(const Distance &D )
{
feet = D.feet;
inches = D.inches;
}
/**重载负号运算符 - **/
Distance operator-()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
/**重载前缀自减运算符 -- **/
Distance operator--()
{
--feet;
--inches;
return Distance(feet, inches);
}
/**重载后缀自减运算符 -- **/
Distance operator--(int)
{
Distance Temp(feet,inches);
feet--;
inches--;
return Temp;
}
#include <iostream>using namespace std;class Distance{ private: double feet; // 0 to infinite double inches; // 0 to 12 public: // required constructors Distance() { feet = 0.0; inches = 0.0; } Distance(double f, double i) { feet = f; inches = i; } /**重载赋值运算符 = **/ void operator=(const Distance &D ) { feet = D.feet; inches = D.inches; } /**重载负号运算符 - **/ Distance operator-() { feet = -feet; inches = -inches; return Distance(feet, inches); } /**重载前缀自减运算符 -- **/ Distance operator--() { --feet; --inches; return Distance(feet, inches); } /**重载后缀自减运算符 -- **/ Distance operator--(int) { Distance Temp(feet,inches); feet--; inches--; return Temp; } // method to display distance void displayDistance() { cout << "Feet: " << feet << " Inches:" << inches << endl; }};int main(){ Distance D1(19.91, -20.13),D2; cout << "\nD1 Distance now: \n"; D1.displayDistance(); --D1; cout << "\nD1 Distance after --D1 : \n"; D1.displayDistance(); -D1; cout << "\nD1 Distance after -D1 : \n"; D1.displayDistance(); cout << "\nD1--.displayDistance():\n"; D1--.displayDistance(); // use assignment operator D2 = D1--; cout << "\nafter D2=D1-- Distance of D2 :\n"; D2.displayDistance(); cout << "\nafter D2=D1-- Distance of D1 :\n"; D1.displayDistance(); return 0;}/**************************运行结果:D1 Distance now:Feet: 19.91 Inches:-20.13D1 Distance after --D1 :Feet: 18.91 Inches:-21.13D1 Distance after -D1 :Feet: -18.91 Inches:21.13D1--.displayDistance():Feet: -18.91 Inches:21.13after D2=D1-- Distance of D2 :Feet: -19.91 Inches:20.13after D2=D1-- Distance of D1 :Feet: -20.91 Inches:19.13Process returned 0 (0x0) execution time : 0.819 sPress any key to continue.***************************/
- C++读书笔记之 赋值运算符= 重载 前缀自减运算符--重载 求负运算符- 重载
- C++:赋值运算符重载
- 重载赋值运算符
- 重载赋值运算符
- 重载赋值运算符
- 重载赋值运算符
- 重载赋值运算符
- 重载赋值运算符
- 赋值运算符重载
- 赋值运算符重载
- 重载赋值运算符
- 重载赋值运算符
- 赋值运算符重载
- 赋值运算符重载
- 重载赋值运算符
- 重载赋值运算符=
- 重载之运算符重载
- C++读书笔记之 下标运算符 [] 重载 运算符重载
- 【数据库】mysql实时同步经验分享
- VC++获得当前系统日期和时间问题汇总
- Velocity 局部静态化 浅谈
- Sicily 1543 Completing Brackets (SOJ 1543) 【括号匹配】
- [Python]项目打包:5步将py文件打包成exe文件
- C++读书笔记之 赋值运算符= 重载 前缀自减运算符--重载 求负运算符- 重载
- 淘宝网店优惠券营销活动攻略
- a famous quote by Calvin Coolidge (1872) Persistence and determination alone are omnipotent
- Python--绘图工具matplotlib的使用
- libvirt API 简介(二)
- 历史名人1——美尼斯
- Java编程思想--一切皆是对象
- axis2使用教程、webservices自定义参数,webservices自定义返回值
- Mule Studio版本3.4案例--Foreach Processing and Choice Routing Example