• 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;
    }
    • 测试结果
  • 相关阅读:
    C语言中的排序算法--冒泡排序,选择排序,希尔排序
    常见算法:C语言求最小公倍数和最大公约数三种算法
    提高软件测试效率的方法探讨
    面试官询问的刁钻问题——以及如何巧妙地应付它们
    软件测试面试--如何测试网页的登录页面
    如何衡量测试效率,如何提高测试效率!
    利用交叉测试提升软件测试效率
    交叉测试的必要性和遇到的问题
    敏捷测试
    HttpWatch工具简介及使用技巧
  • 原文地址:https://www.cnblogs.com/FlyerBird/p/8995968.html
Copyright © 2020-2023  润新知