资源描述
C++面向对象程序设计模拟试题四
一、单项选择题(本大题共10小题,每小题2分,共20分)
1. 下列关于C++函数的说明中,正确的是()。
A)内联函数就是定义在另一个函数体内部的函数B)函数体的
最后一条语句必须是return语句
C)调用一个函数之前,如果还没有定义这个函数,必须先声明其原 型
D)编译器会根据函数的返回值类型和参数表来区分函数的不同重载 形式
2. 假定MyCIass为一个类,那么下列的函数说明中,()为该
类的无参构造函数。_
A ) void MyClass(); B ) MyClass(int n); C ) MyClass( ); D ) MyClass();
3. 下列叙述中,错误的是()。
A)派生类可以使用private派生 B)对基类成员的访问必须
是无二义性的
C)基类成员的访问能力在派生类中维持不变D)赋值兼容规则也
适用于多继承的组合
4. 当一个类的某个函数被说明为virtual时,该函数在该类及其所
有派生类中()。
A.都是虚函数B.只有被重新说明为vittual时才是虚函数C.
都不是虚函数D.上面都不正确
5. 派生类的构造函数的成员初始化表中,不能包含()。
A)基类的构造函数B)派生类中子对象的初始化C)基类中子对象的 初始化D)派生类中一般数据成员的初始化
6. 下列是重载加法运算符的函数原型声明,其中错误的是()。
A ) MyClass operator+(double,double); B ) MyClass operator+(double,MyClass);
C ) MyClass operator+(MyClass,double); D ) MyClass operator+(MyClass,MyClass);
7. 派生类中的成员不能直接访问基类中的()成员。A ) public
B ) private C ) virtual D ) protected
8. 实现运行时的多态性要使用()A )重载函数B)析
构函数 C)构造函数 D)虚函数
9. 如果在类MyClass的外面使用函数调用MyClass::f();则函数f() 是类 MyClass 的( )。
A.静态成员函数B.非静态成员函数C.友元函数
D.前面都不正确
10. 由于常对象不能被更新,因此()。
A)通过常对象只能调用它的常成员函数B)通过常对象只能调用
静态成员函数
C)常对象的成员都是常成员D)通过常对象可以调用任何不改变对 象值的成员函数
二、填空题(本大题共5小题,每小题2分,共10分
1. 对于派生类的构造函数,在定义对象时构造函数的执行顺序为:
先执行调用的构造函数,再执行调用子对象类的构造函 数,最后执行派生类的构造函数体中的内容。
2. 声明类模板应使用关键字()。
3. 重载的关系运算符和逻辑运算符的返回类型应当是
()。
4. 在面向对象方法中,类的实例称为()。
5. 在类的对象被释放时,()函数会被自动调用。
三、完成程序填题(本大题共3个小题,每小题3分,共9分)
1. 请完成下面的程序 #include <iostream> using namespace std;
//测试静态成员类 class Test (private:
int a;
//预处理命令
//使用标准命名空间std
//数据成员
public:
Test(int x = 0):[1](}// 构造函数
void Show()( cout
成员之值
};
int main(void)
(Test obj(168);
obj.Show();
return 0;
}
2. 请完成下面的程序
#include <iostream> using namespace std;
<< "a:" << a << endl; } // 显示数据
// 主函数 main(void)
// 定义对象
// 显示数据成员之值
//返回值0,返回操作系统
// 预处理命令
// 使用标准命名空间
std
//数据成员
//构造函数
//类型转换函数
// 主函数 main(void)
//定义整数对象
//显示整数
//返回值0,返回
//预处理命令
//使用标准命名空间
//实部
//虚部
//整数类
class Integer
(private:
int a;
public:
Integer(int x = 0)( a = x; }
[2]() ( return a; }
(将类类型转换为整型)
};
int main(void)
( Integer a = 6;
cout << a << endl;
return 0;
操作系统
}
3. 请完成下面的程序
#include <iostream> using namespace std;
std
/复数类
class Complex
(private:
double realPart;
double imagePart;
public:
Complex(double real = 0, double image = 0): realPart(real), imagePart(image)( } // 构造函数
double GetRealPart() const( return realPart; }// 返
回实部
double GetImagePart() const( return imagePart; }// 返
回虚部
Complex operator+(const Complex &a) const // 重载加法 运算符+
( return Complex([3]);// 返回和
}};
int main(void)// 主函数 main(void)
(Complex a(1, 2), b(2, 6), c;// 定义复数对
象
c = a + b;//复数加法运算
cout << "a=" << a.GetRealPart() << " + " << a.GetImagePart() << "i" << endl; // 显示 a
cout << "b=" << b.GetRealPart() << " + " << b.GetImagePart() << "i" << endl; // 显示 b
cout << "c=" << c.GetRealPart() << " + " << c.GetImagePart() << "i" << endl; // 显示 c
return 0;//返回值0,返回
操作系统 }
四、程序分析题(本大题共4小题,每小题5分,共20分)给出下 面各程序的输出结果。
1. 阅读下面程序,写出输出结果。
#include <iostream>// 预处理命令
using namespace std;//使用标准命名空间std
class A (public:
virtual void Show() const
(cout << "Class A" << endl; } };
class B: public A
(public:
void Show() const
(cout << "Class B" << endl; } };
void Show(const A & obj)
(obj.Show(); } int main(void)
( A a; B b;
Show(a); Show(b);
A *p;
p = &a; p->Show();
p = &b; p->Show();
B *q;
q = &b; q->Show();
return 0;//返回值0,返回操作系统
}上面程序的输出结果为:
2. 阅读下面程序,写出输出结果。
#include <iostream>// 预处理命令
using namespace std;//使用标准命名空间std
template <class ElemType>
void Show(ElemType a[], int n)
( for (int i = 0; i < n; i++)
( cout << a[i] << " "; }
}
int main(void)
( int a[] = {1, 6, 9};
Show(a, sizeof(a) / sizeof(int));
Show(a, 2);
cout << endl;
return 0;//返回值0,返回操作系统
}上面程序的输出结果为:
3. 阅读下面程序,写出输出结果。
//
//
预处理命令
使用标准命名空间std
#include <iostream> using namespace std;
class MyClass
{public:
MyClass(){ count++; }
MyClass(){ count--; }
static int GetCount(){ return count; } private:
static int count;
};
int MyClass::count = 0;//初始化静态数据成员
int main(void)
{ MyClass objl;
cout << MyClass::GetCount() << endl;
MyClass obj2;
cout << MyClass::GetCount() << endl;
MyClass obj3;
cout << obj1.GetCount() << endl;
MyClass *p = new MyClass;
cout << MyClass::GetCount() << endl;
delete p;
cout << MyClass::GetCount() << endl;
return 0;//返回值0,返回操作系
统
}上面程序的输出结果为:
4. 阅读下面程序,写出输出结果。
#include <iostream>// 预处理命令
using namespace std;//使用标准命名空间std
class A (public:
A() ( cout << "A()" << endl; }
A() ( cout << " A()" << endl; }
virtual void f() ( cout << "A::f()" << endl; }
};
class B: public A
(public:
B() ( cout << "B()" << endl; }
B() ( cout << " B()" << endl; }
void f() ( cout << "B::f()" << endl; }
};
int main(void)
( B obj;
A *p = &obj;
p->f();
return 0;//返回值0,返回操作系
统
}上面程序的输出结果为:
六、编程题(本大题26分)
编写程序,定义抽象基类Shape(形状),由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)和Square (正方形),用函数函数 ShowArea()分别显示各种图形的面积,最后还要显示所有图形的总面
积。
C++面向对象程序设计模拟试题四参考答案
一、单项选择题 1. C 2.C 3.C 4.A 5.C6.A 7. B 8 . D
9. A 10. A
二、填空题1.参考答案:基类2.参考答案:template3.参考答案:
bool或布尔型4.参考答案:对象5.参考答案:析构函数
三、完成程序填题(本大题共3个小题,每小题3分,共9分)下面 程序都留有空白,请将程序补充完整,
1. 参考答案:[1]a(x)2 .参考答案:[2]operator int
3. 参考答案:[3] realPart + a.realPart, imagePart + a.imagePart 或 this->realPart + a.realPart, this->imagePart + a.imagePart
四、程序分析题(本大题共4小题, 面各程序的输出结果。
1. 参考答案:Class A
Class B
Class A
Class B
Class B
2. 参考答案:1 6 9 1 6
3. 参考答案:1
2
3
4
3
4. 参考答案:A()
B()
B::f()
~B()
~A()
六、编程题(本大题26分)
参考程序:
#include <iostream>
using namespace std;
每小题5分,共20分)给出下
//预处理命令
//使用标准命名空间std
//定义常量PI
const double PI = 3.1415926;
class Shape
(public:
Shape() ( }//构造函数
virtual Shape() ( }// 析构函数
virtual ShowArea() = 0;// 显示面积
static double totalArea;// 总面积
static void ShowTotalArea() ( cout << "总面积:"<< totalArea << endl; }
};
class Circle: public Shape
(private:
double radius;// 半径
public:
Circle(double r): radius(r) ( totalArea += PI * r * r; }
// 构造函数
~Circle() ( }// 析构函数
virtual ShowArea () ( cout << ”圆面积:"<< PI * radius * radius << endl; };// 显示面积
};
class Rectangle: public Shape
(private:
double length;// 长
double width;// 宽
public:
Rectangle(double l, double w):length(l),
width(w)( totalArea += l * w; } // 构造函数
~Rectangle() ( }// 析构函数
virtual ShowArea() ( cout << ”矩形面积:"<< length * width << endl; }; //显示面积 };
class Square: public Shape
(private:
double length;// 边长
public:
Square(double l): length(l)( totalArea += l * l; }
// 构造函数
~Square() ( }// 析构函数
virtual ShowArea () ( cout << ”正方形面积:"<< length *
length << endl; };// 显示面积
};
double Shape::totalArea = 0;
totalArea
int main(void)
( Circle c(1);
c.ShowArea();
Rectangle r(1, 2);
r.ShowArea();
Square z(3);
ShowArea();
Shape::ShowTotalArea();
return 0;
系统
// 初始化
//定义圆对象
// 显示面积
//定义矩形对象
// 显示面积
//定义正方形对象
// 显示面积
//显示总面积
//返回值0,返回操作
}
展开阅读全文