设计一个学生类使用c++语言

 

代码如下:如有帮助,请采纳一下,谢谢。

#include <iostream>
using namespace std;

class Student
{
private:
	int mId;
	int mScoreMath;
	int mScoreEng;
	int mScoreC;
public:
	Student(){}
	Student(int id){mId = id;}
	Student(int id,int m,int e,int c){mId = id;mScoreMath=m;mScoreEng=e;mScoreC=c;}
	void setScore(int m,int e,int c){mScoreMath = m;mScoreEng = e;mScoreC = c;}
	float average()	{return (mScoreC + mScoreEng + mScoreMath)/3.0;}
	int sum(){return mScoreC + mScoreEng + mScoreMath;}
	void print(){cout << "学号:" << mId << "  数学:" << mScoreMath<< "  英语:" << mScoreEng << "  C语言:" << mScoreC <<endl;}
};

int main()
{
	Student st(1001);
	st.setScore(88,98,89);
	cout << "平均分:" << st.average() << endl;
	cout << "总分:" << st.sum() << endl;
	st.print();
	return 0;

}

 

#include <iostream>

#include <string>

 

class Student {

  private:

    // 学号

    std::string ID;

    // 数学

    int math;

    // 英语

    int english;

    // c

    int c;

 

  public:

    Student();

    Student(const std::string ID, const int math, const int english, const int c);

 

    std::string getID() const;

    void        setID(const std::string ID);

    int         getMath() const;

    void        setMath(const int math);

    int         getEnglish() const;

    void        setEnglish(const int english);

    int         getC() const;

    void        setC(const int c);

 

    // 设置每门课分数

    void setScore(const int math, const int english, const int c);

    // 及时平均分数

    int average() const;

    // 计算总分数

    int sum() const;

 

    void print() const;

 

    ~Student();

};

 

Student::Student() {

}

Student::Student(const std::string ID, const int math, const int english, const int c) {

    this->ID      = ID;

    this->math    = math;

    this->english = english;

    this->c       = c;

}

std::string Student::getID() const {

    return this->ID;

}

void Student::setID(const std::string ID) {

    this->ID = ID;

}

int Student::getMath() const {

    return this->math;

}

void Student::setMath(const int math) {

    this->math = math;

}

int Student::getEnglish() const {

    return this->english;

}

void Student::setEnglish(const int english) {

    this->english = english;

}

int Student::getC() const {

    return this->c;

}

void Student::setC(const int c) {

    this->c = c;

}

void Student::setScore(const int math, const int english, const int c) {

    this->math    = math;

    this->english = english;

    this->c       = c;

}

int Student::average() const {

    return this->sum() / 3;

}

int Student::sum() const {

    return this->math + this->english + this->c;

}

void Student::print() const {

    std::cout << "学号: " << this->ID << ", "

              << "英语: " << this->english << ", "

              << "c: " << this->c << ", "

              << "数学: " << this->math << ", "

              << "总分: " << this->sum() << ", "

              << "平均分: " << this->average()

              << std::endl;

}

Student::~Student() {

}

 

int main() {

 

    Student* student = new Student("00003", 60, 70, 80);

 

    student->print();

 

    delete student;

    student = nullptr;

 

    return 0;

}