很简单的代码,过了测试用例就没修改了。
一.某公司财务部需要开发一个计算机雇员工资的程序,该公司有3类员工,工人的工资为每小时工资额(通过成员函数设定)乘以当月工作时数(通过成员函数设定)再加上工龄工资;销售员工资为每小时工资额(通过成员函数设定)乘以当月工作时数(通过成员函数设定)加上销售额提成再加上工龄工资,其中销售额提成等于该销售员当月售出商品金额(通过成员函数设定)的1%;管理人员工资为基本工资1000元再加上工龄工资,工龄工资就是雇员在该公司工作的工龄每增加一年,月工资就增加35元。请用面向对象方法分析、设计这个程序,并用C++语言写出完整程序。
设计基类Employee,定义如下:
class Employee
{
protected:
char name[30];
int working_years;
public:
Employee(const char nm[30]=“unnamed”,int wy=0);
char *Getname();
double ComputePay();
void SetWorkyears(int wy);
};
由Employee派生三个类Worker,SalesPerson 和Manager.
在Worker类中添加数据成员
double pay_per_hour;
int work_time;
成员函数:
double count_pay();
void SetWorktime(int wt);
void Setpay_per_hour(int x);
在SalesPerson类中添加数据成员:
double pay_per_hour;
double saleroom;
int work_time; 和Manager.
成员函数:
double count_pay();
void SetWorktime(int wt);
void Setpay_per_hour(int x);
void Setsalesroom(double sr);
在Manager类中添加成员函数:
double count_pay();
使用如下的main()函数,请复制使用:
int main()
{
Worker work(“zhangqiang”,3,200);
work.Setpay_per_hour(50);
cout<<“工资=”<<work.count_pay()<<endl;
work.SetWorktime(180);
work.SetWorkyears(10);
work.Setpay_per_hour(30);
cout<<work.Getname()<<" “<<work.count_pay()<<endl;
SalesPerson sales(“wangjun”,5,300000,25);
sales.SetWorktime(40);
sales.Setpay_per_hour(80);
sales.Setsalesroom(450000);
cout<<sales.Getname()<<” “<<sales.count_pay()<<endl;
Manager mana(“sunchao”,20);
cout<<mana.Getname()<<” "<<mana.count_pay()<<endl;
return 0;
}
运行结果如下:
工资=10105
zhangqiang 5750
wangjun 7875
sunchao 1700
#include<iostream>
#include<string.h>
using namespace std;
class Employee
{
protected:
char name[30];
int working_years;
public:
Employee(const char nm[30] = "unnamed", int wy = 0)
{
strcpy(name, nm);
working_years = wy;
}
char *Getname()
{
return name;
}
double ComputePay()
{
return 35 * working_years;
}
void SetWorkyears(int wy)
{
working_years = wy;
}
};
class Worker :public Employee
{
private:
double pay_per_hour;
int work_time;
public:
Worker(const char a[],int i,int x)
{
strcpy(name, a);
working_years = i;
work_time = x;
}
double count_pay()
{
return pay_per_hour * work_time + ComputePay();
}
void SetWorktime(int wt)
{
work_time = wt;
}
void Setpay_per_hour(int x)
{
pay_per_hour = x;
}
};
class SalesPerson :public Employee
{
private:
double pay_per_hour;
double saleroom;
int work_time;
public:
SalesPerson(const char a[],int i,int x,int y)
{
strcpy(name, a);
working_years = i;
saleroom = x;
work_time = y;
}
double count_pay()
{
return pay_per_hour * work_time + saleroom * 0.01 + ComputePay();
}
void SetWorktime(int wt)
{
work_time = wt;
}
void Setpay_per_hour(int x)
{
pay_per_hour = x;
}
void Setsalesroom(double sr)
{
saleroom = sr;
}
};
class Manager :public Employee
{
public:
double count_pay()
{
return 1000 + ComputePay();
}
Manager(const char a[], int y)
{
strcpy(name, a);
working_years = y;
}
};
int main()
{
Worker work("zhangqiang", 3, 200);
work.Setpay_per_hour(50);
cout << "工资=" << work.count_pay() << endl;
work.SetWorktime(180);
work.SetWorkyears(10);
work.Setpay_per_hour(30);
cout << work.Getname() << " " << work.count_pay() << endl;
SalesPerson sales("wangjun", 5, 300000, 25);
sales.SetWorktime(40);
sales.Setpay_per_hour(80);
sales.Setsalesroom(450000);
cout << sales.Getname() << " " << sales.count_pay() << endl;
Manager mana("sunchao", 20);
cout << mana.Getname() << " " << mana.count_pay() << endl;
getchar();
return 0;
}
二.设计一个产品类 Product ,允许通过如下方式来创建产品对象: 通过指定产品名创建; 通过指定产品名和产品价格创建; 通过指定产品名、产品价格、出厂日期(对象成员)创建; Product 还应该包含如下属性:生产厂家、易碎标记、有效日期(使用对象成员)。设计该类时请至少增加 3 个其他属性。成员函数包括访问和修改这些属性的操作。 在 main( ) 中定义对象,并输出相关信息。
相关类的定义提示:
class Product
{
char *name;
double price;
Date deptime;
char *factory;
bool easy_break;
Date valtime;
…
public:
…
void output ( );
};
输出函数提示 :
void Product::output ( )
{
cout<<name<<" “<<price<<endl;
cout<<deptime.Getyear()<<”-"<<deptime.Getmonth()<<"-"<<deptime.Getday()<<endl;
cout<<factory<<" “<<easy_break<<endl;
cout<<valtime.Getyear()<<”-"<<valtime.Getmonth()<<"-"<<valtime.Getday()<<endl;
}
main()函数如下,请复制使用
int main()
{
char s[10];
cin>>s;
Product p1(“car”);
Product p2(“glass”,3.00);
Product p3(“pen”,5.00,2009,3,14);
p1.SetProduct(“car”,100000.0,2009,3,14,“nanjing”,0,2010,10,14,“red”,1.5);
p1.output();
p2.output();
p3.output();
return 0;
}
输入描述
Start
输出描述
Constrcting One…
Constrcting Two…
Constrcting Three…
car 100000
2009-3-14
nanjing 0
2010-10-14
glass 3
2000-1-1
unnamed 0
2000-1-1
pen 5
2009-3-14
unnamed 0
2000-1-1
样例输入1:
Start
样例输出1:
Constrcting One…
Constrcting Two…
Constrcting Three…
car 100000
2009-3-14
nanjing 0
2010-10-14
glass 3
2000-1-1
unnamed 0
2000-1-1
pen 5
2009-3-14
unnamed 0
2000-1-1
#include<iostream>
#include<string.h>
using namespace std;
#pragma warning(disable:4996)
class Date
{
private:
int year;
int month;
int day;
public:
Date(int y = 2000, int m = 1, int d = 1)
{
year = y;
month = m;
day = d;
}
int Getyear()
{
return year;
}
int Getmonth()
{
return month;
}
int Getday()
{
return day;
}
void settime(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
};
class Product
{
char *name;
double price;
Date deptime;
char *factory;
bool easy_break;
Date valtime;
char* color;
double high;
public:
void output();
Product(const char a[])
{
name = new char[strlen(a) + 1];
strcpy(name, a);
factory = new char[strlen("unnamed") + 1];
strcpy(factory, "unnamed");
easy_break = 0;
price = 0;
cout << "Constrcting One...." << endl;
}
Product(const char a[],double pr)
{
name = new char[strlen(a) + 1];
factory = new char[strlen("unnamed") + 1];
strcpy(name, a);
strcpy(factory, "unnamed");
easy_break = 0;
price = pr;
cout << "Constrcting Two...." << endl;
}
Product(const char a[], double pr,int x,int y,int z):deptime(x,y,z)
{
name = new char[strlen(a) + 1];
factory = new char[strlen("unnamed") + 1];
strcpy(name, a);
strcpy(factory, "unnamed");
price = pr;
easy_break = 0;
cout << "Constrcting Three...." << endl;
}
void SetProduct(const char a[], double pr, int x, int y, int z, const char *fa,bool ea ,int x1, int y1, int z1, const char c[],double h)
{
name = new char[strlen(a) + 1];
factory = new char[strlen(fa) + 1];
color = new char[strlen(c) + 1];
strcpy(name, a);
strcpy(factory, fa);
strcpy(color, c);
deptime.settime(x, y, z);
valtime.settime(x1, y1, z1);
price = pr;
easy_break = ea;
high = h;
}
};
void Product::output()
{
cout << name << " " << price << endl;
cout << deptime.Getyear() << "-" << deptime.Getmonth() << "-" << deptime.Getday() << endl;
cout << factory << " " << easy_break << endl;
cout << valtime.Getyear() << "-" << valtime.Getmonth() << "-" << valtime.Getday() << endl;
}
int main()
{
char s[10];
cin >> s;
Product p1("car");
Product p2("glass", 3.00);
Product p3("pen", 5.00, 2009, 3, 14);
p1.SetProduct("car", 100000.0, 2009, 3, 14, "nanjing", 0, 2010, 10, 14, "red", 1.5);
p1.output();
p2.output();
p3.output();
getchar();
getchar();
return 0;
}