1. 程式人生 > >C++建立學生類練習

C++建立學生類練習

/*作業,定義一個學生類*/
/*資料成員:學號、姓名、數學、英語、計算機三科成績
*成員函式:求總成績、求三科平均成績、輸出學生資訊
*新增一個生日類  2018.4.2
*/

#include <iostream>
#include <string>
using namespace std;

class Data
{
public:
    Data();
    Data(int ye, int mon, int da);
    Data(Data &da);
    void inf();
private:
    int year;
    int month;
    int
day; }; class Student {//define a class called "Student" public: Student(int num, string na, int ma, int en, int cs, Data tp); //constructors Student(Student &stu); //Copy constructors ~Student(); int sum();//the sum grade int ave();//calculate the average grade
void show();//show the details of the student private: Data birthday; int number; string name; int math; int eng; int com; }; Data::Data() { year = 1998; month = 8; day = 3; } Data::Data(int ye = 0, int mon = 0, int da = 0) { year = ye; month = mon; day = da; } Data::Data(Data &da) { cout
<< endl << "Warnning:This Copy constructors.!!!" << endl; year = da.year; month = da.month; day = da.day; } void Data::inf() { cout << "Birthday:" << year << "/" << month << "/" << day << endl; } //the realization of class Student::Student(int num, string na, int ma, int en, int cs, Data tp) :birthday(tp) { number = num; name = na; math = ma; eng = en; com = cs; } Student::~Student() { } Student::Student(Student &stu) :birthday(stu.birthday) { cout << endl << "Warnning:This Copy constructors.!!!" << endl; number = stu.number; name = stu.name; math = stu.math; eng = stu.eng; com = stu.com; } int Student::sum() { return math + eng + com; } int Student::ave() { return (math + eng + com) / 3; } void Student::show() { cout << "Number:" << number << endl; cout << "Name:" << name << endl; birthday.inf(); cout << "Math score:" << math << endl; cout << "English score:" << eng << endl; cout << "Computer score:" << com << endl; cout << "Sum score:" << sum() << endl; cout << "Average score:" << ave() << endl; } //the main int main() { Data tmp(2012, 12, 02); Student noob(001, "!#%$#^$%^", 90, 89, 88, tmp);//Initialization //output noob.show(); Student newbie(noob); //Copy constructors newbie.show(); return 0; }
  • 測試結果
    這裡寫圖片描述