问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程中包括类Date(“日期”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 2006-1-1 2005-12-31 2005-12-31 2006-1-1 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。 #include <iostream> using namespace std; class Date { public: Date(int y=2006, int m=1, int d=1) // ERROR ********* found********* : year=y, month=m, day=d {} /ERROR ********* found********* Date (const Date d) { this->year=d.year; this->month=d.month; this->day=d.day; } void print () const { cout << year << ’-’ << month <<’-’<< day << endl; } private: //ERROR ********* found********* int year (2006), month(1), day(1); }; int main () { Date d1, d2 (2005, 12, 31), d3; d1.print (); d2.print(); d3=d2; d2=d1; d1=d3; d1.print(); d2.print(); return 0; }

【参考答案】

(1):year(y),month(m),day(d) (2)Date(const Date&d) (3)int yea......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中包含源程序文件main.cpp和用户定义的头文件Array.h,整个程序包含有类Array的定义和主函数main的定义。请把主程序文件中的Array类的成员函数MinTwo()的定义补充完整,经修改后运行程序,得到的输出结果应为:829,20,33,12,18,66,25,1412.14注意:只允许在“ ********333********”和“ ********666********”之间填写内容,不允许修改其他任何地方的内容。 Arry. h#include <iostream>#include <cstdlib>using namespaee std;template <class Type>class Array { 数组类public:Array(Type b[], int mm): size (mm) { 构造函数if(size<2) {cout << 数组长度太小, 退出运行! ; exit(1); }a=new Type[ size];for(int i=0; i<size; i++)a[i]=b[i];}~Array() {delete [ ]a;} 析构void MinTwo (Type& x1, Type& x2)const; 由x1和x2带回数组a中最小的两个值int Length() const{ return size; }Type operator [] (int i)const { 下标运算符重载为成员函数if (i<0 || i>=size) {cout<< 下标越界! <<endl; exit (1);}return a[i];}private:Type * a;int size;};void writeToFile(const char * ); 不用考虑此语句的作用 main. cpp#include Array. h template <class Type>void Array <Type>::MinTwo (Type& x1, Type& x2) const { 补充完整函数体的内容a[0] <=a[1] (x1=a[0], x2=a[1]):(x1=a[1], x2=a[0]); ******** 333******** ******** 666********}int main () {int s1[8]={29, 20, 33, 12, 18, 66, 25, 14};Array<int> d1 (s1, 8);int i, a, b;d1.MinTwo (a, b);cout <<d1.Length() <<endl;for(i=0; i<7; i++) cout <<d1[i]<< , ; cout <<d1[7] <<endl;cout <<a<< , <<b<<endl;writeToFile ( );return 0;}