前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++重载运算符练习--对people类重载“= =”运算符和“=”运算符

C++重载运算符练习--对people类重载“= =”运算符和“=”运算符

作者头像
Enterprise_
发布于 2018-05-18 07:23:43
发布于 2018-05-18 07:23:43
3.2K20
代码可运行
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆
运行总次数:0
代码可运行
  1. 题目描述 对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等;“=”运算符实现people类对象的赋值操作。
  2. 代码如下
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<iostream>
#include<string>
using namespace std;
class Data{
public:
    Data(){}
    Data(int yy,int mm,int dd){
    year=yy;
    month=mm;
    day=dd;
    }
    Data(Data &ap){
        year=ap.year;
        month=ap.month;
        day=ap.day;
    }
    ~Data(){
    }
    int get_year(){
        return year;
    }
    int get_month(){
        return month;
    }
    int get_day(){
        return day;
    }
    void set_year(int y){
        year=y;
    }
    void set_month(int m){
        month=m;
    }
    void set_day(int d){
        day=d;
    }
private:
    int year;
    int month;
    int day;
};
class People{
public:
    People(int num,string se,Data birth,string iid):birthday(birth){
        number=num;
        sex=se;
        id=iid;
    }
    People(People &tp){
        number=tp.get_number();
        sex=tp.get_sex();
        id=tp.get_id();
        birthday=tp.get_birthday();
    }
    People(){}
    People get_People(){
        int num,yy,mm,dd;
        string ID,se;
        cout<<"Please enter the number of the People:"<<endl;
        cin>>num;
        cout<<"Please enter the sex:(male or female)"<<endl;
        cin>>se;
        cout<<"Please enter the birthday:"
            <<"(Warning the format is 1998 8 3.)"<<endl;
        cin>>yy>>mm>>dd;
        cout<<"Please enter the id:"<<endl;
        cin>>ID;
        Data birth(yy,mm,dd);
        id=ID;
        number=num;
        sex=se;
        birthday=birth;
        return *this;
    }
    ~People(){}
    void set_number(int num){
        number=num;
    }
    void set_sex(string se){
        sex=se;
    }
    void set_birhtday(Data birth){
        birthday=birth;
    }
    void set_id(string iidd){
        id=iidd;
    }
    inline int get_number(){
        return number;
    }
    inline string get_sex(){
        return sex;
    }
    Data get_birthday()
    {
        return birthday;
    }
    inline string get_id(){
        return id;
    }
    void details(){
        cout<<"Number:"<<number<<endl;
        cout<<"Sex:"<<sex<<endl;
        cout<<"Birthday:"<<birthday.get_year()<<"/"<<birthday.get_month()<<"/"<<birthday.get_day()<<endl;
        cout<<"ID:"<<id<<endl;
    }
    People& operator=(const People &p1){
        if(this==&p1){
            return *this;
        }
        number=p1.number;
        sex=p1.sex;
        birthday=p1.birthday;
        id=p1.id;
    }
private:
    int number;
    string sex;
    Data birthday;
    string id;
    friend bool operator== (const People &p1,const People &p2){
        if(p1.id==p2.id)
            return true;
        else
            return false;
    }
};
int main()
{
    People asp,tcp,tmp;
    asp.get_People();
    asp.details();
    tcp.get_People();
    tcp.details();
    if(asp==tcp)
        cout<<"The id of two people is common!!!"<<endl;
    else
        cout<<"The id of two people is different!!!"<<endl;
    tmp=asp;
    tmp.details();
    return 0;
}
  1. 测试截图
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年05月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
2 条评论
热度
最新
还有替他的方法吗?
还有替他的方法吗?
回复回复点赞举报
很好的解决了我的问题,谢谢楼主分享!
很好的解决了我的问题,谢谢楼主分享!
回复回复点赞举报
推荐阅读
编辑精选文章
换一批
C++创建People类--练习
题目描述 /*设计一个用于人事管理的People(人员)类。*/ /* 考虑到通用性,这里只抽象所有类型人员都具有的属性: number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。 其中“出生日期”定义为一个“日期”类内嵌子对象。 用成员函数实现对人员信息的录入和显示。 要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、聚集。 */ 代码如下 #include<iostream> #include<string> using namespace std; class
Enterprise_
2018/05/18
1.3K0
C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等
从people(人员)类派生出student(学生)类 添加属性:班号char classNO[7];从people类派生出teacher(教师)类, 添加属性:职务char principalship[11]、部门char department[21]。 从student类中派生graduate(研究生)类,添加属性:专业char subject[21]、 导师char teacher_adviser[21];从graduate类和teacher类派生出TA(助教生)类, 注意虚基类
Enterprise_
2018/05/18
2.6K0
C++新闻检索类
研究长字符串快速全文检索技术,实现某电力公司新闻中心新闻稿件全文检索统计系统。 1、 设计实现适合新闻稿件的基础类库 2、 新闻稿件全文检索功能实现 3、 新闻稿件按照关键字统计查询 代码如下 Paper.h #pragma once #ifndef PAPER_H // we're here only if PAPER_H has not yet been defined #define PAPER_H // Definition of PAPER_H class and relate
Enterprise_
2019/02/21
4830
C++创建学生类练习
/*作业,定义一个学生类*/ /*数据成员:学号、姓名、数学、英语、计算机三科成绩 *成员函数:求总成绩、求三科平均成绩、输出学生信息 *新增一个生日类 2018.4.2 */ #include <iostream> #include <string> using namespace std; class Data { public: Data(); Data(int ye, int mon, int da); Data(Data &da); void inf(); p
Enterprise_
2018/05/18
7370
C++实验报告
C++面向对象程序设计实验报告实验1 编程环境的熟悉及简单程序的编制1.3.1 任务一1.3.1 任务二
用户7886150
2021/02/08
1.3K0
C++课本的练习题及答案(第五章)
1.语句  cout<<(1&2)<<","<<(1&&2)<<endl;  的输出结果是(    )。
Twcat_tree
2022/11/29
8760
人事管理系统(数据结构课程设计)
这道题目中,我多加了一个板块,是系统板块,大致是原本我们只需要设计一个人事管理系统,但我觉得系统应该多个,所以我在外层加了一个系统菜单,其次这道题目比较麻烦的有两点,一点是员工的信息太多了,敲的手都酸死,第二点是按薪水进行排序,我用的直接快速排序,时间复杂度为0n(2)。其次我还学到一个新的知识点:为什么不能给结构体指针中的string变量用“=”赋值(更新),也不能输出,但能通过编译??这个问题我在题目中遇到一模一样的,答案是:这里得用new,因为里面有string需要初始化malloc只是申请内存,是无法调用string类型里的初始化函数没有经过初始化的string,在程序执行到这里时出错:node->name =”a”;,所以,在代码中我放弃了malloc,而直接用new,关于new也是新学到的知识点,这里有必要写个笔记。
废江_小江
2022/09/05
1.1K0
人事管理系统(数据结构课程设计)
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
Rossy Yan
2024/12/24
950
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
C++ 日期类Date的实现(运算符重载)
该类综合考察了关于类的基本操作,包括构造,拷贝构造,析构,运算符重载等方面的知识。
用户7886150
2021/02/08
1.3K0
C++运算符重载(五)之关系运算符重载
正常情况下如果想比较两个日期大小是无法实现的,这是因为运算符默认都是给内置类型用的。
CtrlX
2022/09/21
7480
模拟赛 2018 Benelux Algorithm Programming Contest (BAPC 18)(部分题)
After the festive opening of your new store, the Boutique store for Alternative Paramedicine and Cwakhsahlvereigh, to your disappointment you find out that you are not mak- ing as many sales as you had hoped. To remedy this, you decide to run a special offer: you will mark some subset of the n items for sale in your store as participating in the offer, and if people buy exactly two of these items, and the cost of these items is strictly more than X euros, you will give them a free complimentary unicorn horn! Since you recently found out all your unicorn horns are really narwahl tusks, you decide to rig the offer by picking the participating items in such a way that no one can earn a horn anyway. To make sure no one becomes suspicious, you want to mark as many items as possible as participating in the offer. Input • On the first line two integers, 1 ≤ n ≤ 10 5 , the number of items for sale in your store, and 1 ≤ X ≤ 10 9 , the minimum cost specified in the statement. • On the second line n positive integers, each at most 10 9 . These are the prices of the items in the store. Output Print the maximum number of items you can mark as part of your special offer, without anyone actually being able to receive a horn. Sample Input 1 Sample Output 1 5 6 1 2 3 4 5 3 Sample Input 2 Sample Output 2 5 10 4 8 1 9 7 2 Sample Input 3 Sample Output 3 4 10 1 3 1 7 4 4 Problem A: A Prize No One Can Win Sample Input 4 Sample Output 4 1 5 6 1
glm233
2020/09/28
4190
C++课程设计,12306模拟写起来就是这么粗暴
一、12306应该具备那些功能 1.查询(一个月以内的): 1.查车票:出发地+目的地+出发时间->显示经过两站车票信息 (余票,车次信息可直接查询直接打印,是否为过路车,历时不是总历时,是两站间历时)(只有这种查询方式能查询余票) 2.车站车次查询: 车站+出发时间段+到达时间段->打印始发站与终点站信息(车站与时间)。 (查询的的车站即作为始发站,又作为终点站) 3.车次查询:车次+出发日期->打印该车次详细信息 (经过站点序号,站名,到时,发时,停留,终点站,起始站,历时) 2.操作 1.购票(某个区间的票-1)一天限购五张,一个月以内的。 2.退票 (某个区间的票+1) 火车开了之后,不能退票,只能改签。 3.改签(相当于退票+从新购票)一张票仅能改签一次 4.刷新 相当于重新读入 5.换乘 两个城市不能接联通,需要借助最短,需要找出中转城市,可能一个在佳木斯,一个在宜宾,这不一定是换乘一次,只要有火车站无非是换乘的次数多一点,反正能到(间隔大于10min)。 3.管理员操作 1.增删查改站点(查)可以继承一个查询类 票一旦起售,不可修改站点,虽然没怎么见过火车站被拆得,但是还是有,但是拆除不可能是突然的拆除,所以会提前收到消息,先停售,再删除,修改名称可以直接修改。站点到站时间只能修改一个月以后的。 2.放票 无特殊需要,无需操作,每天的列车表都是一样的。 3.增加车次 (没有特殊要求,国家铁路局规定站点名唯一,车次名唯一) 4.用户端操作 1.各种查询 2.买退改,重点是换乘(这个不一定好写) 2.查询已买到的票 二、根据数据写出大体思路
风骨散人Chiam
2020/10/28
7240
C++课程设计详解-12306的模拟实现
设计一个系统,首先要考虑实现什么功能,需要怎样组织数据。所以先通过查询12306,知道了12306最终要实现:
风骨散人Chiam
2020/10/28
7170
C++运算符重载(四)之赋值运算符重载
如果指针不为空就释放指针指向的堆区内存,并且让指针的指向改为NULL,防止之后误操作。
CtrlX
2022/09/21
8620
C++(运算符重载+赋值拷贝函数+日期类的书写)
由于赋值操作我们改变的是调用这个函数的对象,所以我们在参数中可以加上cosnt修饰,传值和传引用,我们选择传引用,最后返回也返回引用,这样可以避免调用拷贝构造函数
用户11305458
2024/10/09
1060
C++(运算符重载+赋值拷贝函数+日期类的书写)
日期比较(运算符重载之类型转换)
构造函数含单个参数,参数是八位整数,默认值为0,实现整数转为日期类型,例如参数为20170612,转为year=2017, month=6, day=12
叶茂林
2023/07/30
1520
新旧身份证(继承)
然后以COldID为基类派生18位身份证号的新身份证类CNewID,并增加3个数据成员:p_id18(18位号码)、issueday(签发日期)和validyear(有效期,年数),并重新定义check()和print()。
叶茂林
2023/07/30
3370
新旧身份证(继承)
银行卡信息录入系统(一)之 宏定义的应用
设计一套代码,将结构体声明放在自定义头文件中,在主程序中实现对800到801个人的信息录入,并将最后一个录入的人员信息打印出来。人员信息包括卡主姓名、性别、卡号、身份证号、余额、出生日期、发卡日期等。
Reborn Lee
2020/06/29
5810
C++——拷贝构造和 运算符重载
开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第13天,点击查看活动详情
lovevivi
2022/12/22
3290
C++——拷贝构造和 运算符重载
【c++】类和对象(五)赋值运算符重载
但是这里是全局的定义的operator==,这里会发现运算符重载成全局的就需要成员变量是公有的,即我的成员不能是private私有的,那么封装性如何保证?
用户11029103
2024/04/02
2290
【c++】类和对象(五)赋值运算符重载
相关推荐
C++创建People类--练习
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验