Student(string id, int score) { this->id = id; this->score = score; }
时间: 2024-06-01 08:11:39 浏览: 67
This code defines a constructor function for a class called "Student". The constructor takes two parameters, a string called "id" and an integer called "score". Within the constructor, the "id" and "score" parameters are assigned to the corresponding member variables of the class using the "this" pointer. This allows the member variables to be accessed and modified within the scope of the constructor function.
相关问题
class People { public: string name;//姓名 int age;//年龄 string id;//学号 People() {} People(string id, string name, int age ) { setId(id); setName(name); setAge(age); } //set void setId(string id) { this->id = id; } void setName(string name) { this->name = name; } void setAge(int age) { if (age >= 0 && age <= 100) { this->age = age; } } //get string getId() { return id; } string getName() { return name; } int getAge() { return age; } }; class Student: public People { public: double score;//成绩 Student() { } Student(string id, string name, int age, double score) :People(id, name, age) { setScore(score); } double getScore() { return score; } //set void setScore(double score) { if (score >= 0 && score <= 100) { this->score = score; } } };
这段代码定义了两个类:People 和 Student。People 类包含了姓名、年龄和学号等属性以及相应的 set 和 get 方法。Student 类继承了 People 类,并添加了一个成绩属性,以及相应的 set 和 get 方法。
在 People 类中,setId、setName 和 setAge 方法用于设置学号、姓名和年龄,getId、getName 和 getAge 方法用于获取学号、姓名和年龄的值。
在 Student 类中,setScore 和 getScore 方法用于设置和获取成绩属性的值。
需要注意的是,成员变量和方法的访问权限使用了 public 关键字,表示可以被其他类访问。同时,类的构造函数也被定义了,用于初始化类的属性。
#include <iostream> using namespace std;//命名空间 class classmanger { public: string student_name; void setname(string student_name); string getname(); string subject; void setsubject(string subject); string getsubject(); int student_score; void setscore(int student_score); int getscore(); void print_data(); private: }; void classmanger::setname() { this->student_name=student_name; } string classmanger::getname() { return this->student_name; } void classmanger::setsubject(string subject) { this->subject=subject; } string classmanger::getsubject() { return this->subject; } void classmanger::setscore(int student_score) { this->student_score=student_score; } int classmanger::getscore() { return student_score; } void classmanger::print_data() { cout<<student_name<<endl; cout<<subject<<endl; cout<<student_score<<endl; } int main() { cout << "Hello World!" << endl; classmanger student1; student1.setname("lingyu"); student1.setsubject("math"); student1.setscore(10); student1.getname(); student1.getsubject(); student1.getscore(); student1.print_data(); return 0; }
这段代码有一些问题,主要是在classmanger类的成员函数中出现了参数未定义的情况。下面是修改后的代码:
```
#include <iostream>
using namespace std;
class classmanger {
public:
string student_name;
void setname(string name);
string getname();
string subject;
void setsubject(string subject);
string getsubject();
int student_score;
void setscore(int score);
int getscore();
void print_data();
private:
};
void classmanger::setname(string name) {
this->student_name = name;
}
string classmanger::getname() {
return this->student_name;
}
void classmanger::setsubject(string subject) {
this->subject = subject;
}
string classmanger::getsubject() {
return this->subject;
}
void classmanger::setscore(int score) {
this->student_score = score;
}
int classmanger::getscore() {
return this->student_score;
}
void classmanger::print_data() {
cout << "Name: " << student_name << endl;
cout << "Subject: " << subject << endl;
cout << "Score: " << student_score << endl;
}
int main() {
cout << "Hello World!" << endl;
classmanger student1;
student1.setname("lingyu");
student1.setsubject("math");
student1.setscore(10);
student1.print_data();
return 0;
}
```
这个程序定义了一个classmanger类,包含学生的名字、科目和分数。setname、setsubject和setscore函数用来设置这些属性,而getname、getsubject和getscore则用来获取这些属性。print_data用于输出学生的信息。在main函数中,我们创建了一个学生对象,设置了他的名字、科目和分数,并输出了他的信息。
阅读全文