#include <iostream>using namespace std;//定義學生類Studentclass Student { int no; //學號 char name[10]; //姓名 double score; //成績 static int totalNumber; //學生總人數 static double totalScore; //總成績 static double lowestScore; //最低成績 static double highestScore; //最高成績public: Student(int no_, char* name_, double score_); //建構式 static void Output(); //輸出相關資訊函式 void StudentInformation(); //輸出學生基本資訊函式};int Student::totalNumber = 0; //靜態資料成員初始化double Student::totalScore = 0;double Student::highestScore = 0.;double Student::lowestScore = 100.;int main(){ Student stu1(1, "張三", 97.5); stu1.StudentInformation(); Student stu2(2, "李四", 83.); stu2.StudentInformation(); Student stu3(3, "王五", 93.); stu3.StudentInformation(); Student stu4(4, "郭六", 62.5); stu4.StudentInformation(); Student stu5(5, "任七", 77.); stu5.StudentInformation(); Student::Output(); return 0;}Student::Student(int no_, char* name_, double score_){ no = no_; strcpy(name, name_); score = score_; totalNumber++; totalScore += score; if (score > highestScore) highestScore = score; if (score < lowestScore) lowestScore = score;}void Student::StudentInformation(){ cout << "學號:" << no << " " << "姓名:" << name << " " << "成績:" << score << endl;}void Student::Output(){ cout << "學生總數:" << totalNumber << " " << "總成績:" << totalScore << " " << endl; cout << "平均成績:" << totalScore / totalNumber << " " << "最高成績" << highestScore << " " << "最低成績" << lowestScore << " " << endl;}
uj5u.com熱心網友回復:
char name[10]; 變成const char name【10】就行了 ,如果還有錯,提示你哪個你就加上const轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/117281.html
標籤:C++ 語言
上一篇:gets函式
下一篇:求大佬幫忙看看是什么情況
