首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
下面程序中对一维坐标点类Point进行运算符重载 #include <iostream> using namespace std; class Point { public: Point(int val) {x=val;}
下面程序中对一维坐标点类Point进行运算符重载 #include <iostream> using namespace std; class Point { public: Point(int val) {x=val;}
admin
2017-10-17
24
问题
下面程序中对一维坐标点类Point进行运算符重载 #include <iostream> using namespace std; class Point { public: Point(int val) {x=val;} Point & operator++() {x++; return *this; } Point operator++(int) {Point old = *this; ++(*this); return old;} int GetX() const {return x;} private: int x; }; int main() { Point a(10); cout << (++a).GetX(); cout << a++.GetX(); return 0; }编译和运行情况是
选项
A、运行时输出1011
B、运行时输出1111
C、运行时输出1112
D、编译有错
答案
B
解析
本题考查的知识点是:重载增1运算符“++”。++既可以是前缀运算符(前增1),又可以是后缀运算符(后增1)。为了区分这两种情况,重载这两个运算符时必须在格式上有所区别:重载后缀++时必须多一个虚拟参数:int。在本题中,Point& operator++()重载的是前缀形式,而Point operator++(int)则是后缀形式。所以,主函数中第1条输出语句cout<<(++a).GetX();等价于cout<<a.operator++().GetX();,即输出 11,此时a.x的值为11。而第2条输出语句cout<<a++.GetX();等价于cout<< a.operator++(0).GetX();(式中的0可以是任意整数),在这个后缀++的重载函数中,首先创建了一个Point对象old保存自身(*this),然后通过语句++(*this];调用前缀++的重载函数,此时自身虽然已经改变,但它返回的是改变前保存的old对象,因此还是输出11。故本题应该选择B。
转载请注明原文地址:https://kaotiyun.com/show/RWAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
已知:intn=10;那么下列语句中错误的是
有以下程序:#include<iostream>usingnamespacestd;classMyClass{public:MyClass();~MyClass();
若要把类FriendClass定义为类MyClass的友元类,则应在类MyClass的定义中加入语句【】。
对类成员访问权限的控制,是通过设置成员的访问控制属性实现的,下列不是访问控制属性的是
程序设计语言的基本成分是数据成分、运算成分、控制成分和
下列程序不能通过编译,应该在划线部分填写的语句是【 】。#include<iostream.h>#include<stdlib.h>doubleFune(inta,intb,charch){doublex;swit
下列不能作为类的成员的是
随机试题
8岁患儿,胸骨左缘第3、4肋间听到响亮而粗糙的收缩期杂音,应考虑为
男孩,2岁,经常患感冒和肺炎。体检:心前区隆起,无震颤,胸骨左缘第2肋间可闻及Ⅱ级喷射性杂音,P2>A2伴有固定分裂,心电图示:电轴右偏,V1呈rsR’波型,P-R间期0.16秒,该患儿最可能的诊断为
施工项目经理部在对施工机械使用费支出的控制过程中,采取合理安排施工生产、加强机械调度工作等的成本控制措施,主要作用是控制机械的()。
普通股是一种永久的长期融资形式,没有到期日。()
某客户是中低收入者,风险承受能力较低,对这类客户适宜推销的理财产品是()。
国有单位之间产权界定应遵循的原则有()。
商业银行在中央银行的法定准备金存款,要按规定的调整日定期调整,当调整日按规定计算的应交法定存款准备金大于上一调整日所计算的法定存款准备金时,其()。
社会工作者老张为王女士提供生涯规划辅导时,无意中发现王女士的丈夫对她有暴力行为,此时,老张首先应该做的是()
假如你的一个很要好的朋友找你借了500元钱,但是他却一直都不还你,你会怎么做?
阅读以下文字,完成61—65题。浮船坞是一种修造舰船的大型装备。外观就像是把一个干船坞从岸边“刨”了出来,主结构是一个巨大的凹字形船舱,两侧有水密结构的墙,前后端是可以开合的门。实际上是一种构造特殊的槽形平底船,且船底被设计得尤为坚固,以承受大型
最新回复
(
0
)