日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 学生类

發布時間:2025/5/22 c/c++ 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 学生类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

學生類——靜態數據成員和靜態成員函數

題目描述

定義一個類Student,要求使用靜態數據成員或靜態成員函數計算全班學生的《計算機導論》課程的總成績和平均成績。
靜態數據成員:static int total;
靜態成員函數:static void Average(){}

輸入描述

學生姓名 課程成績

輸出描述

總成績和平均成績

輸入樣例

Zhang 82 Li 79 Wang 93 Liu 66 Xia 90

輸出樣例

410 82

程序代碼

#include <iostream> #include <cstring>using namespace std;class Student {private:double m_score; // 學生分數char m_name[64];// 統計學生個數的靜態成員變量static int m_count;// 統計學生總分數的靜態成員變量static double sum_score;public:Student() {}Student(char *name ,double score) {// 創建一個學生m_score = score;m_count++; // 對創建的學生對象的人數進行累加 sum_score += score; // 對創建的學生對象的分數進行累加 strcpy(m_name, name);}static double Average() { // 提供一個訪問平均分的靜態方法 return sum_score / m_count;}static double getSumScore() { // 提供一個訪問總分的靜態方法 return sum_score;}~Student() {m_count--; // 每析構一個對象,數量減去一 sum_score -= m_score; // 析構一個對象,減去對象對應的學生分數 } };int Student::m_count = 0; // 對靜態變量進行初始化 double Student::sum_score = 0.0; // 對靜態變量進行初始化 int main(){Student *s1 = new Student("Zhang", 82);Student *s2 = new Student("Li", 79);Student *s3 = new Student("Wang", 93);Student *s4 = new Student("Liu", 66);Student *s5 = new Student("Xia", 90);cout << Student::getSumScore() << endl;cout << Student::Average() << endl;delete s5; delete s4;delete s3;delete s2;delete s1;return 0; }

學生成績高低——友元函數

題目描述

在第一題的基礎上,設計一個友元函數,比較某兩個學生成績的高低。

輸入描述

學生姓名和分數

輸出描述

分數高低的結果(>或<或==)

輸入樣例

zhang 92 li 89

輸出樣例

>

程序代碼

#include <iostream> #include <string.h>using namespace std;class Student{private:int m_score; // 分數 char * m_name; // 姓名 public:Student(const char * n, int score);//構造函數friend char Compare(const Student stu1, const Student stu2);~Student(); Student(); };Student::Student(const char * name, int score){//構造函數int len = strlen(name);m_name = new char[len+1];strcpy(m_name, name);m_score = score; }Student::~Student() { if (m_name != NULL) {m_name = NULL;}}char Compare(const Student stu1, const Student stu2){ // 比較某兩個學生成績的高低 if(stu1.m_score > stu2.m_score) {return('>');} else if (stu1.m_score < stu2.m_score){return('<');} else {return('=');} }int main(){char name[64]; int score;cin >> name >> score;Student *stu1 = new Student(name, score); // 創建對象 cin >> name >> score;Student *stu2 = new Student(name, score); // 創建對象 cout << Compare(*stu1, *stu2) << endl; // 輸出結果 delete stu1;delete stu2;return 0; }

總結

以上是生活随笔為你收集整理的C++ 学生类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。