int main() {
double d; int i ; d = 99.99; i = 88; Fun(d,i);
cout << \return 0;
}
上面程序的输出结果为:
6.阅读下面程序,写出输出结果。
#include
class Point { public: private: };
int main(void) {
Point oPoint1;
const Point oPoint2(3, 4); cout << oPoint1.GetX() << endl; oPoint1.SetX(1);
cout << oPoint1.GetX() << endl; oPoint1.SetY(2);
cout << oPoint1.GetY() << endl; cout << oPoint2.GetX() << endl; cout << oPoint2.GetY() << endl; return 0; int m_x; int m_y;
// X坐标 // X坐标
Point (int x = 0, int y = 0): m_x(x), m_y(y){ } int GetX() const { return m_x; } int GetY() const { return m_y; } void SetX(int x) { m_x = x; } void SetY(int y) { m_y = y; }
}
上面程序的输出结果为:
7. 阅读下面程序,写出输出结果。
#include
Sample::Sample() { }
void Sample::Display() { cout << \
Sample::~Sample() { cout << \ int main() { }
Sample a; a.Display(); return 0;
cout << \ i=0; int i; Sample(); void Display( ); ~Sample(); public:
上面程序的输出结果为:
8.阅读下面程序,写出输出结果。
#include
int a,b;
A() { a = b = 0; } public:
};
A(int aa, int bb) { }
a = aa; b = bb;
cout << a << ' ' << b << endl;
int main() { }
A x, y(2,3); return 0;
上面程序的输出结果为:
五、 程序改错题(本大题共3小题,共3处错, 每改一错2分,共6分)指出下面程序中错误, 说明错误原因, 并加以改正。
1.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。
#include
class A public:
//1 //2 //3 //4 //5 //6 //7 //8 //9 //10 //11 //12 //13 //14 //15 //16 //17 //18
A(int i = 0, int j): mi(i), mj(j) {} void Show()
int mi, mj;
{ cout << mi << ' ' << mj << endl; }
private:
int main()
A obj(12,16); obj.Show(); return 0;
2.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。
#include
class A
//1 //2 //3 //4 //5
public: }; { }
//6 //7 //8 //9 //10 //11 //12 //13 //14 //15 //16 //17 //18 //19
A(int a):m_a = a{} void Show()
{ cout << m_a << endl; } int m_a;
private:
int main()
A obj(8); obj.Show(); return 0;
3.下面程序中类的定义中有一处错误,请指出出错的行,说明错误原因,并加以改正。
#include
class Test public:
//1 //2 //3 //4 //5 //6 //7 //8 //9 //10 //11 //12 //13 //14 //15 //16 //17 //18 //19 //20 //21
Test(int iVar = 0):m_iArr(iVar) void ~Test()
{ cout << \构造函数:\ { cout << \析造函数:\
private:
int m_iArr;
int main()
Test obj1, obj2(8); cout << endl; return 0;
六、 编程题(本大题共2小题,每小题10分,共20分)
1.设计一个类DateInfo,要求其满足下述要求:
(1)要求有一个无参的构造函数,其初始的年、月、日分别为:2010,6,8。 (2)要求有一个带参数的构造函数,其参数分别对应年、月、日。 (3)要求用一个成员函数实现日期的设置。 (4)要求用一个成员函数实现输出日期。 要求用一个成员函数实现日期的获取。
2.定义一个复数类Complex, 二个数据成员为double型r, i 为private属性。定义代二个参数的构造函数和一个Show( ) 函数用以输出r, i的值, 另外作为成员函数重载的运算苻”+”的功能是将此类二个对象的数据成员r和i对应相加。这些成员函数的属性均为public. 请用C++编写此程序, 并编写测试程序进行测试。