C++-实验6 多态性与模板

1. (50分) C+±S6-1-类模板

题目描述
编写一个使用类模板对数组进行排序、查找和显示所有元素值的程序,数组中元素个数3≤n≤15
说明:设计一个类模板
template
class Array,用于对T类型的数组进行排序、查找、显示所有元素,构造函数有两个参数:传递数组首地址和数组元素个数。
主函数中实例化Array产生模板类Array和Array,输入两个数组的长度,再依次输入各元素的值,调用相应的成员函数完成:输出数组的原序列、从键盘输入需要查找的元素值,完成查找(如有相同的元素,则返回找到的第一个数位置)、对数组进行由小到大排序,输出排序后的结果。

输入描述
输入6行数据
int型数组元素的个数
int型数组元素的值
double型数组元素的个数
double型数组元素的值
要查找的int型数据值
要查找的double数据值

输出描述
原int型序列
查找数据所在的位置
排序后的int型序列
原double型序列
查找数据所在的位置
排序后的double型序列

输入样例
9
6 3 8 1 9 4 7 5 2
6
2.3 6.1 1.5 8.4 6.7 3.8
9
8.5

输出样例
array1:
原序列:6 3 8 1 9 4 7 5 2
9在array1中的位置:5
排序后:1 2 3 4 5 6 7 8 9
array2:
原序列:2.3 6.1 1.5 8.4 6.7 3.8
8.5在array2中不存在
排序后:1.5 2.3 3.8 6.1 6.7 8.4(英文冒号)

用户代码

 #include <iostream>
#include<cmath>
 
using namespace std;
 

template <class T>
class Array
{public:
	
	Array(T *s,int ni):s(s),n(ni) {
	}    
	void sort()
	{
		int i,j;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				if(s[i]<s[j])
					swap(s[i],s[j]);
					 
			}
		}
	}
	void show()
	{
		for(int i=0;i<n;i++)
			cout<<s[i]<<" ";
		cout<<endl;
	}
	T search(T a)
	{
		for(int i=0;i<n;i++)
		{
		 
			if(a==s[i])
				return i+1; 
		}
		return 0;
	}
	
	
	private:
		T *s;
		int n;
		 
			
};
int main()
{
	int n ;
	cin>>n; 
	int s[1000];
	for(int i=0;i<n;i++) cin>>s[i];
	Array <int> A(s,n);
	
	cin>>n;
	double s1[1000];  
	for(int i=0;i<n;i++) cin>>s1[i];
	Array <double> B(s1,n);
	
	int a;
	double b;
	cin>>a>>b; 
	cout<<"array1:"<<endl<<"原序列:";  A.show() ;
	if(A.search(a))
		cout<<a<<"在array1"<<"中的位置:"<<A.search(a)<<endl; 
	else cout<<a<<" 在array1"<<"中不存在"<<endl;
	A.sort();
	cout<<"排序后:";A.show();  
	 
	cout<<"array2"<<":"<<endl<<"原序列:";  B.show() ;
	 if(B.search(b))
		cout<<b<<"在array2"<<"中的位置:"<<B.search(b)<<endl; 
	else cout<<b<<" 在array2" <<"中不存在"<<endl;
	B.sort();
	cout<<"排序后:";B.show();  
	 
 	 
	return 0;
}

2. (50分) C++ -S6-2-学院员工-多态性

题目描述
某学院的员工包括教师、行政人员。其中行政人员有基本工资和岗位津贴,教师有基本工资和课时津贴(课时津贴=课时量小时课酬(元/小时)),已知:
行政人员的年薪=基本工资
12+岗位津贴
教师的年薪=基本工资12+课时量小时课酬
定义抽象类Person类,数据成员有工号、姓名、基本工资(double型);成员函数有带参构造函数、虚函数print()、纯虚函数calSalary();定义Person的公有派生类教师类、行政人员类,增加相应的数据成员(均为double型)。计算各类人员的年薪并输出。
主函数中定义教师类、行政人员类对象,其初始值均由键盘输入,利用C++的多态性,利用基类指针调用虚函数,计算各类人员的年薪并输出

输入描述
行政人员类对象的初始值
教师类对象的初始值

输出描述
行政人员类、教师类对象的工号、姓名、年薪

输入样例
1001 赵云 4500 1500
10003 刘玄德 6700 120 50

输出样例
工号:1001
姓名:赵云
年薪:55500
工号:10003
姓名:刘玄德
年薪:86400

用户代码

#include <iostream>
#include<cmath>
#include<string>
using namespace std;

class Person
{
	public:
		Person(int a,string b,double c):num(a),name(b),salary(c)
		{
			
		}
		virtual void print()
		{
			cout<<"工号:"<<num<<endl;
			cout<<"姓名:"<<name<<endl;
 
		}
		virtual void calSalary()=0;
		
	protected:
		int num;
		string name;
		double salary;
};
class Teacher:public Person
{	public:
		Teacher(int a,string b,double c,double ano):Person(a,b,c)
		{
			another=ano;
		
		}
		void calSalary()
		{
			cout<<"年薪:"<<salary*12+another<<endl;
 
		 }
		  
		
	private:
		double another;
};
class OtherPeople:public Person
{	public:
		OtherPeople(int a,string b,double c,double h,double p):Person(a,b,c)
		{
			hour=h;
			price=p;
		
		}
		void calSalary()
		{
			cout<<"年薪:"<<salary*12+hour*price<<endl;
		 
		}
		
	private:
		double price,hour;
};
 
 
int main()
{
	int num1,num2;string name1,name2;double salary1,salary2;
	double add_salary;
	double hour,price;
	cin>>num1>>name1>>salary1>> add_salary;
	cin>>num2>>name2>>salary2>> hour>>price;

	
	Teacher A(num1,name1,salary1, add_salary);
	OtherPeople B(num2,name2,salary2, hour,price);
	Person *p;
	p=&A;
	p->print();
	p->calSalary();
	p=&B;
	p->print();
	p->calSalary(); 
	
	
	 
 	 
	return 0;
}
发布了37 篇原创文章 · 获赞 10 · 访问量 750

猜你喜欢

转载自blog.csdn.net/qq_43608850/article/details/104318671
今日推荐