当前位置:首页 > 四川大学C++面向对象程序设计模拟试题1
cout << x << endl; cout << y << endl; } void Print() const { cout << z << endl; }
private: int x, y; const int z; };
int main() { Test obj1; obj1.Print(); Test obj2(1, 6, 8); obj2.Print(); const Test obj3(6, 0, 18); obj3.Print(); cout << endl; return 0; }
上面程序的输出结果为:0 0 1 6
5.阅读下面程序,写出输出结果。 #include
class MyClass {
private: static int n;
public: MyClass() { n += 1; } 18
~MyClass() { n -= 1; } static int GetNum() { return n; } };
int MyClass::n = 0;
int main() { cout << MyClass::GetNum() << endl; MyClass obj; cout << MyClass::GetNum() << endl; MyClass *p = new MyClass; cout << MyClass::GetNum() << endl; delete p; cout << MyClass::GetNum() << endl; cout << \ return 0; }
上面程序的输出结果为:0 1 2 1 end
6.阅读下面程序,写出输出结果。 #include
class A {
private: int a;
public: A() { cout << \无参构造函数\ A(int a) { cout << \含参构造函数a=\ A(const A ©): a(copy.a) { cout << \复制构造函数\ ~A() { cout << \析构函数\ };
int main() { A obj1, obj2(1), obj3(obj2); return 0; }
上面程序的输出结果为:无参构造函数 含参构造函数a=1 复制构造函数 析构函数 析构函数 析构函数
四、完成程序填题(本大题共4个小题,每小题3分,共12分)下面程序都留有空白,请将程序补充完整。
1.将如下程序补充完整。 #include
class Test {
private: int num;
public: Test(int num = 0) { [1] this->num或Integer::num = num; } 据成员num为形参num int GetNum() const { return num; } };
int main() { Test obj; cout << obj.GetNum() << endl; return 0; }
2.将如下程序补充完整。
#include
class A {
private: int a;
public: A(int m): a(m) {} void Show() const { cout << a << endl; } };
//初始化数
class B: A {
private: int b;
public: B(int m, int n = 0): [2] A(m), b(n) {} // 初始化数据成员b的值为n void Show() const { A::Show(); cout << b << endl; } };
int main() { B obj(8); obj.Show(); return 0; }
3.下列程序的输出结果为: 0 1 0
试将程序补充完整。
#include
class Point {
private: int x, y; static int count;
public: Point(int m = 0, int n = 0): x(m), y(n) { count++; } ~Point() { count--; } int GetX() const { return x; } int GetY() const { return y; } static void ShowCount() { cout << count << endl; } };
共分享92篇相关文档