学生成绩计算(继承和多态)

学生成绩计算(继承和多态)

题目描述

定义Person类具有姓名、年龄等属性,具有输出基本信息的display函数。

选修《面向对象程序设计》课程的学生在Person类的基础上,派生出子类:免听生和非免听生。子类继承父类成员,新增其他成员、改写display函数。

非免听生具有平时成绩、考试成绩和总评成绩三个属性,总评成绩根据(平时成绩40%+考试成绩60%)计算的结果,85分(包含)以上为A,75分(包含)-85分(不包含)为B,65分(包含)-75分(不包含)为C,60分(包含)-65分(不包含)为D,60分(不包含)以下为F。

免听生只有考试成绩和总评成绩两个属性,总评成绩100%根据考试成绩对应上述等级制成绩。

定义上述类并编写主函数,输入类型符号,若输入R,根据学生基本信息、平时成绩和考试成绩,建立非免听生对象,若输入S,根据学生基本信息、考试成绩,建立免听生对象。计算学生的总评成绩,并输出。

输入

测试次数t

随后每行输入学生类型相关信息

输出

每个学生基本信息和总评成绩

示例输入

2
R cindy 18 100 100
S sandy 28 59

示例输出

cindy 18 A
sandy 28 F

#include<iostream>
using namespace std;
class Person
{
	public:
		Person(string name1,int year1)
		{
			name=name1;
			year=year1;
		}
		void display()
		{
			cout<<name<<" "<<year<<" ";
		}
	private:
		string name;
		int year;
		
};

class Student1:public Person//非免听生 
{
	public:	
		Student1(string name1,int year1,int score1,int score2):Person(name1,year1)
		{
			scoreA=score2;
			scoreB=score1;
		}
		void display()
		{
			Person::display();
			double m,n;
			m=scoreA*0.4+scoreB*0.6;
			if(m>=85)
			cout<<"A"<<endl;
			else if(m>=75&&m<85)
			cout<<"B"<<endl;
			else if(m>=65&&m<75)
			cout<<"C"<<endl;
			else if(m>=60&&m<65)
			cout<<"D"<<endl;
			else
			cout<<"F"<<endl;
		}
	private:
		int scoreA;
		int scoreB;
};

class Student2:public Person//免听生 
{
	public:
		Student2(string name1,int year1,int score1):Person(name1,year1)
		{
			scoreB=score1;
		}
		void display()
		{
			Person::display();
			double m,n;
			m=scoreB;
			if(m>=85)
			cout<<"A"<<endl;
			else if(m>=75&&m<85)
			cout<<"B"<<endl;
			else if(m>=65&&m<75)
			cout<<"C"<<endl;
			else if(m>=60&&m<65)
			cout<<"D"<<endl;
			else
			cout<<"F"<<endl;			
		}
	private:
		int scoreB;
};

int main()
{
	int t,i;
	char a,b;
	string name1;
	int year1,score1,score2;
	cin>>t;
	for(i=0;i<t;i++)
	{
		cin>>a;
		if(a=='R')
		{
			cin>>name1>>year1>>score2>>score1;
			Student1 Student3(name1,year1,score2,score1);
			Student3.display();
		}
		else if(a=='S')
		{
			cin>>name1>>year1>>score1;
			Student2 Student4(name1,year1,score1);
			Student4.display();
	}
	}
		return 0;	
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值