(1)编写一个程序计算正方体、球体和圆柱体的表面积和体积。
定义一个抽象类Shape,在此基础上派生出正方体类、球体类和圆柱体类,都有计算对象表面积和体积的函数Area()和计算对象体积的函数Volume (),在主函数中定义一个Shape指针数组分别指向正方体类、球体类和圆柱体类的对象,并通过Shape类的指针实现对这3个对象的成员函数的调用,输出正方体、球体和圆柱体的表面积和体积。
#include <iostream>
using namespace std;
const double pi=3.14;
class Shape{
public:
Shape(){}
virtual double area()=0;
virtual double volume()=0;
};
class Cube:public Shape{
public:
Cube(){}
Cube(double side) {this->side=side;}
double area(){return 6*side*side;}
double volume(){return side*side*side;}
private:
double side;
};
class Sphere:public Shape{
public:
Sphere(){}
Sphere(double radius){this->radius=radius;}
double area(){return 4.0*pi*radius*radius;}
double volume(){return 4.0*pi*radius*radius*radius/3.0;}
private:
double radius;
};
class Volumn:public Shape{
public:
Volumn(){}
Volumn(double radius,double height){
this->radius=radius;
this->height=height;}
double area(){return 2.0*height*pi*radius+2*pi*radius*radius;}
double volume(){return height*pi*radius*radius;}
private:
double radius;
double height;
};
void test1()
{
Shape *p;
Cube A(2.0);
p=&A;
cout<<"Shape:"<<"正方体"<<endl<<" 面积、体积:"<<p->area()<<" "<<p->volume()<<endl;
Sphere B(3.0);
p=&B;
cout<<"Shape:"<<"球体"<<endl<<" 面积、体积:"<<p->area()<<" "<<p->volume()<<endl;
Volumn C(3.0,2.0);
p=&C;
cout<<"Shape:"<<"圆柱体"<<endl<<" 面积、体积:"<<p->area()<<" "<<p->volume()<<endl;
}
int main()
{
test1();
return 0;
}
某学校对教师工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。
要求:
定义教师抽象类,数据成员包括姓名,性别,工号,固定工资,课时数(上课总的时间),总工资;成员函数pay计算总工资,将其定义为虚函数(因计算方法不同),其他成员函数根据需要进行定义。派生出不同职称的教师类(每个类具有相同的数据成员);不同类的工资计算方法不同(课时工资不同)编写程序求若干教师的工资。
#include <iostream>
#include <string>
using namespace std;
class Teacher{
protected:
string name;
string sex;
string id;
double basicsalary;
int coursetime;
double salary;
public:
Teacher(double basicsalary){this->basicsalary=basicsalary;}
virtual void input()=0;
virtual void pay()=0;
virtual ~Teacher(){}
};
class Jiaoshou:public Teacher{
public:
Jiaoshou(double basicsalary=5000):Teacher(basicsalary){}
void input(){
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"工号:";
cin>>id;
cout<<"课时数:";
cin>>coursetime;}
void pay(){
salary=basicsalary+coursetime*50;
cout<<"本月工资:"<< salary <<endl;}
};
class Fujiaoshou:public Teacher{
public:
Fujiaoshou(double basicsalary=3000):Teacher(basicsalary){}
void input(){
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"工号:";
cin>>id;
cout<<"课时数:";
cin>>coursetime;}
void pay(){
salary=basicsalary+coursetime*30;
cout<<"本月工资:"<< salary <<endl;}
};
class Jiangshi:public Teacher{
public:
Jiangshi(double basicsalary=2000):Teacher(basicsalary){}
void input(){
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"工号:";
cin>>id;
cout<<"课时数:";
cin>>coursetime;}
void pay(){
salary=basicsalary+coursetime*20;
cout<<"本月工资:"<< salary <<endl;}
};
void test1()
{
Teacher *p;
Jiaoshou A; p=&A; p->input(); p->pay();
Fujiaoshou B; p=&B; p->input(); p->pay();
Jiangshi C; p=&C; p->input(); p->pay();
}
int main()
{
test1();
return 0;
}
- 某公司主要有四类人员:
经理、兼职技术员、兼职销售员,销售经理。
这些人员具有职工编号、姓名、年龄、性别、级别、月工资
兼职技术人员按照每小时100元领取月工资
兼职销售人员按照销售额的4%领取月工资
经理领取8000元的固定工资
销售经理既拿固定月薪,也领取销售提成;固定月薪为5000,销售提成为当月销售总额的千分之5 - 程序要具有对所有人提升级别功能(经理为4级,兼职销售为1级,其他人员为3级)人员编号基数为1000。每输入一个人员信息,编号顺序加1。
类层次结构下:
设计基类:
职工类employee
数据成员:职工编号、姓名、年龄、性别、级别,月工资
静态数据成员total(记录人员编号的最大值,新员工的编号在此基础上增1)
成员函数:
带参的构造函数(输入除工资外的基本信息。设置级别的默认值为1)
设置成员函数change_level(int),用来调整级别,定义纯虚函数pay(),用于计算月工资。派生出:
兼职技术员类technician
经理类manager
兼职销售员类salesman
利用经理类和销售员类派生出销售经理类
#include <iostream>
#include <string>
using namespace std;
class Employee{
protected:
int age,level;
double salary;
int long id;
string name,sex;
static int total;
public:
Employee(){}
Employee(int long id,string name,int age,string sex,int level=1){
this->id=id;
this->name=name;
this->age=age;
this->sex=sex;
this->level=level;}
void change_level(int newlevel){
level=newlevel;
cout<<"当前等级:"<<level<<" "<<"升级后:"<<newlevel;}
virtual void setid()=0;
virtual void input()=0;
virtual void pay()=0;
virtual ~Employee(){}
};
int Employee::total=1000;
class Technician:virtual public Employee{
private:
int workhours;
public:
Technician(){}
Technician(int long id,string name,int age,string sex,int level,int workhours):Employee(id,name,age,sex,level){
this->workhours=workhours;}
void setid(){ ++total; id=total;}
void input(){
cout<<"姓名:";cin>>name;
cout<<"年龄:";cin>>age;
cout<<"性别:";cin>>sex;
cout<<"级别:";cin>>level;
cout<<"工作时长:";cin>>workhours;}
void pay(){
cout<<"职工编号:"<<id<<" "<<"姓名:"<<name<<" "<<"年龄:"<<age<<" "<<"性别:"<<sex<<" "<<"级别:"<<level<<endl;
cout<<"工作时长:"<<workhours<<" ";
salary=100*workhours;
cout<<"本月工资:"<<salary<<endl;}
};
class Manager:virtual public Employee{
protected:
double bsalary;
public:
Manager(){}
Manager(int long id,string name,int age,string sex,int level,double bsalary):Employee(id,name,age,sex,level){
this->bsalary=bsalary;}
void setid(){ ++total; id=total;}
void input(){
cout<<"姓名:";cin>>name;
cout<<"年龄:";cin>>age;
cout<<"性别:";cin>>sex;
cout<<"级别:";cin>>level;
cout<<"固定工资:";cin>>bsalary;}
void pay(){
cout<<"职工编号:"<<id<<" "<<"姓名:"<<name<<" "<<"年龄:"<<age<<" "<<"性别:"<<sex<<" "<<"级别:"<<level<<endl;
salary=bsalary;
cout<<"本月工资:"<<salary<<endl;}
};
class Saleman:virtual public Employee{
protected:
double ssalary;
public:
Saleman(){}
Saleman(int long id,string name,int age,string sex,int level,double ssalary):Employee(id,name,age,sex,level){
this->ssalary=ssalary;}
void setid(){ ++total; id=total;}
void input(){
cout<<"姓名:";cin>>name;
cout<<"年龄:";cin>>age;
cout<<"性别:";cin>>sex;
cout<<"级别:";cin>>level;
cout<<"销售额:";cin>>ssalary;}
void pay(){
cout<<"职工编号:"<<id<<" "<<"姓名:"<<name<<" "<<"年龄:"<<age<<" "<<"性别:"<<sex<<" "<<"级别:"<<level<<endl;
salary=0.04*ssalary;
cout<<"本月工资:"<<salary<<endl;}
};
class Smanager:public Saleman,public Manager{
public:
Smanager(){}
Smanager(int long id,string name,int age,string sex,int level,double bsalary,double ssalary):Saleman(id,name,age,sex,level,ssalary),Manager(id,name,age,sex,level,bsalary){}
void setid(){ ++total; id=total;}
void input(){
cout<<"姓名:";cin>>name;
cout<<"年龄:";cin>>age;
cout<<"性别:";cin>>sex;
cout<<"级别:";cin>>level;
cout<<"固定工资:";cin>>bsalary;
cout<<"销售额:";cin>>ssalary;}
void pay(){
cout<<"职工编号:"<<id<<" "<<"姓名:"<<name<<" "<<"年龄:"<<age<<" "<<"性别:"<<sex<<" "<<"级别:"<<level<<endl;
salary=bsalary+0.005*ssalary;
cout<<"本月工资:"<<salary<<endl;}
};
void test1()
{
Employee *p;
Technician A;
p=&A; p->setid(); p->input(); p->pay();
Manager B;
p=&B; p->setid(); p->input(); p->pay();
Saleman C;
p=&C; p->setid(); p->input(); p->pay();
Smanager D;
p=&D; p->setid(); p->input(); p->pay();
}
int main()
{
test1();
return 0;
}