C++程序设计试卷
cout<< horizontalDistance(a,b) < 12、编写class cA的派生类class cAB,增加成员函数,用于求圆的周长 class cA { int r; public: cA(int x){ r=x; } double area( ) { return r*r*3.14; } int get_r( ) { return r; } }; class cAB:public Ca {cAB(int x):cA(x){ } double girth( ) { return 2*get_r( )*3.14; }}; 16、定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:①构造函数重载;②成员函数设置缺省参数;③定义一个友元函数来打印日期(3分);④可使用不同的构造函数来创建不同的对象。 #include TDate(); //构造函数 TDate(int nMoth,int nDay,int nYear); //构造函数重载 void SetDay(int nDay=1); //三个设置某个成员变量的成员函数,都带有默认值 void SetMonth(int nMonth=1); void SetYear(int nYear=2001); void SetDate(int nMoth,int nDay,int nYear);//一个非静态成员函数 friend void PrintDate(TDate cTdate); //友员函数 private: int m_nMonth; 第 26 页 共 29 页 C++程序设计试卷 int m_nDay; int m_nYear; }; TDate::TDate() { m_nDay=1; m_nMonth=1; m_nYear=2000; } TDate::TDate(int nMoth,int nDay,int nYear) { m_nYear=nYear; m_nDay=nDay; m_nMonth=nMoth; } void TDate::SetDate(int nMoth,int nDay,int nYear) { m_nYear=nYear; m_nDay=nDay; m_nMonth=nMoth; } void TDate::SetDay(int nDay/*=1*/) { m_nDay=nDay; } void TDate::SetMonth(int nMonth/*=1*/) { m_nMonth=nMonth; } 第 27 页 共 29 页 C++程序设计试卷 void TDate::SetYear(int nYear/*=2000*/) { m_nYear=nYear; } void PrintDate(TDate cTDate) { printf(\} void main() { TDate cTdate; cTdate.SetDate(6,1,2012); //使用成员函数 cTdate.SetDay(10); TDate CMyDate(6,1,2012);//重载的构造函数生成对象实例 PrintDate(CMyDate); //使用友员函数 } 23、定义个datetime类,使其对象可以显示当前系统时间和日期 。 编写一个程序,该程序的输入有三个整数,找出其中最大的两个整数并输出这两个整数的和 #include public: int year; int month; int day; int hour; int min; int sec; datetime() { struct tm* ptm; time_t m; time(&m); ptm = localtime(&m); year = ptm->tm_year+1900; 第 28 页 共 29 页 C++程序设计试卷 month = ptm->tm_mon+1; day = ptm->tm_mday; hour = ptm->tm_hour; min = ptm->tm_min; sec = ptm->tm_sec; } void output() { printf(\ } }; void main(void) { datetime d; d.output(); } 第 29 页 共 29 页 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库大学C++期末考试题库及答案(更正部分答案)(6)在线全文阅读。
相关推荐: