在线等高手帮忙做C++.要快,谢谢了
发布网友
发布时间:2023-07-12 10:45
我来回答
共3个回答
热心网友
时间:2024-02-27 13:10
晕死 ....哥们....你谁....这不是咱下午考试题么》???
我给你一个.....
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class employee
{
private:
string name;
string id;
public:
employee(string n,string i)
{
name=n;id=i;
}
string get_name()
{
return name;
}
string get_id()
{
return id;
}
virtual void print() {};
virtual void earings() {};
};
class salariedemployee:public employee
{
private:
double salary;
public:
salariedemployee(string n,string i,double s):employee( n,i )
{
salary=s;
}
double get_salary()
{
return salary;
}
void print()
{
cout<<"*******************************"<<endl;
cout<<"正式雇员的姓名为:"<<get_salary()<<endl;
cout<<"正式雇员的id为:"<<get_id()<<endl;
cout<<"正式雇员的薪水为:"<<get_salary()<<endl;
}
void earings()
{
cout<<"该正式雇员的收入为:"<<get_salary()<<endl;
}
};
class hourlyemployee:public employee
{
private:
double wage;
double hours;
public:
hourlyemployee(string n,string i,double w,double h):employee(n,i)
{
wage=w;hours=h;
}
double get_wage()
{
return wage;
}
double get_hours()
{
return hours;
}
void print()
{
cout<<"*******************************"<<endl;
cout<<"合同工的姓名为:"<<get_name()<<endl;
cout<<"合同工的id为:"<<get_id()<<endl;
cout<<"合同工的每小时工资为:"<<get_wage()<<endl;
cout<<"合同工的每周工作小时数为:"<<get_hours()<<endl;
}
double get_earings()
{
return (wage*hours);
}
void earings()
{
cout<<"该合同工的收入为:"<<wage*hours<<endl;
cout<<"*******************************"<<endl;
}
};
///////////////////////////////////////////
int main()
{
salariedemployee s1("阿黄","0001",5000);
salariedemployee s2("阿黑","0002",6000);
hourlyemployee h1("阿绿","0003",200,10);
hourlyemployee h2("阿紫","0004",150,15);
employee *pt[4];
pt[0]=&s1;
pt[0]->print();pt[0]->earings();
pt[1]=&h1;
pt[1]->print();pt[1]->earings();
pt[2]=&s2;
pt[2]->print();pt[2]->earings();
pt[3]=&h2;
pt[3]->print();pt[3]->earings();
ofstream outfile("employee.txt",ios::out);
if(! outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<"正式雇员"<<"姓名:"<<s1.get_name()<<"id:"<<s1.get_id()<<"薪水:"<<s1.get_salary()<<endl;
outfile<<"正式雇员"<<"姓名:"<<s2.get_name()<<"id:"<<s2.get_id()<<"薪水:"<<s2.get_salary()<<endl;
outfile<<"合同工"<<"姓名:"<<h1.get_name()<<"id:"<<h1.get_id()<<"每小时工资"<<h1.get_wage()<<"小时数"<<h1.get_hours()<<"薪水"<<h1.get_earings()<<endl;
outfile<<"合同工"<<"姓名:"<<h2.get_name()<<"id:"<<h2.get_id()<<"每小时工资"<<h2.get_wage()<<"小时数"<<h2.get_hours()<<"薪水"<<h2.get_earings()<<endl;
outfile.close();
cout<<"读出为:"<<endl;
ifstream infile("employee.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
char ch;
while(infile.get(ch))
cout.put(ch);
cout<<endl;
infile.close();
return 0;
}
热心网友
时间:2024-02-27 13:10
真的很急,谢谢大家..没有奖励.但只要做了我加QQ,会有补偿给你的。谢谢拉。.
热心网友
时间:2024-02-27 13:11
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<fstream>
using namespace std;
class Employee
{
string name;
string id;
public:
Employee(string n, string i)
:name(n),
id(i)
{}
virtual double earnings() const=0;
virtual void print(ofstream&outfile) const
{
outfile<<name<<":\n";
outfile<<"id: "<<id<<endl;
}
};
class salariedEmployee:public Employee
{
double salary;
public:
salariedEmployee(string n,string i,double s)
:Employee(n,i),
salary(s)
{}
double earnings() const
{
return salary;
}
void print(ofstream&outfile) const
{
Employee::print(outfile);
outfile<<"salary: "<<salary<<endl;
}
};
class hourlyEmployee:public Employee
{
double wage;
double hours;
public:
hourlyEmployee(string n,string i,double w,double h)
:Employee(n,i),
wage(w),
hours(h)
{}
double earnings() const
{
return wage*hours;
}
void print(ofstream&outfile) const
{
Employee::print(outfile);
outfile<<"wage: "<<wage<<endl;
outfile<<"hours: "<<hours<<endl;
}
};
int main()
{
ofstream outfile("b.txt",ios::out);
salariedEmployee se("T","56087",2000);
hourlyEmployee he("R","11028",20,48);
vector<Employee*>em(2);
em[0]=&se;
em[1]=&he;
for(int i=0;i<2;i++)
{
em[i]->print(outfile);
outfile<<"total earnings:"<<em[i]->earnings()<<endl;
cout<<endl;
}
ifstream infile("b.txt",ios::in);
string str;
while(infile>>str)cout<<str<<endl;
return 0;
}