1 | (3分)构造函数可以对静态数据成员进行初始化; |
| 对 |
√ | 错 |
2 | (3分)重载函数必须遵循可区分原则,比如函数参数个数、参数类型及函数返回值类型不同等。 |
| 对 |
√ | 错 |
3 | (3分)虚函数只能是成员函数 |
√ | 对 |
| 错 |
4 | (3分)关于派生类析构函数的执行顺序描述正确的是 |
| 先执行子对象所在类的析构函数(如果有子对象的话) |
√ | 先执行派生类析构函数的函数体 |
| 先执行直接基类中的析构函数 |
| 先执行多个基类中最左基类的析构函数。 |
5 | (3分)在类的定义中,类的( ) 描述了该类的对象的行为特征. |
| 类名 |
√ | 方法 |
| 所属的名字空间 |
| 私有域 |
6 | (3分)在 C++语言中,对函数参数默认值描述正确的是: |
| 函数参数的默认值只能设定一个 |
| 一个函数的参数若有多个,则参数默认值的设定可以不连续 |
| 函数参数必须设定默认值 |
√ | 在设定了参数的默认值后,该参数后面定义的所有参数都必须设定默认值述。 |
7 | (3分)假定一个类的构造函数为 A(int aa,int bb){a=aa++;b=a*(bb+aa++);},则执行A X(4,5);语句后,X.b的值为( ) |
| 40 |
8 | (3分)执行语句 k=30;j=k++;后 k和j 和的值分别为:【答案用英文逗号隔开】 |
| 31,30 |
9 | (5分)下面关于友元函数的描述中正确的是: |
| 如果类A 是类B 的友元,那么类 B 也是类A 的友元 |
√ | 如果函数 fun()被说明为类 A 的友元,那么在 fun()中可以访问类 A 的私有成员 |
√ | 友元关系不能被继承 |
√ | 友元破坏了类的封装性 |
10 | (5分)以下关于静态数据成员的描述正确的是()。 |
| 静态数据成员可以在类体中进行初始化 |
| 静态数据成员不可以被类的实例调用 |
| 静态数据成员不能受 protected 控制符的作用 |
√ | 静态数据成员可以直接用类名调用 |
11 | (5分)以下通过虚函数能实现多态性的有: |
| 基类和派生类具有同名虚函数,而它们的参数个数不同 |
√ | 用基类的指针或基类的引用来访问虚函数 |
| 基类和派生类具有同名虚函数,而它们的参数至少有一个类型不同 |
√ | 基类和派生类具有函数原型完全相同的同名虚函数,而函数体内的执行代码不同 |
12 | (5分)关于纯虚函数和抽象类的描述中正确的有: |
√ | 纯虚函数是一种特殊的虚函数,它没有具体的实现部分 |
√ | 抽象类是指具有纯虚函数的类 |
| 一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类逻辑运算符 |
√ | 抽象类只能作为基类使用,其纯虚函数的实现部分由派生类给出 |
类改错B
试题描述
改正下面程序的错误,请不要修改类名、类中数据成员名称、成员函数名称。
class Rectangle
{
private:
int x1;
int y1;
int x2;
int y2;
public:
void Set2Point(int a, int b, int c, int d)
{
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
int area()
{
return abs(a - c) * abs(b - d);
}
}
int main()
{
int a, b, c, d;
cout << "请输入长方形的左上角的坐标:" << endl;
cin >> a >> b;
cout << "请输入长方形的右下角的坐标:" << endl;
cin >> c >> d;
Rectangle r1;
r1.Set2Point();
int a = r1.area();
cout << "长方形的面积为:" << a;
return 0;
}
注意:1.请务必提交完整的程序代码,不要修改代码框架。2.请不要修改试题描述中的所有标识符,注意大小写敏感。
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<cmath>
using namespace std;
class Rectangle
{
private:
int x1;
int y1;
int x2;
int y2;
public:
void Set2Point(int a, int b, int c, int d)
{
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
int area()
{
return abs(x1 - x2) * abs(y1 - y2);
}
};
int main()
{
int a, b, c, d;
cout << "请输入长方形的左上角的坐标:" << endl;
cin >> a >> b;
cout << "请输入长方形的右下角的坐标:" << endl;
cin >> c >> d;
Rectangle r1;
r1.Set2Point(a,b,c,d);
a = r1.area();
cout << "长方形的面积为:" << a;
return 0;
}
继承基础填空A
试题描述
请在空白处补充代码,实现要求的功能。
#include < ① >
#include <string>
#include <iomanip>
using namespace ② ;
③ Person //定义基类
{
public:
④ () //构造函数
{
}
void setName(string n)
{
name = n;
}
string getName() //获得姓名函数
{
return ⑤ ;
}
⑥ setColor(string c) //设置肤色函数
{
color = ⑦ ;
}
void setWeight(float w) //设置重量函数
{
⑧ = w;
}
float getWeight() //获得重量函数
{
return weight;
}
private:
string color; //肤色
int age; //年龄
float weight; //重量
string name; //姓名
};
class Student : ⑨ Person //定义派生类,公有继承方式
{
public:
⑩ () //构造函数
{
}
};
int main()
{
string n, c;
float w;
cin >> n >> c >> w;
Student a;
a.setName(n);
a.setColor(c);
a.setWeight(w);
cout << a.getName() << endl;
cout << setiosflags(ios::fixed) << setprecision(2) << a.getWeight() << endl;
return 0;
}
注意:1.请务必提交完整的程序代码,不要修改代码框架。2.请不要修改试题描述中的所有标识符,注意大小写敏感。
输入
输入两个字符串和一个浮点数,以空格隔开。
输出
依据题意输出两行,分别是姓名和体重。体重输出要求小数点后必须保留2位有效数字(四舍五入),不足补零。
输入示例
唐老鸭 白色 14.5
输出示例
唐老鸭
14.50
数据范围
输入字符串和float范围的浮点数
#include <iostream >
#include <string>
#include <iomanip>
using namespace std ;
class Person //定义基类
{
public:
Person () //构造函数
{
}
void setName(string n)
{
name = n;
}
string getName() //获得姓名函数
{
return name ;
}
void setColor(string c) //设置肤色函数
{
color = c ;
}
void setWeight(float w) //设置重量函数
{
weight = w;
}
float getWeight() //获得重量函数
{
return weight;
}
private:
string color; //肤色
int age; //年龄
float weight; //重量
string name; //姓名
};
class Student : public Person //定义派生类,公有继承方式
{
public:
Student () //构造函数
{
}
};
int main()
{
string n, c;
float w;
cin >> n >> c >> w;
Student a;
a.setName(n);
a.setColor(c);
a.setWeight(w);
cout << a.getName() << endl;
cout << setiosflags(ios::fixed) << setprecision(2) << a.getWeight() << endl;
return 0;
}
基类之水陆两用汽车
试题描述
交通工具Vehicle类、船Boat类、汽车Car类和水陆两用汽车AmphibianCar类的继承关系如下图所示,继承关系均为公有继承。
为了简化问题,四个类都没有数据成员,但是都要求定义默认构造函数(分别输出“创建交通工具Vehicle”、“创建船Boat”、“创建汽车Car”和“创建水陆两用汽车AmphibianCar”),定义析构函数(分别输出“销毁交通工具Vehicle”、“销毁船Boat”、“销毁汽车Car”和“销毁水陆两用汽车AmphibianCar”)。
int main()
{
int x;
cin >> x;
if (x == 1)
{
AmphibianCar a;
cout << endl;
}
else if (x == 2)
{
Car a;
cout << endl;
}
else if (x == 3)
{
Boat a;
cout << endl;
}
else if (x == 4)
{
Vehicle a;
cout << endl;
}
return 0;
}
注意:1.请务必提交完整的程序代码,不要修改代码框架。2.请不要修改试题描述中的所有标识符,注意大小写敏感。
输入
输入一个整数。
输出
依据题意输出若干行。
输入示例
1
输出示例
创建交通工具Vehicle
创建汽车Car
创建船Boat
创建水陆两用汽车AmphibianCar
销毁水陆两用汽车AmphibianCar
销毁船Boat
销毁汽车Car
销毁交通工具Vehicle
数据范围
输入int范围的整数
#include <iostream >
#include <string>
#include <iomanip>
using namespace std ;
class Vehicle
{
public:
Vehicle()
{
cout<<"创建交通工具Vehicle"<<endl;
}
~Vehicle()
{
cout<<"销毁交通工具Vehicle"<<endl;
}
};
class Boat:virtual public Vehicle
{
public:
Boat():Vehicle()
{
cout<<"创建船Boat"<<endl;
}
~Boat()
{
cout<<"销毁船Boat"<<endl;
}
};
class Car:virtual public Vehicle
{
public:
Car():Vehicle()
{
cout<<"创建汽车Car"<<endl;
}
~Car()
{
cout<<"销毁汽车Car"<<endl;
}
};
class AmphibianCar:public Car,public Boat
{
public:
AmphibianCar():Vehicle(),Car(),Boat()
{
cout<<"创建水陆两用汽车AmphibianCar"<<endl;
}
~ AmphibianCar()
{
cout<<"销毁水陆两用汽车AmphibianCar"<<endl;
}
};
int main()
{
int x;
cin >> x;
if (x == 1)
{
AmphibianCar a;
cout << endl;
}
else if (x == 2)
{
Car a;
cout << endl;
}
else if (x == 3)
{
Boat a;
cout << endl;
}
else if (x == 4)
{
Vehicle a;
cout << endl;
}
return 0;
}
虚函数之球体与圆柱体
试题描述
使用虚函数编写程序求球体和圆柱体的体积及表面积。
由于球体和圆柱体都可以看做由圆继承而来,所以可以定义圆类作为基类。在Circle类中定义一个数据成员radius半径和两个虚函数area()和volume()。由Circle类公有派生Sphere类和Column类。在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的表面积和体积。
球的表面积计算公式 = 4 * 3.14 * r * r。
球的体积计算公式 = (4.0 / 3) * 3.14 * r * r * r。
void func(Circle &c)
{
cout << setiosflags(ios::fixed) << setprecision(2) << c.area() << " " << c.volume() << endl;
}
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
Circle cir(a);
Sphere sph(b);
Column col(c, d);
func(cir);
func(sph);
func(col);
return 0;
}
注意:1.请务必提交完整的程序代码,不要修改代码框架。2.请不要修改试题描述中的所有标识符,注意大小写敏感。
输入
输入四个非负整数,相邻两项以空格隔开。
输出
输出三行。每行均包含面积和体积,两项以空格隔开。输出要求小数点后必须保留2位有效数字(四舍五入),不足补零。
输入示例
1 1 1 1
输出示例
3.14 0.00
12.56 4.19
12.56 3.14
数据范围
输入int范围的非负整数
#include <iostream >
#include <string>
#include <iomanip>
using namespace std ;
class Circle //基类Circle
{
protected:
int radius; //圆半径
public:
Circle(int a)
{
radius=a;
}
virtual float area()
{
return 3.14 * radius * radius;
}
virtual float volume()
{
return 0.00;
}
};
class Sphere:public Circle
{
protected:
int radius;
public:
Sphere(int a):Circle(radius)
{
radius=a; }
float volume()
{
return (4.0 / 3) * 3.14 * radius * radius * radius;
}
float area()
{
return 4 * 3.14 * radius * radius;
}
};
class Column:public Circle
{
private:
int h;
int radius;
public:
Column(int a,int b):Circle(radius){
radius=a;
h=b;
}
float volume()
{
return 3.14*radius*radius*h;
}
float area()
{
return 3.14*radius*radius*2+2*3.14*radius*h;
}
};
void func(Circle &c)
{
cout << setiosflags(ios::fixed) << setprecision(2) << c.area() << " " << c.volume() << endl;
}
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
Circle cir(a);
Sphere sph(b);
Column col(c, d);
func(cir);
func(sph);
func(col);
return 0;
}