C++试题精选----I/O----NO.1
Posted 敲代码的xiaolang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++试题精选----I/O----NO.1相关的知识,希望对你有一定的参考价值。
希望c++的入门者们闲暇之余,可以浏览思考,有什么问题欢迎留言或者私信。
I/O----NO.1
#include<iostream>
#include<string.h>
using namespace std;
class EMPLOYEE{
private:
long id;
char name[20];
char department[50];
int age;
double salary;
public:
EMPLOYEE();
EMPLOYEE(const EMPLOYEE& obj);
EMPLOYEE(long idd, char namee[], char depar[], int agee, double sala);
~EMPLOYEE();
void changeDepartment(char newDepar[]);
void changeAge(int agee);
void raiseSalary(double delta);
// 定义友元函数,提供输入/输出运算"<<"和">>"。
friend ostream& operator<<(ostream& stream, EMPLOYEE& obj);
friend istream& operator>>(istream& stream, EMPLOYEE& obj);
};
EMPLOYEE::EMPLOYEE()
{
id=0;
name[0]=0;//字符数组进行初始化
department[0]=0;//字符数组进行初始化
age=0;
salary=0;
}
EMPLOYEE::EMPLOYEE(long idd,char namee[],char depar[],int agee,double sala)
{
id=idd;
age=agee;
salary=sala;
strcpy(name,namee);
strcpy(department,depar);
}
EMPLOYEE::~EMPLOYEE()
{
}
void EMPLOYEE::changeDepartment(char NewDepar[])
{
department[80]=NewDepar[80];
}
void EMPLOYEE::changeAge(int Agee)
{
age=Agee;
}
void EMPLOYEE::raiseSalary(double delta)
{
salary=delta;
}
ostream&operator<<(ostream&stream,EMPLOYEE&obj)
{
stream<<"工号:"<<obj.id<<endl;
stream<<"姓名:"<<obj.name<<endl;//输出一个数组的操作
stream<<"年龄:"<<obj.age<<endl;
stream<<"部门:"<<obj.department<<endl;//同上,输出数组
stream<<"工资:"<<obj.salary<<endl;
return stream;
}
istream&operator>>(istream&stream,EMPLOYEE&obj)
{
cout<<"请输入工号:";
stream>>obj.id;
cout<<"请输入名字:";
stream>>obj.name;
cout<<"输入部门:";
stream>>obj.department;
cout<<"输入年龄:";
stream>>obj.age;
cout<<"输入工资:";
stream>>obj.salary;
return stream;
}
int main( )
{
EMPLOYEE A(200701, "Tom","DEPARTMENT A",34,2345.6);
cout<<A;
cout<<"----------------------------"<<endl;
EMPLOYEE B;
cin>>B;
cout<<"输入的信息:"<<B<<endl;
system("pause");
return 0;
}
//本代码并非完美,有些不足之处,希望大家能自己去尝试改正
运行结果
本代码存在两个warning,希望大家自行改正
有问题私聊博主或者在下面留言,如果有更好的解法也请留言,欢迎大家讨论,共同进步,一起学习。
“C++是面向对象编程,我也想面向对象编程。”
以上是关于C++试题精选----I/O----NO.1的主要内容,如果未能解决你的问题,请参考以下文章