2021-04-21

定义一个人员类,输出其信息并且调用构造函数以及拷贝函数

#include<iostream>
#include<cstring>     //这里运用了string函数的头文件
using namespace std;
class people   //这里定义了一个people类
{
private:
    string id; string name; string sex;       //这里包含了四个属性
    struct birth
    {
        int year; int month; int day;
    }b1;           //这里定义了一个birth的结构体类型,里面包含了三个元素
    string homeadd;
public:     //定义了两个公共成员函数
    people(string i,string n,string s,string h)       //这里定义了有参构造函数
    {
         id = i;
         name = n;
         sex = s;
        cout << "请输入年月日" << endl;
        cin >> b1.year >> b1.month >> b1.day;
         homeadd = h;
        

    }
    void output();        //成员函数output输出人员信息

  1. };

int main()
{
    people p("123","张安","男","江西南昌");     //调用了有参构造函数
    p.output();
    people p1(p);     //对象作为函数参数传递来复制(拷贝)构造函数
    cout << "这是拷贝后的" << endl;
    p1.output();
}
void people::output()              //在调用类的公共函数时需要在子函数类型名后面加上people::
{
    cout << "id:" << id <<'\t'<< "name:" << name<<'\t' << "sex:" << sex << endl;
    cout << "birth:" << b1.year << '.' << b1.month << '.' << b1.day << endl;
    cout<< "homeadd" << homeadd << endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值