前言
大家好吖,欢迎来到 YY 滴C++考前速过系列 ,热烈欢迎! 本章主要内容面向接触过C++的老铁
主要内容含:
欢迎订阅 YY滴C++专栏!更多干货持续更新!以下是传送门!
目录
程序1:
设计以下三个类:显卡类、主板类、集成了显卡的集成主板类。
对这三个类的描述如下:
- 显卡类:CPU频率、显存频率。
- 主板类:北桥芯片类型、版型。
- 集成主板类:CPU频率、显存频率、北桥芯片类型、版型。
每个类都有自己的构造函数和析构函数,构造函数通过给定的初始值生成对应类的对象。同时这三个类都能显示其数据成员的值。
要求:通过类的多继承完成集成主板类的设计。
cpp
#include <iostream>
using namespace std;
// 显卡类
class GraphicsCard {
public:
int cpuFrequency;
int memoryFrequency;
GraphicsCard(int cpu, int memory) : cpuFrequency(cpu), memoryFrequency(memory) {
cout << "GraphicsCard constructed" << endl;
}
~GraphicsCard() {
cout << "GraphicsCard destructed" << endl;
}
void display() {
cout << "CPU Frequency: " << cpuFrequency << " MHz" << endl;
cout << "Memory Frequency: " << memoryFrequency << " MHz" << endl;
}
};
// 主板类
class Motherboard {
public:
string chipsetType;
string formFactor;
Motherboard(string chipset, string form) : chipsetType(chipset), formFactor(form) {
cout << "Motherboard constructed" << endl;
}
~Motherboard() {
cout << "Motherboard destructed" << endl;
}
void display() {
cout << "Chipset Type: " << chipsetType << endl;
cout << "Form Factor: " << formFactor << endl;
}
};
// 集成主板类
class IntegratedMotherboard : public GraphicsCard, public Motherboard {
public:
IntegratedMotherboard(int cpu, int memory, string chipset, string form)
: GraphicsCard(cpu, memory), Motherboard(chipset, form) {
cout << "IntegratedMotherboard constructed" << endl;
}
~IntegratedMotherboard() {
cout << "IntegratedMotherboard destructed" << endl;
}
void display() {
GraphicsCard::display();
Motherboard::display();
}
};
int main() {
IntegratedMotherboard mb(3000, 1600, "Intel", "ATX");
mb.display();
return 0;
}
程序2:
程序2.开发一个简单的大学人员管理程序。该程序可以管理大学的一些基本人员信息:学生(student)和教师(teacher)。
- 首先设计一个虚基类person,通过该类描述人员的基本信息:姓名(name)、年龄(age)和性别(sex)。
- 然后使用该类派生出学生类student和教师类teacher,在其中添加各自的特性。例如,在student类中添加专业(specialty),在teacher类中添加院系(department)等。
- 还有部分教师在工作的同时,在职攻读学位,因此具有教师和学生双重身份,由student类和teacher类再次派生出stuTeacher类。
为每个类定义一个输出函数print,输出该类的相关信息。
cpp
#include <iostream>
#include <string>
using namespace std;
// 虚基类 person
class Person {
public:
string name;
int age;
char sex;
Person(string n, int a, char s) : name(n), age(a), sex(s) {}
virtual void print() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Sex: " << sex << endl;
}
};
// 学生类 student
class Student : public Person {
public:
string specialty;
Student(string n, int a, char s, string sp) : Person(n, a, s), specialty(sp) {}
void print() override {
Person::print();
cout << "Specialty: " << specialty << endl;
}
};
// 教师类 teacher
class Teacher : public Person {
public:
string department;
Teacher(string n, int a, char s, string dep) : Person(n, a, s), department(dep) {}
void print() override {
Person::print();
cout << "Department: " << department << endl;
}
};
// 具有教师和学生双重身份的类 stuTeacher
class StuTeacher : public Student, public Teacher {
public:
StuTeacher(string n, int a, char s, string sp, string dep) : Student(n, a, s, sp), Teacher(n, a, s, dep) {}
void print() override {
Student::print();
Teacher::print();
}
};
int main() {
Student s("Alice", 20, 'F', "Computer Science");
Teacher t("Bob", 35, 'M', "Mathematics");
StuTeacher st("Carol", 25, 'F', "Physics", "Engineering");
cout << "Student Information:" << endl;
s.print();
cout << "\nTeacher Information:" << endl;
t.print();
cout << "\nStudent-Teacher Information:" << endl;
st.print();
return 0;
}
程序3:
程序3.某学校教学管理人员分工如下:教师负责讲授各类课程;班主任负责管理班级;兼职班主任既讲授课程又担任班级的管理工作。对他们的描述如下:
- 教师:员工编号、姓名、职称、基本工资、每个月的授课量。 班主任:员工编号、姓名、职称、基本工资、管理的班级数量。
- 兼职班主任:员工编号、姓名、职称、基本工资、每个月的授课量、管理的班级数量。
所有教学管理人员都有输出自己基本信息的功能,包括:员工编号、姓名、职称、每个月的授课量、管理的班级数量。
所有的教学管理人员都有计算自己工资的功能。其中, 教师的工资=基本工资+每个月的授课量*20;- 班主任的工资=基本工资+管理的班级数量300; 兼职班主任的工资=基本工资+每个月的授课量 20+管理班级的数量*300。
要求:现在学校要开发教学管理软件来处理学校日常工作,请用多继承方式定义描述以上3类教学管理人员的类。
cpp
#include <iostream>
#include <string>
using namespace std;
// 基类:教学管理人员
class TeachingStaff {
public:
int employeeID;
string name;
string title;
double baseSalary;
TeachingStaff(int id, string n, string t, double salary)
: employeeID(id), name(n), title(t), baseSalary(salary) {}
virtual void printInfo() {
cout << "Employee ID: " << employeeID << endl;
cout << "Name: " << name << endl;
cout << "Title: " << title << endl;
}
virtual double calculateSalary() = 0;
};
// 教师类
class Teacher : public TeachingStaff {
public:
int teachingHoursPerMonth;
Teacher(int id, string n, string t, double salary, int hours)
: TeachingStaff(id, n, t, salary), teachingHoursPerMonth(hours) {}
void printInfo() override {
TeachingStaff::printInfo();
cout << "Teaching Hours Per Month: " << teachingHoursPerMonth << endl;
}
double calculateSalary() override {
return baseSalary + teachingHoursPerMonth * 20;
}
};
// 班主任类
class Headmaster : public TeachingStaff {
public:
int numberOfClassesManaged;
Headmaster(int id, string n, string t, double salary, int classes)
: TeachingStaff(id, n, t, salary), numberOfClassesManaged(classes) {}
void printInfo() override {
TeachingStaff::printInfo();
cout << "Number of Classes Managed: " << numberOfClassesManaged << endl;
}
double calculateSalary() override {
return baseSalary + numberOfClassesManaged * 300;
}
};
// 兼职班主任类
class PartTimeHeadmaster : public Teacher, public Headmaster {
public:
PartTimeHeadmaster(int id, string n, string t, double salary, int hours, int classes)
: Teacher(id, n, t, salary, hours), Headmaster(id, n, t, salary, classes) {}
void printInfo() override {
Teacher::printInfo();
Headmaster::printInfo();
}
double calculateSalary() override {
return baseSalary + teachingHoursPerMonth * 20 + numberOfClassesManaged * 300;
}
};
int main() {
Teacher t(1001, "John", "Professor", 5000, 40);
Headmaster h(2001, "Alice", "Headmaster", 6000, 3);
PartTimeHeadmaster p(3001, "Bob", "Associate Professor", 4000, 30, 2);
cout << "Teacher Information:" << endl;
t.printInfo();
cout << "Salary: $" << t.calculateSalary() << endl;
cout << "\nHeadmaster Information:" << endl;
h.printInfo();
cout << "Salary: $" << h.calculateSalary() << endl;
cout << "\nPart-Time Headmaster Information:" << endl;
p.printInfo();
cout << "Salary: $" << p.calculateSalary() << endl;
return 0;
}