案例分析:
/** * 统计某班选修课考试的平均成绩 */ #include <iostream> #include <string> using namespace std; class Student { private: string name; // 姓名 string gender; // 性别 float score; // 分数 string subject; // 课程 static int total_counts; // 总人数 static float chinese_scores; // 语文分数 static int chinese_counts; // 语文课人数 static float math_scores; // 数学分数 static int math_counts; // 数学课人数 public: Student(string name, string gender, float score, string subject); ~Student(); static float aveScores(string subject); void printStudentInfo(); void printAveScores(); }; int Student::total_counts = 0; float Student::chinese_scores = 0; int Student::chinese_counts = 0; float Student::math_scores = 0; int Student::math_counts = 0; Student::Student(string name, string gender, float score, string subject) { this->name = name; this->gender = gender; this->score = score; this->subject = subject; if(subject == "chinese" || subject == "语文") { chinese_scores += score; chinese_counts++; } else if(subject == "math" || subject == "数学") { math_scores += score; math_counts++; } else { cout << "this is no the subect:" << subject << endl; } total_counts += (chinese_counts + math_counts); } Student::~Student() { total_counts--; chinese_counts--; math_counts--; } float Student::aveScores(string subject) { float ave_score = 0; if(chinese_counts > 0 && subject == "chinese" || subject == "语文") { ave_score = (chinese_scores / chinese_counts); //cout << subject << "t" << chinese_counts << "t" << chinese_scores << endl; } else if(math_counts > 0 && subject == "math" || subject == "数学") { ave_score = (math_scores / math_counts); //cout << subject << "t" <<math_counts << "t" << math_scores << endl; } return ave_score; } void Student::printStudentInfo() { cout << name << "t" << gender << "t" << score << "t" << subject << endl; } void Student::printAveScores() { cout <<subject << " average score: " << aveScores(subject) << endl; } int main(int argc, char const *argv[]) { const int SIZE = 5; Student stu[SIZE] = { Student("10001", "male", 92, "语文"), Student("10002", "male", 91, "数学"), Student("10003", "male", 91, "数学"), Student("10004", "male", 93, "语文"), Student("10005", "male", 92, "语文"), }; for(int i = 0; i < SIZE; i++) { stu[i].printStudentInfo(); } stu[0].printAveScores(); stu[1].printAveScores(); cout << "语文" << " average score: " << Student::aveScores("语文") << endl; cout << "数学" << " average score: " << Student::aveScores("数学") << endl; return 0; } 静态成员的案例分析
下面看下c语言中的static关键字的作用
在我们日常使用过程中,static通常有两个作用:
1、修饰变量
静态全局变量:全局变量前加static修饰,该变量就成为了静态全局变量。我们知道,全部变量在整个工程都可以被访问(一个文件中定义,其它文件使用的时候添加extern关键字声明 ),而在添加了static关键字之后,这个变量就只能在本文件内被访问了。因此,在这里,static的作用就是限定作用域。
静态局部变量:局不变量添加了static修饰之后,该变量就成为了静态局部变量。我们知道局部变量在离开了被定义的函数后,就会被销毁,而当使用static修饰之后,它的作用域就一直到整个程序结束。因此,在这里static的作用就是限定生命周期。
2、修饰函数
修饰函数则该函数成为静态函数,函数的作用域仅限于本文件,而不能被其它文件调用。
总结
以上所述是小编给大家介绍的c++中的 static 关键字及作用,希望对大家有所帮助!
—-想了解详解c++中的 static 关键字及作用分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/483801.html