数的定义补充完整。
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;
};
第1题
数的定义补充完整。
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题
#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;}
第7题
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();
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!