c++篇——从零开始学类【二】多个类与继承入门

c++篇——从零开始学类【二】

hello大家好,最近在做c++实验,就正好更新以下c++一大特色类的使用,主要通过实验来进行讲解,欢迎大家观看学习以及讨论~~

本次目的

我们会通过本次实验先进行对于类和对象的复习,然后接触一点点简单的继承,这一次学习不必对于继承有太深的认识,只要理解我写的就可以了。
那么我们开始吧

实现目的

1.创建学生类,包含至少学号、姓名两个私有属性。
2.创建科目类,包含ID、科目名两个私有属性。
3.创建成绩管理类,包含学生的成绩列表(可以用固定数组,也可以用Vector类模板的动态数据),存储学生的各科成绩。
4.成绩管理类至少需要有学生成绩的增加、删除、修改、查询四个成员函数,以及最高、最低、平均成绩三个成员函数。
5.要求学生至少有5个,科目至少有2个。
6.附加内容,不做要求:通过控制台界面增删改查学生、科目、成绩。

实现实验

我们看要求可以知道了几个关键点:
1.vector的使用
2需要至少三个以上的类
3.需要一个控制台【加分项】我们也带着大家做一做

开始写代码
这里我们就是一个很小型的项目了
开始吧
我们先把我们需要用到的类理一下:
1.学生类
2.科目类
3.成绩类
4.控制台
因为老师要求所以我的实现是每一排是每一位同学的一课成绩,可以实现一个同学n个科目的输入,但是每一排只能输入一个成绩
看代码

 #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int dd=0;
int q=1;
class student{
	public:
		student(string name,string hao){
			this->hao=hao;
			this->name=name;
		}
	string hao;
	string name;	
};
class subject{
	public:
		int id;
		string sname;
		subject(int id,string sname){
			this->id=id;
			this->sname=sname;
		} 
};
class score:virtual public subject,virtual public student{	
	public:
		float scores;
		score();
		score(string name,string hao,int id,string sname,float scores):student(name,hao),subject(id,sname){
			this->scores=scores;
		}
};
class way{
	public:
			vector<score>a;
			void add();//添加 
			void del();//删除 
			void gai();//修改 
			void show();//显示 
			void find();//查询 
			void maxx();//最大值 
			void minn();//最小值 
			void mainx();
			string sname;
			float scores;
			string name;
			string hao;
			int id;
			int n;
};	

可以先看看代码,我这次使用的导入方式就是指针导入,很快很方便。
这里我们用到了类的继承,但是不用多想,这里应用继承就是通过继承的特性将学生和科目类里的数据导入到成绩类中来罢了,只要记住这个格式就可以了剩下的不用多管。
因为我们的数据是存入在每个单独的类中的,通过继承可以得到这个类里的东西,就好像儿子继承父亲的遗产一个道理
剩下的就好实现了,但是这里我们先看看vector的应用,因为vector是动态数组,应用起来无论是添加删除还是查找都会一定的方便一些
我们直接看控制台的实现
vector在for循环中只需要特殊的定义就可以直接定义到相关的位置很方便,也为我们节省了不少精力

void way::add(){
	cout<<"请输入学生姓名,学号,科目id,科目,科目成绩"<<endl; 
	cin>>name>>hao>>id>>sname>>scores;
	score s(name,hao,id,sname,scores);
	a.push_back(s);
	cout<<"添加成功"<<endl; 
}
void way::find(){
	string name;
	string hao;
	cout<<"请输入学生姓名和学号"<<endl; 
	cin>>name>>hao;
	for(vector<score>::iterator i=a.begin();i!=a.end();i++){
		if((*i).name==name&&(*i).hao==hao){
			cout<<(*i).name<<" "<<(*i).hao<<" "<<(*i).id<<" "<<(*i).sname<<" "<<(*i).scores<<endl;
		} 
	}
} 
void way::del(){
	cout<<"请输入学生的学号"<<endl;
	string hao;
	cin>>hao;
	for(vector<score>::iterator i=a.begin();i!=a.end();i++)	{
		if((*i).hao==hao){
			a.erase(i);
			break;
		}
	} 
	cout<<"删除成功"<<endl;
}
void way::gai(){
	cout<<"请输入要修改的学生学号:"<<endl;
	string hao;
	cin>>hao;
	cout<<"输入要修改学科:"<<endl;
	cin>>sname;
	 for (vector<score>::iterator i=a.begin();i != a.end();i++){
	 	if((*i).hao==hao&&(*i).sname==sname){
	 		cout << "请重新输入该学生此科目成绩" << endl;
			cin>>scores;
		 }
		 (*i).scores=scores;
	 }
	 cout<<"修改成功"<<endl;
}
void way::show(){
	for (vector<score>::iterator i=a.begin();i != a.end();i++){
		cout<<(*i).name<<" "<<(*i).hao<<" "<<(*i).id<<" "<<(*i).sname<<" "<<(*i).scores<<endl;
	}
}
void way::maxx(){
	int maxn=-1;
	vector<score>::iterator e;
	for (vector<score>::iterator i=a.begin();i != a.end();i++){
		if(maxn<(*i).scores){
			maxn=(*i).scores;
			e=i;
		}
	}
	cout<<(*e).name<<" "<<(*e).hao<<" "<<(*e).id<<" "<<(*e).sname<<" "<<(*e).scores<<endl;
}
void way::minn(){
	int minx=1000;
	vector<score>::iterator e;
	for (vector<score>::iterator i=a.begin();i != a.end();i++){
		if(minx>(*i).scores){
			minx=(*i).scores;
			e=i;
		}
	}
	cout<<(*e).name<<" "<<(*e).hao<<" "<<(*e).id<<" "<<(*e).sname<<" "<<(*e).scores<<endl;
}

void way::mainx(){
	int n;
//	cout<<"请输入你要添加学生个数:"; 
//	cin>>n;
	string sname;
	float scores;
	string hao;
	string name;
//	cout<<"学生相关信息"<<endl;
//	cout<<"请输入学生姓名,学号,科目id,科目,科目成绩"<<endl;
	cout<<"目前已有学生情况如下:"<<endl; 
	score *s1;
	score *s2;
	score *s3;
	s1=new score("a","1",1,"math",90);
	s2=new score("a","1",2,"english",100);
	s3=new score("b","1",3,"c",120);
	a.push_back(*s1);
	a.push_back(*s2);
	a.push_back(*s3);
	show();
//	for(int i=0;i<n;i++){
//		cin>>name>>hao>>id>>sname>>scores;
//		score s(name,hao,id,sname,scores);
//		a.push_back(s);
//	}
	cout<<"可以选择以下功能:"<<endl;
	cout<<"1.添加     2.删除"<<endl; 
	cout<<"3.修改     4.显示"<<endl;
	cout<<"5.查询     6.最大值"<<endl;
	cout<<"7.最小值          "<<endl;
	cout<<"输入0退出"<<endl;
	int d=1;
	while(d!=0){
		cin>>d;
		switch(d){
			case 1:
				add();
				break;
			case 2:
				del();
				break;
			case 3:
				gai();
				break;
			case 4:
				show();
				break;
			case 5:
				find();
				break;
			case 6:
				maxx();
				break;
			case 7:
				minn();
				break;
		}
	} 
	if(d==0){
		q=0;
	}
}

我是因为老师的要求不需要自定义输入所以我注释掉了,如果你们需要可以自行定义注释中的内容。那么这一章也就没什么好说的了。我们直接看完整的代码吧

 #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int dd=0;
int q=1;
class student{
	public:
		student(string name,string hao){
			this->hao=hao;
			this->name=name;
		}
	string hao;
	string name;	
};
class subject{
	public:
		int id;
		string sname;
		subject(int id,string sname){
			this->id=id;
			this->sname=sname;
		} 
};
class score:virtual public subject,virtual public student{	
	public:
		float scores;
		score();
		score(string name,string hao,int id,string sname,float scores):student(name,hao),subject(id,sname){
			this->scores=scores;
		}
};
class way{
	public:
			vector<score>a;
			void add();//添加 
			void del();//删除 
			void gai();//修改 
			void show();//显示 
			void find();//查询 
			void maxx();//最大值 
			void minn();//最小值 
			void mainx();
			string sname;
			float scores;
			string name;
			string hao;
			int id;
			int n;
};	
void way::add(){
	cout<<"请输入学生姓名,学号,科目id,科目,科目成绩"<<endl; 
	cin>>name>>hao>>id>>sname>>scores;
	score s(name,hao,id,sname,scores);
	a.push_back(s);
	cout<<"添加成功"<<endl; 
}
void way::find(){
	string name;
	string hao;
	cout<<"请输入学生姓名和学号"<<endl; 
	cin>>name>>hao;
	for(vector<score>::iterator i=a.begin();i!=a.end();i++){
		if((*i).name==name&&(*i).hao==hao){
			cout<<(*i).name<<" "<<(*i).hao<<" "<<(*i).id<<" "<<(*i).sname<<" "<<(*i).scores<<endl;
		} 
	}
} 
void way::del(){
	cout<<"请输入学生的学号"<<endl;
	string hao;
	cin>>hao;
	for(vector<score>::iterator i=a.begin();i!=a.end();i++)	{
		if((*i).hao==hao){
			a.erase(i);
			break;
		}
	} 
	cout<<"删除成功"<<endl;
}
void way::gai(){
	cout<<"请输入要修改的学生学号:"<<endl;
	string hao;
	cin>>hao;
	cout<<"输入要修改学科:"<<endl;
	cin>>sname;
	 for (vector<score>::iterator i=a.begin();i != a.end();i++){
	 	if((*i).hao==hao&&(*i).sname==sname){
	 		cout << "请重新输入该学生此科目成绩" << endl;
			cin>>scores;
		 }
		 (*i).scores=scores;
	 }
	 cout<<"修改成功"<<endl;
}
void way::show(){
	for (vector<score>::iterator i=a.begin();i != a.end();i++){
		cout<<(*i).name<<" "<<(*i).hao<<" "<<(*i).id<<" "<<(*i).sname<<" "<<(*i).scores<<endl;
	}
}
void way::maxx(){
	int maxn=-1;
	vector<score>::iterator e;
	for (vector<score>::iterator i=a.begin();i != a.end();i++){
		if(maxn<(*i).scores){
			maxn=(*i).scores;
			e=i;
		}
	}
	cout<<(*e).name<<" "<<(*e).hao<<" "<<(*e).id<<" "<<(*e).sname<<" "<<(*e).scores<<endl;
}
void way::minn(){
	int minx=1000;
	vector<score>::iterator e;
	for (vector<score>::iterator i=a.begin();i != a.end();i++){
		if(minx>(*i).scores){
			minx=(*i).scores;
			e=i;
		}
	}
	cout<<(*e).name<<" "<<(*e).hao<<" "<<(*e).id<<" "<<(*e).sname<<" "<<(*e).scores<<endl;
}
void way::mainx(){
	int n;
//	cout<<"请输入你要添加学生个数:"; 
//	cin>>n;
	string sname;
	float scores;
	string hao;
	string name;
//	cout<<"学生相关信息"<<endl;
//	cout<<"请输入学生姓名,学号,科目id,科目,科目成绩"<<endl;
	cout<<"目前已有学生情况如下:"<<endl; 
	score *s1;
	score *s2;
	score *s3;
	s1=new score("a","1",1,"math",90);
	s2=new score("a","1",2,"english",100);
	s3=new score("b","1",3,"c",120);
	a.push_back(*s1);
	a.push_back(*s2);
	a.push_back(*s3);
	show();
//	for(int i=0;i<n;i++){
//		cin>>name>>hao>>id>>sname>>scores;
//		score s(name,hao,id,sname,scores);
//		a.push_back(s);
//	}
	cout<<"可以选择以下功能:"<<endl;
	cout<<"1.添加     2.删除"<<endl; 
	cout<<"3.修改     4.显示"<<endl;
	cout<<"5.查询     6.最大值"<<endl;
	cout<<"7.最小值          "<<endl;
	cout<<"输入0退出"<<endl;
	int d=1;
	while(d!=0){
		cin>>d;
		switch(d){
			case 1:
				add();
				break;
			case 2:
				del();
				break;
			case 3:
				gai();
				break;
			case 4:
				show();
				break;
			case 5:
				find();
				break;
			case 6:
				maxx();
				break;
			case 7:
				minn();
				break;
		}
	} 
	if(d==0){
		q=0;
	}
}
int main(){
	way w;
	while(q){
		w.mainx();
	}
	return 0;
} 

大家看我的代码会发现我不是很喜欢用一些很复杂的英文单词,只要我定义的意思我自己知道就可以了,所以大家不要介意
最后运行得到这样的结果就可以了
在这里插入图片描述

好啦,第二次学习就到这里了,你学废了嘛~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酱油牌酱油菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值