gogoWebsite

C++ Student Class

Updated to 9 hours ago

Student Classes - Static Data Members and Static Member Functions

Question description

Define a class Student that requires the use of static data members or static member functions to calculate the total score and average scores of the entire class's Introduction to Computer course.
Static data member: static int total;
Static member function: static void Average(){}

Enter a description

Student Name Course Results

Output description

Total and average scores

Enter sample
Zhang 82
Li 79
Wang 93
Liu 66
Xia 90
Output sample
410
82
Program code
#include <iostream>
#include <cstring>

using namespace std;

class Student {
	private:
		double m_score; // Student score
		char m_name[64];

		// Static member variables that count the number of students
		static int m_count;

		// Static member variables for stating the total score of students
		static double sum_score;

	public:
		Student() {
		}
		
		Student(char *name ,double score) {
			// Create a student
			m_score = score;

			m_count++; // Add up the number of students created
			sum_score += score;  //Accumulate the scores of the created student objects
			
			strcpy(m_name, name);
		}

		static double Average() {   // Provide a static method to access the average score
			return sum_score / m_count;
		}
		
		static double getSumScore() {   // Provide a static method to access the total score
			return sum_score;
		}
				
		~Student() {
			m_count--;   // Each object is destructed, the number is minus one
			sum_score -= m_score; // Deconstruct an object and subtract the student scores corresponding to the object
			
		}
};


int Student::m_count = 0;  // Initialize static variables

double Student::sum_score = 0.0; // Initialize static variables

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;
}

Student's grades - Friendly function

Question description

Based on the first question, design a friendly function to compare the grades of two students.

Enter a description

Student name and score

Output description

Results of scores (> or < or ==)

Enter sample
zhang 92
li 89
Output sample
>
Program code
#include <iostream>
#include <>

using namespace std;
 
class Student{
	private:
	    int m_score;   // Fraction
	    char * m_name; // Name
	    
	public:
	    Student(const char * n, int score);//Constructor
	    friend char Compare(const Student stu1, const Student stu2);
	    ~Student();    
	    Student();
};
 
Student::Student(const char * name, int score){//Constructor
    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){  // Compare the grades of two students
    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);  // Create an object
    
    cin >> name >> score;
    Student *stu2 = new Student(name, score);  // Create an object
    
    cout << Compare(*stu1, *stu2) << endl;  // Output result
    
    delete stu1;
    delete stu2;
    
    return 0;
}