定义一个学生类studentC++
时间: 2023-11-14 10:23:05 浏览: 114
c++定义一个学生类(属性有学号,姓名,成绩),要求输入学生的学号,姓名及三科的成绩,以输入内容来设置学生对象的属性。最后输出学生的学号,姓名及三科的平均成绩。
以下是一个简单的学生类的 C++ 实现:
```cpp
class Student {
private:
string name;
int age;
string department;
double gpa;
public:
Student(string name, int age, string department, double gpa) {
this->name = name;
this->age = age;
this->department = department;
this->gpa = gpa;
}
string getName() {
return name;
}
int getAge() {
return age;
}
string getDepartment() {
return department;
}
double getGPA() {
return gpa;
}
void setName(string name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
void setDepartment(string department) {
this->department = department;
}
void setGPA(double gpa) {
this->gpa = gpa;
}
void print() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << department << endl;
cout << "GPA: " << gpa << endl;
}
};
```
这个类包含了四个私有成员变量:姓名,年龄,专业和平均学分绩点,以及一个构造函数和一些公有成员函数来获取和设置这些变量,以及打印学生信息。
阅读全文