您的当前位置:首页正文

C++期末考试试题

2022-03-08 来源:易榕旅网
编程题1. 创建一个名为time的类,它包括分别表示小时、分钟和秒的int类型的数据成员以及两个构造函数,一个构造函数将数据成员初始化为8,另一个将数据成员初始化为一个固定的值。另有一成员函数将时间以10:10:10的格式显示在屏幕上,编写成为一个完整的程序。#include class Time { private:

int x,y,z;

public: };

void main() { }

编程题2.设计一个学校在册人员类(Person)。数据成员包括:身份证号(IdPerson),姓名(Name),性别(Sex),生日(Birthday)和家庭住址(HomeAddress)。成员函数包括人员信息的录入和显示。还包括构造函数与拷贝构造函数。编写成为一个完整的程序。#include #include using namespace std; enum Tsex{mid,man,woman};

Time t1(10,10,10); t1.disp(); Time(){x=y=z=8;}

Time(int a,int b,int c){x=a;y=b;z=c;} void disp() { }

cout<class Person{

char IdPerson[19]; //身份证号,18位数字 char Name[20]; //姓名 Tsex Sex; //性别

int Birthday; //生日,格式1986年8月18日写作19860818 char HomeAddress[50]; //家庭地址 public:

Person(char *,char *,Tsex,int,char *); Person(Person &); Person(); ~Person();

void PrintPersonInfo(); void inputPerson(); };

Person::Person(char *id,char *name,Tsex sex,int birthday,char *homeadd){ strcpy(IdPerson,id); strcpy(Name,name); Sex=sex;

Birthday=birthday;

strcpy(HomeAddress,homeadd); }

Person::Person(){

IdPerson[0]='\\0';Name[0]='\\0';Sex=mid; Birthday=0;HomeAddress[0]='\\0'; }

Person::Person(Person & Ps){ strcpy(IdPerson,Ps.IdPerson); strcpy(Name,Ps.Name); Sex=Ps.Sex;

Birthday=Ps.Birthday;

strcpy(HomeAddress,Ps.HomeAddress); }

Person::~Person(){ }

void Person::inputPerson(){ char ch;

cout<<\"请输入身份证号,18位数字:\"<cout<<\"请输入性别m或w:\"<>ch;

if(ch=='m') Sex=man; else Sex=woman;

cout<<\"请输入生日,格式1986年8月18日写作19860818:\"<>Birthday;

cin.get(); //吸收回车符,否则地址输不进去 cout<<\"请输入地址:\"<void Person::PrintPersonInfo(){ int i;

cout<<\"身份证号:\"<cout<cout<int main(){

Person Ps1(\"340822198608181611\程利\安徽省安庆市怀宁县\"), Ps2(Ps1),Ps3;

Ps1.PrintPersonInfo(); Ps2.PrintPersonInfo(); Ps3.inputPerson(); Ps3.PrintPersonInfo(); return 0; }

编程题3.建立一个基类CBuilding,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Chousing,继承CBuilding,并存储下面的内容:卧室和浴室的数量。另外再建立派生类COfficBuilding,继承CBuilding,并存储灭火器和电话的数目。然后编写应用程序,建立住宅对象和办公楼对象,并输出它们的有关数据。编写成为一个完整的程序。

#include #include using namespace std; class cbuilding { protected:

int floor,rooms,area;

public:

cbuilding(int f,int r,int a) { floor=f; rooms=r; }

area=a;

};

class chousing: public cbuilding { int bedrooms,bathrooms; public:

void display()

{cout<<\"the number of floor is \"<cout<<\"the number of bedrooms is \"<chousing( int f,int r,int a,int b,int ba):cbuilding( f, r, a) { bedrooms=b; bathrooms=ba; }

};

class cofficebuildings: public cbuilding { int fires,phones; public:

void display()

cofficebuildings( int f,int r,int a,int b,int ba):cbuilding( f, r, a) { fires=b; phones=ba; }

{cout<<\"the number of floor is \"<};

void main() {

chousing house(1,2,3,4,5); }

编程题4.某公司雇员(employee)包括技术人员(technician)和销售员(salesman)。以employ类为虚基类派生出technician和salesman类;employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。

technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。业绩工资为三者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。salesman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。编写成为一个完整的程序。 #include

cofficebuildings office(6,7,8,9,10); house.display(); office.display();

#include using namespace std;

static int Grades[]={500,600,750,1000,1400,2000,2800,4000}; class employee{ protected:

string name;//姓名 int ID;//职工号 int grade;//工资级别 double salary;//月

double base_salary;//基本月薪 double career_salary;//业绩工资

public: };

employee::employee(string nn,int id,int gr){ }

employee::employee(employee &emp){ //拷贝构造函数

name=emp.name; name=nn; ID = id; grade = gr;

salary=0; //月薪 base_salary=0; //基本月薪 career_salary=0; //业绩工资

employee(string=\"\int=0,int=0);

employee(employee &); //拷贝构造函数 virtual void pay(); //月薪计算函数 void show();

double getsalary(){return salary;}

double getbase_salary(){return base_salary;} double getcareer_salary(){return career_salary;}

}

ID=emp.ID;

grade =emp.grade;

salary=emp.salary; //月薪 base_salary=emp.base_salary; //基本月薪 career_salary=emp.career_salary;//业绩工资

void employee::show(){ cout<class manager:virtual public employee{//虚基类 protected:

double prize;//固定奖金额 double factor;//业绩系数 int days;

cout<<\"请输入请假天数:\\n\"; cin>>days;

base_salary = Grades[grade]*(23 - days)/23;

career_salary = base_salary/2; //普通员工业绩工资为基本工资的一半 salary = base_salary+career_salary;

public:

manager(string=\"\int=0,int=0,double=0); manager(manager&mag); //拷贝构造函数 void pay();};

manager::manager(manager&mag):employee(mag){

prize=mag.prize; factor=mag.factor; }

manager::manager(string nn,int id,int gr,double pr):employee(nn,id,gr){

prize = pr;//固定奖金额 factor = 0; }

void manager::pay(){

}

int days;

cout<<\"请输入请假天数:\\n\"; cin>>days;

cout<<\"请输入业绩系数:\\n\"; cin>>factor;

base_salary = Grades[grade]*(23 - days)/23; career_salary = prize*factor*(23 - days)/23; salary = base_salary + career_salary ;

class technician:virtual public employee{ protected:

double hours;//月工作时数 double perhour;//每小时附加酬金 double shfactor;//研究进度系数

public:

technician(string=\"\int=0,int=0,double=0); technician(technician &); //拷贝构造函数 void pay();};

technician::technician(technician& tech):employee(tech){ //拷贝构造函数 }

technician::technician(string nn,int id,int gr,double phr):employee(nn,id,gr){

hours = 0;

perhour = phr;//每小时附加酬金 shfactor = 0; }

hours=tech.hours;//月工作时数

perhour=tech.perhour;//每小时附加酬金 shfactor=tech.shfactor;//研究进度系数

void technician::pay(){

int days;

cout<<\"请输入请假天数:\\n\";

cin>>days;

cout<<\"请输入研究进度系数:\\n\"; hours=8*(23 - days); cin>>shfactor;

base_salary = Grades[grade]*(23 - days)/23;

career_salary = perhour*hours*shfactor*(23 - days)/23; salary= base_salary + career_salary ; }

class salesman:virtual public employee{ protected:

double amount;//销售额 double slfactor;//提成比例

public: };

salesman::salesman(string nn,int id,int gr,double slfac):employee(nn,id,gr){

amount = 0;

slfactor = slfac; }

salesman(string=\"\int=0,int=0,double=0); salesman(salesman &); //拷贝构造函数 void pay();

salesman::salesman(salesman &sale):employee(sale){

amount=sale.amount; slfactor=sale.slfactor; }

void salesman::pay(){

int days;

cout<<\"请输入请假天数:\\n\"; cin>>days;

cout<<\"请输入销售额:\\n\"; cin>>amount;

base_salary = Grades[grade]*(23 - days)/23;

career_salary = amount*slfactor; salary = base_salary + career_salary ;}

class developermanager:public manager,public technician { public:

developermanager(string=\"\int id=0,int gr=0,double pr=0, double phr=0); developermanager(developermanager &); //拷贝构造函数 void pay();};

developermanager::developermanager(string nn,int id,int gr,double pr,double phr)

:manager(nn,id,gr,pr),technician(nn,id,gr,phr),employee(nn,id,gr){}

developermanager::developermanager(developermanager &deman)

:manager(deman),technician(deman),employee(deman){}

void developermanager::pay(){ }

class salesmanager:public manager,public salesman{ public:

salesmanager(string=\"\int=0,int=0,double pr=0, double slfac=0); salesmanager(salesmanager &); //拷贝构造函数 void pay(); int days;

cout<<\"请输入请假天数:\\n\"; cin>>days;

cout<<\"请输入业绩系数:\\n\"; cin>>factor;

cout<<\"请输入研究进度系数:\\n\"; cin>>shfactor; hours=8*(23 - days);

base_salary = Grades[grade]*(23 - days)/23;//基本工资

career_salary = perhour*hours*shfactor*(23 - days)/23;//技术人员奖金 career_salary += prize*factor*(23 - days)/23;//加经理业绩工资 career_salary /=2;

salary = base_salary + career_salary ;

};

salesmanager::salesmanager(string nn,int id,int gr,double pr,double slfac)

:manager(nn,id,gr,pr),salesman(nn,id,gr,slfac),employee(nn,id,gr){}

salesmanager::salesmanager(salesmanager& salman) 数

void salesmanager::pay(){

int days;

cout<<\"请输入请假天数:\\n\"; cin>>days;

cout<<\"请输入业绩系数:\\n\"; cin>>factor;

cout<<\"请输入销售额:\\n\"; cin>>amount;

base_salary = Grades[grade]*(23 - days)/23;//基本工资 career_salary = prize*factor*(23 - days)/23;//经理业绩工资 career_salary /=2;

career_salary += amount*slfactor;//加销售业绩工资 salary = base_salary + career_salary ; }

:manager(salman),salesman(salman),employee(salman){} //拷贝构造函

int main(){

employee eml1(\"张伟\manager mag1(\"姚婕echnician tec1(\"王茜\salesman sal1(\"朱明\

developermanager dem1(\"沈俊\salesmanager sam1(\"况钟\eml1.pay(); eml1.show(); mag1.show(); tec1.show(); sal1.show();

tec1.pay(); sal1.pay(); emlp=&dem1;

mag1.pay();

emlp->pay(); emlp->show();

emlp=&sam1; emlp->pay(); emlp->show();

cout<employee eml0(eml1); manager mag0(mag1); technician tec0(tec1); salesman sal0(sal1);

developermanager dem0(dem1); salesmanager sam0(sam1); cout<<\"输出拷贝结果:\"<mag0.show(); sal0.show();

emlp=&dem0; emlp->show(); emlp=&sam0; emlp->show(); return 0;}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 版权所有