#include <iostream> // 編譯預處理命令
#include <cstring>
#include<fstream>
using namespace std; // 使用命名空間std
// 宣告員工類
class Employee
{
protected:
// 保護成員
int num; // 編號
string name; // 姓名
float basicSalary; // 基本工資
float prize; // 獎金
public:
// 公有函式
Employee(int nu,string na,float bs,float pr):num(nu),name(na),basicSalary(bs),prize(pr){}
virtual void Show()
{
cout<<"編號:"<<num<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"基本工資:"<<basicSalary<<"元"<<endl;
cout<<"獎金:"<<prize<<"元"<<endl;
}
virtual void ShowSalary()
{
cout<<"實發工資:"<<basicSalary+prize<<"元"<<endl;
}
};
int main() // 主函式main()
{
Employee b[3]={Employee(106, "李1明", 1680, 1080),Employee(107, "李2明", 1681, 1081),Employee(10, "李3明", 1682, 1082)}; // p指向矩形物件
fstream f;
int x;
f.open("Employee.txt",ios::out);
if(f.fail())
{
cout<<"打開檔案失敗"<<endl;
exit(1);
}
for(int i=0;i<3;i++)
{
f<<b[i]<<endl;
}
f.close();
f.open("Employee.txt",ios::in);
if(f.fail())
{
cout<<"打開檔案失敗"<<endl;
exit(2);
}
while(!f.eof())
{
f>>x;
cout<<x<<endl;
}
cout<<endl;
f.close();
cout << "員工資訊:" << endl;
for(int i=0;i<3;i++)
{ // 輸出提示
b[i].Show(); // 輸出基本資訊
b[i].ShowSalary();
}
}
uj5u.com熱心網友回復:
你要在employee類里面實作<<uj5u.com熱心網友回復:
怎么實作呀?
uj5u.com熱心網友回復:
運算子多載ostream& operator <<(ostream& os ,const Employee& tmp)
{
os<<tmp. num<<tmp. name; //你想要的輸出格式
return os;
}
手機敲的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/37753.html
標籤:C++ 語言
