题目内容 (请给出正确答案)
[主观题]

运算符重载是一种特殊的函数重载,其函数名为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、<<和>>

点击查看答案
热门考试 全部 >
相关试卷 全部 >
账号:
你好,尊敬的上学吧用户
发送账号至手机
获取验证码
发送
温馨提示
该问题答案仅针对搜题卡用户开放,请点击购买搜题卡。
马上购买搜题卡
我已购买搜题卡, 登录账号 继续查看答案
重置密码
确认修改
谢谢您的反馈

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

警告:系统检测到您的账号存在安全风险

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

微信搜一搜
上学吧
点击打开微信
警告:系统检测到您的账号存在安全风险
抱歉,您的账号因涉嫌违反上学吧购买须知被冻结。您可在“上学吧”微信公众号中的“官网服务”-“账号解封申请”申请解封,或联系客服
微信搜一搜
上学吧
点击打开微信