zoukankan      html  css  js  c++  java
  • 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;
    }
    • 测试结果
  • 相关阅读:
    linux-log-files/
    SSL SSH
    C++学习的书籍
    Linux IO 分析
    LINUX 常用操作
    Find Large Files in Linux
    Linux 常见操作
    Linux Performance tool
    /linux-command-line-bash-shortcut-keys/
    Sed
  • 原文地址:https://www.cnblogs.com/FlyerBird/p/8995968.html
Copyright © 2011-2022 走看看