21xrx.com
2025-03-26 05:14:51 Wednesday
文章检索 我的文章 写文章
中南大学C++期末考试题及答案
2023-06-23 08:55:32 深夜i     16     0
中南大学 C++期末考试 题目 答案 成绩

近日,中南大学计算机系进行了一次C++期末考试。考试难度适中,考查了学生们的C++编程能力、理解能力等多个方面。

以下是本次考试的题目及答案:

一、选择题(共30分)

1.以下哪个选项不是C++语言的关键字?

A. class  B. virtual  C. remove  D. const 

答案:C

2.C++中使用哪个关键字定义常量?

A. const  B. static  C. void  D. volatile

答案:A

3.C++中定义一个类需要使用哪个关键字?

A. class  B. def  C. import  D. include

答案:A

4.C++中如何定义纯虚函数?

A. virtual void func() = 0;  B. void func() = 0; 

C. virtual void func();  D. void func();

答案:A

5.C++中用于输出对象的流对象是?

A. cin  B. cout  C. cerr  D. clog

答案:B

二、编程题(共70分)

1.(20分)已知有两个整数a和b,编写C++程序,将a和b交换,并输出交换后的a和b的值。注:必须使用指针实现。

答案:

#include <iostream>
using namespace std;
void swap(int *p1, int *p2){
  int tmp = *p1;
  *p1 = *p2;
  *p2 = tmp;
}
int main(){
  int a = 10, b = 20;
  cout << "交换前:a = " << a << ", b = " << b << endl;
  
  swap(&a, &b);
  
  cout << "交换后:a = " << a << ", b = " << b << endl;
  return 0;
}

2.(25分)定义一个Rectangle类,包含长、宽两个数据成员,以及计算矩形面积和周长的成员函数。编写C++程序,创建两个Rectangle对象,计算它们的面积和周长,并输出计算结果。

答案:

#include <iostream>
using namespace std;
class Rectangle{
public:
  double length;
  double width;
  
  Rectangle(double l, double w): length(l), width(w)
  
  
  double area(){
    return length * width;
  }
  
  double perimeter(){
    return 2 * (length + width);
  }
};
int main(){
  Rectangle r1(5, 6);
  Rectangle r2(7, 8);
  cout << "r1的面积为:" << r1.area() << ", 周长为:" << r1.perimeter() << endl;
  cout << "r2的面积为:" << r2.area() << ", 周长为:" << r2.perimeter() << endl;
  return 0;
}

3.(25分)定义一个Shape类,包含计算面积和周长的纯虚函数。从Shape类派生出Rectangle类和Circle类,分别实现计算面积和周长的函数。编写C++程序,创建一个Rectangle对象和一个Circle对象,分别计算它们的面积和周长,并输出计算结果。

答案:

#include <iostream>
using namespace std;
class Shape{
public:
  virtual double area() = 0;
  virtual double perimeter() = 0;
};
class Rectangle: public Shape{
public:
  double length;
  double width;
  
  Rectangle(double l, double w): length(l), width(w)
  
  
  double area(){
    return length * width;
  }
  
  double perimeter(){
    return 2 * (length + width);
  }
};
class Circle: public Shape{
public:
  double radius;
  const double PI = 3.1415926;
  
  Circle(double r): radius(r)
  
  
  double area(){
    return PI * radius * radius;
  }
  
  double perimeter(){
    return 2 * PI * radius;
  }
};
int main(){
  Rectangle r(5, 6);
  Circle c(2);
  cout << "矩形的面积为:" << r.area() << ", 周长为:" << r.perimeter() << endl;
  cout << "圆形的面积为:" << c.area() << ", 周长为:" << c.perimeter() << endl;
  return 0;
}

总结:此次C++期末考试考查了学生们对C++语言的掌握程度,选择题主要考察了C++语言的一些基本概念,编程题则主要考察了C++语言的具体应用能力。学生们可以从考试结果中了解到自己对C++语言的掌握程度,以便更好地提升自己的学习效果。

  
  

评论区