C++实验06_多态性与模板
实验06(01)类模板
题目描述
编写一个使用类模板对数组进行排序、查找和显示所有元素值的程序,数组中元素个数3≤n≤15
说明:设计一个类模板
template<class T>
class Array,用于对T类型的数组进行排序、查找、显示所有元素,构造函数有两个参数:传递数组首地址和数组元素个数。
主函数中实例化Array<T>产生模板类Array<int>和Array<double>,输入两个数组的长度,再依次输入各元素的值,调用相应的成员函数完成:输出数组的原序列、从键盘输入需要查找的元素值,完成查找(如有相同的元素,则返回找到的第一个数位置)、对数组进行由小到大排序,输出排序后的结果。
输入描述
输入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>
using namespace std;
template < class T >
class Array
{
T * set;
int n;
public :
Array ( T * data, int i)
{
set = data;
n = i;
}
~ Array ( ) { }
void sort ( )
{
T temp;
for ( int i = 1 ; i < n; i++ )
for ( int j = n - 1 ; j >= i; j-- )
if ( set[ j - 1 ] > set[ j] )
{
temp = set[ j - 1 ] ;
set[ j - 1 ] = set[ j] ;
set[ j] = temp;
}
}
T seek ( T key)
{
for ( int i = 0 ; i < n; i++ )
if ( set[ i] == key)
return i + 1 ;
return 0 ;
}
void disp ( )
{
for ( int i = 0 ; i < n; i++ )
cout << set[ i] << " " ;
cout << "\n" ;
}
} ;
int main ( )
{
int n1, n2;
int m1;
double m2;
int a[ 15 ] = { 0 } ;
double b[ 15 ] = { 0 } ;
cin >> n1;
for ( int i = 0 ; i < n1; i++ )
cin >> a[ i] ;
cin >> n2;
for ( int i = 0 ; i < n2; i++ )
cin >> b[ i] ;
cin >> m1 >> m2;
Array< int > arr1 ( a, n1) ;
Array< double > arr2 ( b, n2) ;
cout << "array1:" << endl;
cout << "原序列:" ;
arr1. disp ( ) ;
if ( arr1. seek ( m1) )
cout << m1 << "在array1中的位置:" << arr1. seek ( m1) << endl;
else
cout << m1 << "在array1中不存在" << endl;
arr1. sort ( ) ;
cout << "排序后:" ;
arr1. disp ( ) ;
cout << "array2:" << endl;
cout << "原序列:" ;
arr2. disp ( ) ;
if ( arr2. seek ( m2) )
cout << m2 << "在array2中的位置:" << arr2. seek ( m2) << endl;
else
cout << m2 << "在array2中不存在" << endl;
arr2. sort ( ) ;
cout << "排序后:" ;
arr2. disp ( ) ;
return 0 ;
}
实验06(02)学院员工-多态性
题目描述
某学院的员工包括教师、行政人员。其中行政人员有基本工资和岗位津贴,教师有基本工资和课时津贴(课时津贴=课时量* 小时课酬(元/小时)),已知:
行政人员的年薪=基本工资 * 12+岗位津贴
教师的年薪=基本工资 * 12+课时量 * 小时课酬
定义抽象类Person类,数据成员有工号、姓名、基本工资(double型);成员函数有带参构造函数、虚函数print()、纯虚函数calSalary();定义Person的公有派生类教师类、行政人员类,增加相应的数据成员(均为double型)。计算各类人员的年薪并输出。
主函数中定义教师类、行政人员类对象,其初始值均由键盘输入,利用C++的多态性,利用基类指针调用虚函数,计算各类人员的年薪并输出
输入描述
行政人员类对象的初始值
教师类对象的初始值
输出描述
行政人员类、教师类对象的工号、姓名、年薪
输入样例
1001 赵云 4500 1500
10003 刘玄德 6700 120 50
输出样例
工号:1001
姓名:赵云
年薪:55500
工号:10003
姓名:刘玄德
年薪:86400
#include <iostream>
#include <string>
using namespace std;
class Person
{
public :
Person ( int a, string b, double c) ;
virtual void print ( ) ;
virtual void calSalary ( ) = 0 ;
double GetWage ( )
{
return Wage;
}
private :
int Num;
string Name;
double Wage;
} ;
Person:: Person ( int a, string b, double c)
{
Num = a;
Name = b;
Wage = c;
}
void Person:: print ( )
{
cout << "工号:" << Num << endl;
cout << "姓名:" << Name << endl;
}
class Teacher : public Person
{
public :
Teacher ( int a, string b, double c, double d, double e) ;
virtual void print ( ) ;
void calSalary ( ) ;
private :
double HourlyPay;
double Hours;
} ;
void Teacher:: print ( )
{
Person:: print ( ) ;
Teacher:: calSalary ( ) ;
}
void Teacher:: calSalary ( )
{
cout << "年薪:" << GetWage ( ) * 12 + Hours * HourlyPay << endl;
}
Teacher:: Teacher ( int a, string b, double c, double d, double e) : Person ( a, b, c)
{
Hours = d;
HourlyPay = e;
}
class Administrator : public Person
{
public :
Administrator ( int a, string b, double c, double d) ;
virtual void print ( ) ;
void calSalary ( ) ;
private :
double PostAllowance;
} ;
Administrator:: Administrator ( int a, string b, double c, double d) : Person ( a, b, c)
{
PostAllowance = d;
}
void Administrator:: calSalary ( )
{
cout << "年薪:" << GetWage ( ) * 12 + PostAllowance << endl;
}
void Administrator:: print ( )
{
Person:: print ( ) ;
Administrator:: calSalary ( ) ;
}
int main ( )
{
int Num1;
string Name1;
double Wage1;
double PostAllowance;
int Num2;
string Name2;
double Wage2;
double HourlyPay;
double Hours;
cin >> Num1 >> Name1 >> Wage1 >> PostAllowance >> Num2 >> Name2 >> Wage2 >> Hours >> HourlyPay;
Administrator A ( Num1, Name1, Wage1, PostAllowance) ;
Teacher B ( Num2, Name2, Wage2, Hours, HourlyPay) ;
Person * p1, * p2;
p1 = & A;
p2 = & B;
p1- > print ( ) ;
p2- > print ( ) ;
return 0 ;
}