以学生为基类编写C++程序,程序中需要体现类的多重继承、类的多级继承、虚继承、公共继承、保护继承、私有继承、子对象、类的组合、虚函数、纯虚函数等特性,须完整各类的构造函数、析构函数
时间: 2023-08-31 20:53:34 浏览: 101
基于c++类的编写
下面是一个基于学生类的C++程序示例,其中展示了类的多重继承、类的多级继承、虚继承、公共继承、保护继承、私有继承、子对象、类的组合、虚函数和纯虚函数等特性。请注意,这只是一个简单的示例,可能不符合实际需求,但可以帮助你理解这些概念。
```cpp
#include <iostream>
#include <string>
// 基类:人类
class Person {
protected:
std::string name;
public:
Person(const std::string& name) : name(name) {
std::cout << "Person constructor" << std::endl;
}
virtual ~Person() {
std::cout << "Person destructor" << std::endl;
}
virtual void introduce() {
std::cout << "Person: My name is " << name << std::endl;
}
};
// 基类:学生类
class Student : public virtual Person {
protected:
int studentId;
public:
Student(const std::string& name, int studentId) : Person(name), studentId(studentId) {
std::cout << "Student constructor" << std::endl;
}
~Student() {
std::cout << "Student destructor" << std::endl;
}
void introduce() override {
std::cout << "Student: My name is " << name << " and my student ID is " << studentId << std::endl;
}
};
// 通过虚继承,避免多重继承产生的二义性
class GradStudent : public virtual Student {
protected:
std::string researchTopic;
public:
GradStudent(const std::string& name, int studentId, const std::string& researchTopic)
: Person(name), Student(name, studentId), researchTopic(researchTopic) {
std::cout << "GradStudent constructor" << std::endl;
}
~GradStudent() {
std::cout << "GradStudent destructor" << std::endl;
}
void introduce() override {
std::cout << "GradStudent: My name is " << name << ", my student ID is " << studentId
<< ", and my research topic is " << researchTopic << std::endl;
}
};
// 子对象:Course类
class Course {
protected:
std::string courseName;
public:
Course(const std::string& courseName) : courseName(courseName) {
std::cout << "Course constructor" << std::endl;
}
~Course() {
std::cout << "Course destructor" << std::endl;
}
void displayCourse() {
std::cout << "Course: " << courseName << std::endl;
}
};
// 组合类:学生课程类
class StudentCourse {
protected:
Student student;
Course course;
public:
StudentCourse(const std::string& name, int studentId, const std::string& courseName)
: student(name, studentId), course(courseName) {
std::cout << "StudentCourse constructor" << std::endl;
}
~StudentCourse() {
std::cout << "StudentCourse destructor" << std::endl;
}
void displayStudentInfo() {
student.introduce();
}
void displayCourseInfo() {
course.displayCourse();
}
};
int main() {
Student student("John", 12345);
student.introduce();
std::cout << std::endl;
GradStudent gradStudent("Alice", 54321, "Machine Learning");
gradStudent.introduce();
std::cout << std::endl;
StudentCourse studentCourse("Bob", 67890, "Computer Science");
studentCourse.displayStudentInfo();
studentCourse.displayCourseInfo();
return 0;
}
```
这个程序展示了基于学生类的继承关系和组合关系。`Person`是人类基类,`Student`是学生类继承自`Person`,`GradStudent`是研究生类继承自`Student`,通过虚继承避免多重继承带来的二义性。
程序还展示了子对象和类的组合关系。`Course`是课程类,`StudentCourse`是学生课程类,它组合了`Student`和`Course`对象,并可以调用它们的函数。
注意:以上代码只是为了展示相关概念,并不一定符合实际的需求。在实际开发中,需要根据具体情况来设计和使用继承、组合等特性。
阅读全文