有关C++类的一个题目的疑问
建立三个类,一个为日期类Date,一个为时间类Time,另一个为上面两个类的派生类DateTime。为什么date2dateandtimeis1987/8/12;12:4...
建立三个类,一个为日期类Date,一个为时间类Time,另一个为上面两个类的派生类DateTime。
为什么 date2 date and time is 1987/8/12;12:45:10??
DATE2变成了1987呢?
#include <iostream>
#include <string>
#include <stdio.h>
typedef char string80[80];
class Date
{
public:
Date(){ }
Date(int y,int m,int d){ SetDate(y,m,d);}
void SetDate(int y,int m,int d)
{ Year=y;Month=m;Day=d; }
void GetStringData(string &Date)
{sprintf(Date,”%d/%d/%d”,Year,Month,Day); }
protected:
int Year,Month,Day;
};
class Time
{
public:
Time(){ }
Time(int h,int m,int s){ SetTime(h,m,s);}
void SetTime(int h,int m,int s)
{ Hours=h;Minutes=m;Seconds=s; }
void GetStringTime(string80 &Time)
{sprintf(Time,”%d:/%d:/%d”,Hours,Minutes,
Seconds);}
protected:
int Hours,Minutes,Seconds;
};
class TimeDate: public Date,public Time
{
public:
TimeDate():Date(),Time(){ }
TimeDate(int y,int mo,int d,int h,int mi,
int s):Date(y,mo,d),Time(h,mi,s) { }
void GetStringDT(string80 &DTstr)
{
sprintf(DTstr,”%d/%d/%d;%d:/%d:/%d”,
Year, Month,Day,Hours,Minutes,Seconds);
}
};
void main( )
{ TimeDate date1,date2(1998,8,12,12,45,10);
string DemoStr;
date1.SetDate(1998,8,7); //来自于Date基类
date1.SetTime(10,30,45); //来自于Time基类
date1.GetStringDT(DemoStr); //自身成员函数
cout<<“The date1 date and time is” <<DemoStr<<endl;
date1.GetStringDate(DemoStr); //来自于Date基类
cout<<“The date1 date is” <<DemoStr<<endl;
date1.GetStringTime(DemoStr); //来自于Time基类
cout<<“The date1 time is” <<DemoStr<<endl;
Date2.GetStringDT(DemoStr); //自身成员函数
cout<<“The date2 date and time is” <<DemoStr<<endl; }
执行该程序输出结果如下:
The date1 date and time is 1998/8/7;10:30:45
The date1 date is 1998/8/7
The date1 time is 10:30:45
The date2 date and time is 1987/8/12;12:45:10
为什么 date2 date and time is 1987/8/12;12:45:10??
DATE2变成了1987呢? 展开
为什么 date2 date and time is 1987/8/12;12:45:10??
DATE2变成了1987呢?
#include <iostream>
#include <string>
#include <stdio.h>
typedef char string80[80];
class Date
{
public:
Date(){ }
Date(int y,int m,int d){ SetDate(y,m,d);}
void SetDate(int y,int m,int d)
{ Year=y;Month=m;Day=d; }
void GetStringData(string &Date)
{sprintf(Date,”%d/%d/%d”,Year,Month,Day); }
protected:
int Year,Month,Day;
};
class Time
{
public:
Time(){ }
Time(int h,int m,int s){ SetTime(h,m,s);}
void SetTime(int h,int m,int s)
{ Hours=h;Minutes=m;Seconds=s; }
void GetStringTime(string80 &Time)
{sprintf(Time,”%d:/%d:/%d”,Hours,Minutes,
Seconds);}
protected:
int Hours,Minutes,Seconds;
};
class TimeDate: public Date,public Time
{
public:
TimeDate():Date(),Time(){ }
TimeDate(int y,int mo,int d,int h,int mi,
int s):Date(y,mo,d),Time(h,mi,s) { }
void GetStringDT(string80 &DTstr)
{
sprintf(DTstr,”%d/%d/%d;%d:/%d:/%d”,
Year, Month,Day,Hours,Minutes,Seconds);
}
};
void main( )
{ TimeDate date1,date2(1998,8,12,12,45,10);
string DemoStr;
date1.SetDate(1998,8,7); //来自于Date基类
date1.SetTime(10,30,45); //来自于Time基类
date1.GetStringDT(DemoStr); //自身成员函数
cout<<“The date1 date and time is” <<DemoStr<<endl;
date1.GetStringDate(DemoStr); //来自于Date基类
cout<<“The date1 date is” <<DemoStr<<endl;
date1.GetStringTime(DemoStr); //来自于Time基类
cout<<“The date1 time is” <<DemoStr<<endl;
Date2.GetStringDT(DemoStr); //自身成员函数
cout<<“The date2 date and time is” <<DemoStr<<endl; }
执行该程序输出结果如下:
The date1 date and time is 1998/8/7;10:30:45
The date1 date is 1998/8/7
The date1 time is 10:30:45
The date2 date and time is 1987/8/12;12:45:10
为什么 date2 date and time is 1987/8/12;12:45:10??
DATE2变成了1987呢? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询