选择题
11.
有如下类定义:
class Date{
public:
Date(int y=2014, int m=9, int d=9):year(y), month(m), day(d){}
______(ostream&stream, Date&ddd); //运算符<<的声明
private:
int year, month, day;
};
若要使语句序列
Date GoodDay;
cout<<GoodDay<<endl;
能够正常运行,横线处的内容应为______。
A、
istream& operator <<
B、
ostream& operator <<
C、
friend istream& operator <<
D、
friend ostream& operator <<
【正确答案】
D
【答案解析】
本题考查的是输出运算符<<的重载首先函数的第一个形参类型为ostream&,所以返回值也应该是ostream&,以保证输出运算符可以链接使用,选项A、C错误;由于L/O操作符的第一个形参类型并不是Date类类型,所以不能作为成员函数,必须使用友元函数,声明前必须加关键字friend,选项B错误,选项D正确。
提交答案
关闭