学生类Student,属性有姓名、年龄、C++分数、高等数学分数等,请编写代码实现输出成绩优秀的学生的个数,成绩优秀指的是C++分数和高等数学分数都大于等于90分。
时间: 2023-05-31 15:01:49 浏览: 37
class Student:
def __init__(self, name, age, c_score, math_score):
self.name = name
self.age = age
self.c_score = c_score
self.math_score = math_score
def is_excellent(self):
return self.c_score >= 90 and self.math_score >= 90
students = [Student('Tom', 18, 95, 92),
Student('Jerry', 19, 85, 94),
Student('Alice', 20, 92, 88),
Student('Bob', 21, 90, 90)]
excellent_students_count = 0
for student in students:
if student.is_excellent():
excellent_students_count += 1
print('成绩优秀的学生个数为:', excellent_students_count)
相关问题
使用面向对象编程思想,设计并实现以下类并测试 学生类Student,属性有姓名、年龄、C++分数、高等数学分数等,请编写代码实现输出成绩优秀的学生的个数,成绩优秀指的是C++分数和高等数学分数都大于等于90分。
class Student:
def __init__(self, name, age, c_score, math_score):
self.name = name
self.age = age
self.c_score = c_score
self.math_score = math_score
def is_excellent(self):
return self.c_score >= 90 and self.math_score >= 90
students = []
students.append(Student('张三', 20, 92, 88))
students.append(Student('李四', 21, 90, 90))
students.append(Student('王五', 19, 88, 90))
students.append(Student('赵六', 22, 90, 92))
excellent_count = 0
for student in students:
if student.is_excellent():
excellent_count += 1
print('{}是一个成绩优秀的学生。'.format(student.name))
print('成绩优秀的学生个数为{}。'.format(excellent_count))
定义一个学生类student,包含姓名和分数两个属性,定义静态成员函数static void输出
好的,以下是我作为 CSDN 开发的 C知道 的回答:
可以这样定义一个学生类 student:
```c++
class student {
public:
student(string name, int score) : name_(name), score_(score) {}
static void output(student s) {
cout << "姓名:" << s.name_ << ",分数:" << s.score_ << endl;
}
private:
string name_;
int score_;
};
```
其中,构造函数用于初始化姓名和分数两个属性,静态成员函数 output 用于输出学生的姓名和分数。使用时,可以这样调用:
```c++
student s("小明", 90);
student::output(s);
```
这样就可以输出学生的姓名和分数了。
希望我的回答能够帮到你,如果你还有其它问题,可以继续问我哦!