深圳大学-计算机信息管理课程实验 C++ 自考模拟题
第1题:公司日期
需要私有成员变量:年,月,日
需要无参构造函数,并输出“default called”
需要有参构造函数,需要年,月,日三个入参,并输出“param called”
需要print函数,输出格式为 yyyy-mm-dd
1.1,使用无参构造函数
输出格式如下:
default called
2024-1-1
1.2,通过输入流,获取输入的年月日,并使用有参构造函数。
输入格式为:2024 7 1
输出格式如下:
param called
2024-7-1
#include <iostream>
using namespace std;class CompanyDate {
private:int year, month, day;
public:CompanyDate() {// cout << "default called" << endl;year = 2024; month = 1; day = 1;}CompanyDate(int y, int m, int d) {// cout << "param called" << endl;year = y; month = m; day = d;}void print() {cout << year << "-" << month << "-" << day << endl;}
};
// ============================ 主函数测试 ============================
int main() {// 1.1 无参构造CompanyDate d1;d1.print();// 1.2 有参构造int y, m, day;cin >> y >> m >> day;CompanyDate d2(y, m, day);d2.print();return 0;
}
第2题:肥猫
需要私有成员变量名字,体重
需要有有参构造函数,参数包括名字,体重
2.1,需要输入n组数据,每组数据包括名字和体重
输入格式:
4
name1 100
name2 111
name3 101
name4 108
2.2,根据体重排序,由低到高输出名字
输出格式:
name1 name3 name4 name2
#include <iostream> // 引入输入输出库,提供 cin、cout 等流操作。
#include <vector> // 引入动态数组容器 std::vector。
#include <string> // 引入 std::string 字符串类。
#include <algorithm> // 引入常用算法(如 sort)
using namespace std; //使用 std 命名空间,后续可以直接写 cout、string、vector 等而不用加 std:: 前缀(在小程序/作业中常用,工程中建议避免全局 using)。class FatCat { // 定义一个类 FatCat,用来表示“肥猫”对象。
private: // 私有成员变量 name(名字)、weight(体重)。private 表示外部无法直接访问这两个成员,封装性。string name;int weight;
public: // public::公有成员,对外可访问。FatCat(string n, int w) : name(n), weight(w) {} // 有参构造函数,使用成员初始化列表将参数 n、w 分别赋给成员 name、weight。比在函数体内赋值效率更高且对某些类型必要。string getName() const { return name; } // 返回名字的访问器(getter),加 const 表示该函数不会修改对象成员。int getWeight() const { return weight; } // 返回体重的访问器(getter),加 const 表示该函数不会修改对象成员。
};int main() {int n; // 定义整型变量 n,用来存放接下来要读入的记录数。cin >> n;vector<FatCat> cats; // 定义一个 FatCat 类型的动态数组(向量)cats,用来保存所有输入的猫对象。for (int i = 0; i < n; i++) { // 循环读入数据:string name;int weight;cin >> name >> weight; // 从输入读取名字和体重(例如 "name1 100")。// emplace_back 与 push_back 的区别是:emplace_back 直接使用构造函数就地构造对象,避免了临时对象拷贝/移动,性能更好。cats.emplace_back(name, weight); // 将一个新的 FatCat 对象就地构造并添加到 vector 尾部。}// 这是一个 lambda(匿名函数),作为比较函数传入 sort。// 参数 a、b 是两个 const FatCat 引用,返回 true 表示 a 应排在 b 前面。// 这里用体重升序(从低到高):如果 a.getWeight() < b.getWeight() 为真,a 放前面。sort(cats.begin(), cats.end(), [](const FatCat &a, const FatCat &b) {return a.getWeight() < b.getWeight(); // 根据体重从低到高排序});// 输出名字,空格分割for (int i = 0; i < n; i++) {cout << cats[i].getName();if (i != n - 1) cout << " ";}cout << cats << endl;cout << endl;return 0;
}
第3题:圆包含点
设计一个类名叫Point,拥有私有变量x,y
设计一个类名Circle,继承Point类,并有自己的成员变量:r;
需要有成员函数,计算圆的面积(圆的面积公式:面积=3.14 * r * r )
需要有print函数,输出圆的坐标信息,半径。
需要有包含函数,判断一个点是否在圆内(通过圆心到点的距离,判断两点间距离是否大于半径。不在圆内,则输出“no”,在圆内,则输出“yes”)
3.1,通过输入流输入圆的信息x、y、r,计算圆的面积
输入信息为:0,0,1
输出格式为:(0,0),1;area:3.14
3.2,输入一个点的坐标信息,判断该点是否在圆内。
(两点间距离公式:double squareroot = sqrt((x1-x2)*(x1-x2)+(y1-y2)(y1-y2))
输入格式:2 3
输出格式:no
#include <iostream>
#include <cmath>using namespace std;class Point {
protected:double x, y;
public:Point(double x = 0, double y = 0) : x(x), y(y) {}
};class Circle : public Point {
private:double r;
public:Circle(double x = 0, double y = 0, double r = 0) : Point(x, y), r(r) {}double area() {return 3.14 * r * r;}void print() {cout << "(" << x << "," << y << ")," << r << ";area:" << area() << endl;}void contains(double px, double py) {double dist = sqrt((x - px) * (x - px) + (y - py) * (y - py));if (dist > r) cout << "no" << endl;else cout << "yes" << endl;}
};// ============================ 主函数测试 ============================
int main() {// 圆面积double cx, cy, cr;cin >> cx >> cy >> cr;Circle c(cx, cy, cr);c.print();// 判断点是否在圆内double px, py;cin >> px >> py;c.contains(px, py);
}