定义Boat和Car两个类,两者都有私有成员weight属性,定义两者的一个友员函数totalweight(),计算两个类的对象的重量和。
请根据给定的main函数和totalweight()函数的定义,完善Boat和Car两个类。
友元函数的使用
#include <iostream>
using namespace std;
class Car;
class Boat{
friend int totalweight(Boat &b,Car &a);
public:
Boat(int w = 0){
weight = w;
};
private:
int weight;
};
class Car{
friend int totalweight(Boat &b,Car &a);
public:
Car(int w = 0){
weight = w;
}
private:
int weight;
};
int totalweight(Boat& b,Car& c)
{
return b.weight+c.weight;
}
int main()
{
Boat b(100);
Car c(200);
cout<<totalweight( b, c)<<endl;
return 0;
}
【问题描述】声明一个表示时间的类CTime,可以精确表示年、月、日、小时、分、秒,请计算两个日期对象之间相隔的天数。
要求:
1、包括私有成员年、月、日、小时、分、秒。
2、请使用构造函数实现的类的初始化工作,并判断日期和时间的有效性。年月日时分秒应该在正确的范围内。考虑闰年时候二月份的情况。时间的格式是xx:xx:xx,小时不是超过23,分钟和秒不能超过59。
1)如果日期无效,则输出 “date error! ” 并将年、月、日、小时、分、秒置为0。
2)如果时间无效,则输出 “time error! ” 并将年、月、日、小时、分、秒置为0。
3)如果日期和时间都有效,则根据传递的参数初始化年、月、日、小时、分、秒。
4)构造函数的三个参数:小时、分、秒 设计为默认形成,其默认值为0。
5) 输出"构造函数被调用"
3、请设计一个拷贝构造函数,实现将参数的值全部传递给当前对象,同时输出“拷贝构造函数被调用”
4、请设计一个析构函数,同时输出“析构函数被调用”
5、设计一个成员函数 int dayDiff(CTime t) ,用于计算当前对象与形参t之间的相隔的天数,注意相隔天数为大于等于0的正整数。注意闰年的问题。
6、设计一个成员函数 showTime(),用于显示日期,显示格式为:2020/3/12 11:50:20
#include<iostream>
using namespace std;
int isRuiYear(int y){
if( y%4==0 ){
if( y%100==0 && y%400!=0) return 0;
else return 1;
}
return 0;
}
int Maxday(int y,int m){
if( m == 2 ){
if( isRuiYear(y) ) return 29;
return 28;
}
else if( m==1 ||m ==3 || m == 5 || m ==7 || m ==8 || m== 10 || m==12 ) return 31;
return 30;
}
int DAYS(int y,int m,int d){
int res = 0;
res += ( y/4 )*1461;
for(int i = y/4*4+1;i<=y;i++){
if( isRuiYear(i) ) res += 366;
else res += 365;
}
for(int i=1;i<m;i++){
res+=Maxday(y,i);
}
return res+d;
}
class CTime{
public:
~CTime(){
cout<<"析构函数被调用"<<endl;
}
bool isvalid(){
return check(year,month,day,hour,minute,second)==0;
}
int check(int y,int m,int d,int h,int M,int s){
if( y<0 ||m<1 || d>Maxday(y,m) ||d<1 ) return 1;
if( h<0 || M<0 || s<0 || h>23 || M>59 || s>59 ) return 2;
return 0;
}
void Initial(int y,int m,int d,int h,int M,int s){
year=y ,month=m ,day=d ,hour=h ,minute=M ,second=s ;
}
CTime(int y,int m,int d,int h=0,int M=0,int s=0){
if( check( y, m, d, h, M, s)==1 ){
cout<<"date error!"<<endl;
Initial(0,0,0,0,0,0);
}
else if( check( y, m, d, h, M, s)==2 ){
cout<<"time error!"<<endl;
Initial(0,0,0,0,0,0);
}
else
Initial(y,m,d,h,M,s);
cout<<"构造函数被调用"<<endl;
}
CTime(const CTime &CT){
cout<<"拷贝构造函数被调用"<<endl;
year=CT.year ,month=CT.month ,day=CT.day ,hour=CT.hour ,minute=CT.minute ,second=CT.second ;
}
void showTime(){
cout<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<minute<<":"<<second<<endl;
}
int dayDiff(CTime t1);
private:
int year,month,day,hour,minute,second;
};
int CTime :: dayDiff(CTime t1){
int res = DAYS( year,month,day) - DAYS(t1.year,t1.month,t1.day );//先不考虑时分秒
int flag = 0,flag2 = 0;
if( hour!=t1.hour && hour<t1.hour) flag= 1;
else if( minute !=t1.minute&&minute < t1.minute ) flag = 1;
else if( second !=t1.second &&second < t1.second ) flag = 1;
if( hour!=t1.hour && hour>t1.hour) flag2= 1;
else if( minute !=t1.minute && minute > t1.minute ) flag2 = 1;
else if( second !=t1.second &&second > t1.second) flag2 = 1;
//flag 表示 t0 的时分秒小于 t1的时分秒 flag2 同理
if( res>0 && flag ) res--;
if( res<0 && flag2 ) res++;
return res>0?res:-res;
}
int main()
{
int year, month, day, hour, minute, second;
cin >> year >> month >> day >> hour >> minute >> second;
CTime t1(year, month, day, hour, minute, second);
t1.showTime();
CTime t2(2000, 1, 1); //利用默认形参将时间初始化为00:00:00
if (t1.isvalid()) //如果日期和时间合法,则计算天数
{
int days=0;
days=t1.dayDiff(t2);
cout << "这两天之间相隔了" << days << "天" << endl;
days=t2.dayDiff(t1);
cout << "这两天之间相隔了" << days << "天" << endl;
}
}
/*
【样例输入1】
3 12 11 50 20
【样例输出1】
构造函数被调用
2020/3/12 11:50:20
构造函数被调用
拷贝构造函数被调用
析构函数被调用
这两天之间相隔了7376天
拷贝构造函数被调用
析构函数被调用
这两天之间相隔了7376天
析构函数被调用
析构函数被调用
*/
【问题描述】设计一个CRectangle类,其中包括CPoint类的两个对象成员,表示左上角和右下角的两个点。要求求解矩形的面积。
注意,每个类的构造函数、拷贝构造函数需要输出“*** is called”,具体的请根据输出进行分析。
main函数已经给定如下:
int main()
{
int a=1, b=1, c=6, d=11;
cout<<"# Define p1 ######"<<endl;
CPoint p1;
cout<<"# Define p2 ######"<<endl;
CPoint p2(10,20);
cout<<"# Define rect1 ######"<<endl;
CRectangle rect1;
cout<<"# Define rect2 ######"<<endl;
CRectangle rect2(p1, p2);
cout<<"# Define rect3 ######"<<endl;
CRectangle rect3(a, b, c, d);
cout<<"# Define rect4 ######"<<endl;
CRectangle rect4(rect2);
cout<<"# Calculate area ######"<<endl;
cout << "rect1面积为" << rect1.GetArea() << endl;
cout << "rect2面积为" << rect2.GetArea() << endl;
cout << "rect3面积为" << rect3.GetArea() << endl;
cout << "rect4面积为" << rect4.GetArea() << endl;
system("pause");
return 0;
}
【样例输出】
# Define p1 ######
CPoint contstructor with default value(0,0) is called.
# Define p2 ######
CPoint contstructor with default value(0,0) is called.
# Define rect1 ######
CPoint contstructor with default value(0,0) is called.
CPoint contstructor with default value(0,0) is called.
CRectangle default contstructor is called.
# Define rect2 ######
CPoint copy contstructor is called.
CPoint copy contstructor is called.
CPoint copy contstructor is called.
CPoint copy contstructor is called.
CRectangle contstructor with (CPoint,CPoint) is called.
# Define rect3 ######
CPoint contstructor with default value(0,0) is called.
CPoint contstructor with default value(0,0) is called.
CRectangle contstructor with (int,int,int,int) is called.
# Define rect4 ######
CPoint copy contstructor is called.
CPoint copy contstructor is called.
CRectangle copy contstructor is called.
# Calculate area ######
rect1面积为0
rect2面积为200
rect3面积为50
rect4面积为200
#include<iostream>
using namespace std;
class CRectangle;
class CPoint{
friend CRectangle;
public:
CPoint(int x=0,int y=0){
cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
a = x,b = y;
}
CPoint( const CPoint &P ){
cout<<"CPoint copy contstructor is called."<<endl;
a = P.a,b = P.b;
}
private:
int a,b;
};
class CRectangle{
public:
CRectangle(int a=0,int b=0,int c=0,int d=0){
if( a==b&&b==c&&c==d &&d==0 ){
cout<<"CRectangle default contstructor is called."<<endl;
A.a = 0,A.b = 0,B.a = 0,B.b = 0;
}
else{
A.a = a, A.b = b,B.a = c,B.b = d;
cout<<"CRectangle contstructor with (int,int,int,int) is called."<<endl;
}
}
CRectangle(CPoint Pa,CPoint Pb): A(Pa),B(Pb){
cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
}
CRectangle(const CRectangle &rec):A(rec.A),B(rec.B){
cout<<"CRectangle copy contstructor is called."<<endl;
}
int GetArea(){
return abs( (A.a-B.a)*(A.b-B.b) );
}
private:
CPoint A,B;
};
int main()
{
int a=1, b=1, c=6, d=11;
cout<<"# Define p1 ######"<<endl;
CPoint p1;
cout<<"# Define p2 ######"<<endl;
CPoint p2(10,20);
cout<<"# Define rect1 ######"<<endl;
CRectangle rect1;
cout<<"# Define rect2 ######"<<endl;
CRectangle rect2(p1, p2);
cout<<"# Define rect3 ######"<<endl;
CRectangle rect3(a, b, c, d);
cout<<"# Define rect4 ######"<<endl;
CRectangle rect4(rect2);
cout<<"# Calculate area ######"<<endl;
cout << "rect1面积为" << rect1.GetArea() << endl;
cout << "rect2面积为" << rect2.GetArea() << endl;
cout << "rect3面积为" << rect3.GetArea() << endl;
cout << "rect4面积为" << rect4.GetArea() << endl;
system("pause");
return 0;
}