选择题:
下列程序代码,正确的输出结果是( C)
int a=0xfe;
char b=062;
cout<<a<<","<<b<<endl;
A.
0xfe,062
B.
fe,62
C.
254,2
D.
254,62
2-2
分数 4
作者 刘骥
单位 重庆大学
下列代码能正确执行的是( A)
A.
char a='C';
const char& p=a;
B.
char a='C';
char* const p;
p=&a;
C.
char a='C';
const char& p;
p=a;
D.
char a='C';
char& p;
p=a;
2-3
分数 4
作者 刘骥
单位 重庆大学
new和delete运算符正确的使用是( D)
A.
int* p=new int[10];
delete p;
B.
int* p=new int(10);
delete []p;
C.
vector<int>* v=new vector<int>[10];
delete []v;
D.
int* a=new int[]{1,2,3,4};
delete []a;
2-4
分数 4
作者 刘骥
单位 重庆大学
下列哪个代码能够实现x和y值的交换(B )
A.
void fun(int a,int b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(&x, &y);
cout << x << ","<<y << endl;
return 0;
}
B.
void fun(int* a,int* b) {
int x = *a;
*a = *b;
*b = x;
}
int main() {
int x = 1, y = 2;
fun(&x,&y);
cout << x << ","<<y << endl;
return 0;
}
C.
void fun(int& a,int& b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(&x, &y);
cout << x << ","<<y << endl;
return 0;
}
D.
void fun(const int&a,const int&b) {
int x = a;
a = b;
b = x;
}
int main() {
int x = 1, y = 2;
fun(x, y);
cout << x << ","<<y << endl;
return 0;
}
2-5
分数 4
作者 刘骥
单位 重庆大学
下面有一个结构体,对结构体的错误使用是( B)
struct Book{
string name;
double price;
}
A.
Book b{"C++",20};
B.
Book b;
b->name="C++";
b->price=20;
C.
Book *b=new Book[2];
b[0].name="C++";
b[0].price=20;
D.
Book *p=new Book();
p->name="C++";
p->price=20;
2-6
分数 4
作者 葛亮
单位 重庆大学
下列种类的函数中,哪一种不是类的成员函数?(C)
A.
构造函数
B.
析构函数
C.
友元函数
D.
拷贝构造函数
2-7
分数 4
作者 葛亮
单位 重庆大学
父类Base和子类Derive的定义如下。请问在子类中,继承的父类成员f,x,y的访问控制权限分别是:D
class Base
{
public:
void f();
protected:
int x;
private:
int y;
};
class Derive : protected Base
{
};
A.
public, protected, private
B.
public, public, public
C.
private, private, private
D.
protected, protected, private
2-8
分数 4
作者 葛亮
单位 重庆大学
当变量x的输入值是9时,下面程序的运行结果是:C
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x;
cin>>x;
try
{
cout<<"begin"<<endl;
if(x>100)
{
cout<<"1"<<endl;
throw string("too big.");
}
else
{
cout<<"x="<<x<<endl;
}
cout<<"2"<<endl;
}
catch(string e)
{
cout<<"3:"<<e<<endl;
}
cout<<"end"<<endl;
return 0;
}
A.
begin
1
2
end
B.
begin
1
3: too big.
2
end
C.
begin
x=9
2
end
D.
begin
1
3: too big.
end
2-9
分数 4
作者 刘骥
单位 重庆大学
下列哪个代码不会调用对象的拷贝构造函数(C )
A.
MyClass a;
MyClass b=a;
B.
MyClass a;
MyClass b(a);
C.
MyClass a;
MyClass& b=a;
D.
void f(MyClass obj)
{
....
}
MyClass a;
f(a);
2-10
分数 4
作者 刘骥
单位 重庆大学
根据下列类模板声明,正确初始化对象的方式是( A)
template<typename T1, typename T2>
class MyClass{
private:
T1 x;
T2 y;
public:
MyClass(T1 _x, T2 _y):x(_x),y(_y){}
};
A.
MyClass<int,char> a(10,'a');
B.
MyClass a(10,'a');
C.
MyClass<int,char> a;
D.
MyClass a;
函数题:
6-1 类的定义(教师类Teacher):本题要求定义一个教师类Teacher,数据成员包含姓名name和年龄age,类的声明见给出的代码,请给出类的完整实现,并通过测试程序。
Teacher::Teacher(string n,int a){
name =n;
age=a;
}
string Teacher::getName() const{
return name;
}
int Teacher::getAge() const{
return age;
}
void Teacher::setName(string n)
{
name =n;
}
void Teacher::setAge(int a)
{
age=a;
}
6-2 加、不等和输入输出的运算符重载(2维向量Vec2)
本题要求定义一个二维向量类Vec2,类的声明见给出的代码,请给出类的完整实现,并通过测试程序。类的声明包括如下内容:
数据成员,向量的第一维u和第二维v;
u和v的访问函数;
构造函数;
加号、减号运算符重载(遵守二维向量的运算规则);
==和!=运算符重载(遵守二维向量的运算规则)
输入、输出运算符重载。
Vec2::Vec2(double u1,double v1){
u=u1;
v=v1;
}
Vec2 Vec2::operator+(const Vec2&b){
return Vec2(u+b.u,v+b.v);
}
bool operator!=(const Vec2&a,const Vec2&b)
{
return a.u!=b.u&&a.v!=b.v;
}
ostream&operator<<(ostream&os,const Vec2&c){
os<<"u="<<c.u<<","<<"v="<<c.v;
return os;
}
istream&operator>>(istream&is,Vec2&c){
is>>c.u>>c.v;
}
6-3 继承和多态(水果和香蕉)
请设计水果和香蕉类,并通过测试程序,具体要求如下:
水果(Fruit)是基类,成员包含:
保护成员变量重量(weight,int类型)
公有构造函数
公有析构函数
公有函数display
香蕉(Banana)从水果类公有继承,成员包含:
私有成员变量产地(origin,string类型)
公有构造函数
公有析构函数
公有函数display
class Fruit{
public:
int weight;
Fruit(int a){
weight =a;
cout<<"Fruit Constructor"<<endl;
};
virtual ~Fruit(){
cout<<"Fruit Destructor"<<endl;
};
virtual void display()
{
cout<<"weight="<<weight<<endl;
};
};
class Banana: public Fruit{
private:
string origin="";
public:
Banana(string a,int b):origin(a),Fruit(b){
cout<<"Banana Constructor"<<endl;
};
~Banana(){
cout<<"Banana Destructor"<<endl;
};
void display()
{
cout<<"origin="<<origin<<",";
cout<<"weight="<<weight<<endl;
};
};
编程题:
7-1 约数
当整数a除以整数b(b≠0),除得的商正好是整数而没有余数时,称b是a的约数。给你两个正整数A和B, 如果B是A的约数,请打印输出A+B, 否则打印输出A−B。
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x,y;
cin>>x>>y;
if(x/y==round(x/y)){
cout<<x+y<<endl;
}
else{
cout<<x-y<<endl;
}
return 0;
}
7-2 逆序整数:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n,m,x,y;
int a[100];
int b[100];
cin>>n>>m>>x>>y;
int max=x;
int min=x;
int maxy=y;
int miny=y;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]>max){
max =a[i];
}
if(a[i]<min){
min =a[i];
}
}
for(int j=0;j<m;j++){
cin>>b[j];
if(b[j]>maxy){
maxy =b[j];
}
if(b[j]<miny){
miny =b[j];
}
}
int cnt=0;
for(int k=0;k<m;k++){
if(b[k]<max&&b[k]>min){
cnt++;
}
}
if(y<max&&y>min){
cnt++;
}
if(miny<min&&maxy>max){
cnt++;
}
if(cnt==0){
cout<<x<<" and "<<y<<": impossible"<<endl;
}
else{
cout<<x<<" and "<<y<<": possible"<<endl;
}
return 0;
}
7-3 一维世界的疫情传播:
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int main()
{
int n;
string x,y;
cin>>n;
cin>>x;
cin>>y;
int cnt=0;
for(int k=0;k<=n-1;k++){
if(x[k]==y[n-k-1]){
cnt++;
}
}
if(cnt==n){
cout<<x<<" and "<<y<<" are reverse."<<endl;
}
else{
cout<<x<<" and "<<y<<" are not reverse."<<endl;
}
return 0;
}