void class_First_one() { int count1= 0; name = " Primary enginneering training"; teacher = "Mr.Li"; classroom = "Practice training building"; class_num =1.1 ; suit_grade =1 ; count1++; //调用成员函数,学生人数加1 cout << "The current number of select this class is :" << count1 << endl; }以这个作为例子给我代码
时间: 2024-01-28 21:03:47 浏览: 70
这个例子中,计数器 count1 定义在成员函数 class_First_one() 中,每次调用该函数时,计数器加一,表示有一个学生选择了该课程。下面是代码示例:
```cpp
class MyClass {
public:
void class_First_one() {
static int count1 = 0; // 将计数器定义为静态变量,确保其生命周期与类对象相同
name = "Primary engineering training";
teacher = "Mr.Li";
classroom = "Practice training building";
class_num = 1.1;
suit_grade = 1;
count1++; // 调用成员函数,学生人数加1
cout << "The current number of select this class is: " << count1 << endl;
}
private:
string name;
string teacher;
string classroom;
float class_num;
int suit_grade;
};
```
这样,每次调用 class_First_one() 函数时,都会输出当前选择该课程的学生人数。
阅读全文