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;
}
  • 测试结果

     

 

转载于:https://www.cnblogs.com/FlyerBird/p/8995968.html

1、已知一个链表中存储了若干名学生的信息,每名学生的信息包括:学号、英语成绩、数学成绩、计算机成绩。 现编写一个函数search(),要求对输入的无序学号进行排序,然后采用折半查找方法查找输入学生学号,并输出该学生各科成绩。 2、设计一个学生类(CStudent),它具有私有数据成员是:学号、姓名、数学、外语和计算机课程的成绩。要求能实现求三门课总成绩和平均成绩,并能设置和显示学生信息 (类声明和成员函数定义分离)。设计一个友元函数,按照成绩从高到低的顺序输出姓名、学号和成绩信息。 3、实现雇员管理,类Employee需存储雇员的姓名。这种信息对于所有雇员(包括Employee的派生类的雇员)是很普遍的。现在假设从雇员类Employee派生出了小时工类HourlyWorker、计件工类PieceWorker、老板类Boss和销售员类CommissionWorker。小时工每周工作40小时,超过40小时部分的报酬是平时的1.5倍;计件工是按生产的工作件数计算报酬的,每件的报酬是固定的,假设他只生成一种类型的工件,因而类PieceWorker的private数据成员是生产的工件数量和每件的报酬;老板每周有固定的薪水;销售员每周有小部分固定的基本工资加上其每周销售额的固定百分比。设计和规划该类体系,并分别产生每个基类及派生类对象,并显示该员工的工资。 4、约瑟夫生死者游戏 每30个旅客同乘一条船,因为严重超载,加上风高浪大,危险万分;因此船长告诉乘客,只有将全船一半的旅客投入海中,其余人才能幸免遇难。无奈,大家只得同意这种办法,并议定30个人围成一圈,由第一个人数起,依次报数,数到第9人,便把他投入大海中,然后再从他的下一个人数起,数到第9人,再将他扔进大海中,如此循环地进行,直到剩下15个乘客为止。问哪些位置是将是被扔下大海的位置。 5、求二叉树上结点的路径 要求在采用链式存储结构存储的二叉树上,以bt指向根结点,p指向任一给定的结点,编程实现求出从根节点到给定结点之间的路径。 6、图的操作 (1)写出将一个无向图的邻接矩阵转换成邻接表的算法 (2)设计一个算法,判断无向图G是否连通。若连通则返回1; 返回0。 7、内部排序算法的性能分析 要求:(1)对冒泡排序、直接排序、简单选择排序、快速排序、希尔排序、堆排序算法进行比较; (2)待排序表的表长不小于100,表中数据随机产生,至少用5组不同数据作比较,比较指标有:关键字参加比较次数和关键字的移动次数(关键字交换记为3次移动); (3)输出各种算法的排序结果和比较结果。 8.2、通讯录管理系统 编程实现通讯录管理系统,要求该系统能够完成通讯信息的建立、查询、插入、删除等基本功能。程序运行后至少给出下面7个菜单项的选择并分别实现其功能: 0、 通讯录的建立 1、通讯录信息输出 2、 通讯者结点信息的删除 3、通讯者结点信息的查询 4、 通讯者结点信息的插入 5、通讯录信息更改 6、 退出通讯录管理系统 设计的任务要求,通讯录中每个学生的基本信息应包括姓名、地址、电话等基本信息,采用链表存储结构。(复习c语言结构体和链表知识)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值