关于#c++#的问题:其中构造函数有默认赋值,ChangeName可以修改姓名、部门和年龄, Display进行输出显示, 析构函数把姓名等信息清空,dev c++可运行即可

创建一个Employee类,该类中有姓名、部门和年龄,请构造带有默认参数的构造函数、ChangeName( )、Display( )、析构函数。其中构造函数有默认赋值,ChangeName可以修改姓名、部门和年龄, Display进行输出显示, 析构函数把姓名等信息清空,

img


dev c++可运行即可,需要.cpp和.h的

Employee.h

#include<iostream>
#include<string>
using namespace std;
class Employee{
    public:
        Employee(string nam="xiaoming",string dep="security",int age1=18)
        {
            name=nam;
            dept=dep;
            age=age1;
        }
        void ChangeName(string name1,string dept1,int age1)
        {
            name=name1;
            dept=dept1;
            age=age1;
        }
        void Display()
        {
            cout<<"姓名:"<<name<<endl<<"部门"<<dept<<endl<<"年龄"<<age<<endl;
        }
        ~Employee()
        {
            cout<<"over "<<endl;
        }
    private:
        string name;
        string dept;
        int age;
};

main.cpp

#include"Employee.h"
int main()
{
    Employee employee1;
    employee1.Display();
    employee1.ChangeName("libai","xiaoshou",30);
    employee1.Display();
    return 0;
}

#pragma once
#ifndef _EMPLOYEE_
#define _EMPLOYEE_
#include<string>
#include<iostream>
using namespace std;
class Employee
{
private:
    string name;
    string dept;
    int age;
public:
    Employee();
    void ChangeName(const string &name,const string &dept,int age);
    void display();
    ~Employee();
};
Employee::Employee()
{
    name = dept = "";
    age = 0;
}
inline void Employee::ChangeName(const string& name, const string& dept, int age)
{
    this->name = name;
    this->dept = dept;
    this->age = age;
}
inline void Employee::display()
{
    cout << name << "\t" << dept << "\t" << age << endl;
}
inline Employee::~Employee()
{
    name = dept = "";
    age = 0;
}
#endif // !_EMPLOYEE_