重要提示: 请勿将账号共享给其他人使用,违者账号将被封禁!
查看《购买须知》>>>
找答案首页 > 全部分类 > 求职面试
搜题
网友您好, 请在下方输入框内输入要搜索的题目:
搜题
题目内容 (请给出正确答案)
[主观题]

运算符重载是一种特殊的函数重载,其函数名为operator <重载运算符> 。

查看答案
更多“运算符重载是一种特殊的函数重载,其函数名为operator <重载运算符> 。”相关的问题

第1题

( 13 )在有理数类 Rational 中重载插入运算符 << ,以便按 a/q 形式输出。请将 << 运算符函

数的定义补充完整。

class Rational{

public:

Rational(int aa, int qq):a(aa),q(qq){ }

friend 【 14 】 operator<<(ostream &out, Rational &x)

{

return (out<<x.a<< ' / ' <<x.q);

}

private:

int a,q;

};

点击查看答案

第2题

●试题八

阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。

【说明】

设计一个日期类Date包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。

在Date类中设计如下重载运算符函数:

Date operator+(int days):返回某日期加上天数得到的日期。

Date operator-(int days):返回某日期减去天数得到的日期。

int operator-(Date&b):返回两日期相差的天数。

【程序】

#include<iostream.h>

int day tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},

{31,29,31,30,31,30,31,31,30,31,30,31}};

∥day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年class Date

{

int year,month,day;∥年,月,日

int leap(int);∥判断是否为闰年

int dton(Date&);

Date ntod(int);

public:

Date(){}

Date(int y,int mint d){year=y;month=m;day=d;}

void setday(intd){day=d;}

void setmonth(int m){month=m;}

void setyear(int y){year=y;}

int getday(){return day;}

int getmonth(){return month:}

int getyear(){return year;)

Date operator+(int days)∥+运算符重载函数

{

static Date date;

int number= (1) ;

date=ntod(number);

return date;

}

Date operator-(int days)∥-运算符重载函数

{

staffs Date date;

int number= (2) ;

number-=days;

date=ntod(number);

return date;

}

int operator-(Date &b)∥-运算符重载函数

{

int days= (3) ;

return days;

}

void disp()

{

cout<<year<<"."<<month<<"."<<day<<endl;

}

};

int Date::leap(int year)

{if( (4) )∥是闰年

return 1;∥不是闰年

else

return0:

}

int Date::dton(Date &d)∥求从公元0年0月0日到d日期的天数

{

inty,m,days=0;

for(y=1;y<=d.year;y++)

if( (5) )days+=366;∥闰年时加366天

else days+=365;∥非闰年时加365天

for(m=0;m<d.month-1;m++)

if( (6) )

days+=day_tab[1][m];

else

days+=day_tab[0][m];

days+=D.day;

return days;

}

Date Date::ntod(intn)∥将从公元0年0月0日的天数转换成日期

{

int y=1,m=1,d,rest=n,lp;

while (1)

{if(leap(y))

if(rest<=366)break;

else rest-=366;

else∥非闰年

if(rest=365)break;

else rest-=365;

y++;

}

y--;

Ip=Ieap(y);

while (1)

{

if(Ip)∥闰年

if(rest>day_tab[1][m-1])rest-=day_tab[1][m-1];

else break;

else∥非闰年

if(rest>day_tab[0][m-1])rest-=day_tab[0][m-1];

else break;

m++;

}

d=rest;

return Date(y;m,d);

}

void main()

{

Date now(2003,10,1),then(2005,6,5);

cout<<"now:";now.disp();

cout<<"then:";then.disp();

cout<<"相差天数:"<<(then-now)<<endl;

Date dl=now+1000,d2=now-1000;

cout<<"now+1000:";d1.disp();

cout<<"now-1000:":d2.disp();

}

点击查看答案

第3题

( 33 )有如下程序:

#include<iostream>

using namespace std;

class Pair{

int m;

int n;

public:

Pair ( int i , int j ) : m ( i ) , n ( j ) {}

boot operator > ( pair p ) const; // 须在类体外给出定义

} ;

int main () {

Pair Al ( 3,4 ) , p2 ( 4,3 ) ; p3 ( 4,5 ) ;

Cout<< ( pl>p2 ) << ( P2>P1 ) << ( p2>p3 ) << ( p3>p2 ) ;

return 0;

}

运算符函数 。 operator> 的功能是比较两个 Pair 对象的大小 , 当左边对象大时 , 返回 true , 否则返 回false 。 比较规则是首先比较两对象的 m 成员 , m 大者为大 ; 当 m 相等时比较 n , n 大者为大 。 程序输出 0101 ,下列对运算符重载函数的正确定义是

A ) bool Pair::operator> ( Pair p ) const

{if ( m!=p.m ) return m>p.m; return n>p.n;}

B ) bool Pair:;operator> ( Pair p )

{if ( m!=p.m ) return m>p.m; return n>p.n;}

C ) bool Pair::operator> ( Pair p ) const

{if ( m>p.m ) return true; return n>p.n;}

D ) bool Pair:;operator> ( Pair p )

{if ( m>p.m ) return true; return n>p.n;}

点击查看答案

第4题

有如下程序:

}}}}include<iostream>

using namespace std;

class Pair{

int m;

int n;

public:

Pair(int i,int J):m(i),n(J){}

bool operator>(Pair P)const; //需在类体外给出定义

};

int main(){

Pair pl(3,4),p2(4,3),p3(4,5);

COUt<<(pl>p2)<<(p2>p1)<<(p2>p3)<<(p3>p2);

return 0;

{

运算符函数operator>功能是比较两个Pair对象的大小,当左边对象大时,返回true,否则返回false。比较规则是首先比较两对象的m成员,m大者为大;当m相等时比较n.n大者为大。程序输出0101,下列对运算符重载函数的正确定义是

A.bool Pair::operator>(Pair P)const {if(m!=P.m)return m>P.m;return n>P.n;)

B.bool Pair::operator>(Pair P) {if(m!=P.m)return m>P.m;return n>P.n;)

C.bool Pair::operator>(Pair P)const {if(m>P.m)return true;return 11>P.n;)

D.bool Pair::operator>(Pair P) {if(m>P.m)return true;return 11>P.n;}

点击查看答案

第5题

下列运算符中,不属于关系运算符的是()。
A.<

B.>=

C.!

D.!=

点击查看答案

第6题

>>符号和< <符号都是被重载的函数。>
点击查看答案

第7题

下面的程序是在str类中重载运算符=,请将程序补充完整,使程序的输出结果为:

he

she

he

he

#include <iostream>

using namespace std;

class str

{

private:

char *st;

public:

str(char *a)

{

set(a);

}

str & perator=(## )

{

delete []st;

set(a.st);

return *this;

}

void show()

{

cout<<st<<endl;

}

~str()

{

delete []st;

}

void set(char *s)

{

st=new char[strlen(s)+1];

strcpy(st,s);

}

};

int main()

{

str s1("he"),s2("she");

s1.show();

s2.show();

##;

s1.show();

s2.show();

}

点击查看答案

第8题

以下不是为运算符的是?

A、&和|

B、^和~

C、<和>

D、<<和>>

点击查看答案
下载上学吧APP
客服
TOP
重置密码
账号:
旧密码:
新密码:
确认密码:
确认修改
购买搜题卡查看答案
购买前请仔细阅读《购买须知》
请选择支付方式
微信支付
支付宝支付
选择优惠券
优惠券
请选择
点击支付即表示你同意并接受《服务协议》《购买须知》
立即支付
搜题卡使用说明

1. 搜题次数扣减规则:

功能 扣减规则
基础费
(查看答案)
加收费
(AI功能)
文字搜题、查看答案 1/每题 0/每次
语音搜题、查看答案 1/每题 2/每次
单题拍照识别、查看答案 1/每题 2/每次
整页拍照识别、查看答案 1/每题 5/每次

备注:网站、APP、小程序均支持文字搜题、查看答案;语音搜题、单题拍照识别、整页拍照识别仅APP、小程序支持。

2. 使用语音搜索、拍照搜索等AI功能需安装APP(或打开微信小程序)。

3. 搜题卡过期将作废,不支持退款,请在有效期内使用完毕。

请使用微信扫码支付(元)
订单号:
遇到问题请联系在线客服
请不要关闭本页面,支付完成后请点击【支付完成】按钮
遇到问题请联系在线客服
恭喜您,购买搜题卡成功 系统为您生成的账号密码如下:
重要提示: 请勿将账号共享给其他人使用,违者账号将被封禁。
发送账号到微信 保存账号查看答案
怕账号密码记不住?建议关注微信公众号绑定微信,开通微信扫码登录功能
警告:系统检测到您的账号存在安全风险

为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!

- 微信扫码关注上学吧 -
警告:系统检测到您的账号存在安全风险
抱歉,您的账号因涉嫌违反上学吧购买须知被冻结。您可在“上学吧”微信公众号中的“官网服务”-“账号解封申请”申请解封,或联系客服
- 微信扫码关注上学吧 -
请用微信扫码测试
选择优惠券
确认选择
谢谢您的反馈

您认为本题答案有误,我们将认真、仔细核查,如果您知道正确答案,欢迎您来纠错

上学吧找答案