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;
}
- 测试结果
(。・∀・)ノ
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架