根据People类信息重载“==”运算符和“=”运算符,“==”运算符判断两个People类对象的id属性是否相等,“=”运算符实现People类对象的赋值操作

1.问题描述


        根据People类信息重载“==”运算符和“=”运算符,“==”运算符判断两个People类对象的id属性是否相等,“=”运算符实现People类对象的赋值操作。

2.问题分析


        (1)根据People类信息重载“==”运算符和“=”运算符,“==”和“=”区别在于:“==”用于if语句等中作逻辑判断,“=”表示 赋值,为赋值运算符,进行重载时要区分开。

        (2)C++标准中规定:

                “=”运算符只能定义为“类的成员函数” 而“==”定义为类的成员函数或非成员函数都行要区别开格式:“类的成员函数运算符重载”与“非类成员函数运算符重载”   格式区别 在于 “类的成员函数运算符重载” 加了“类名”的限定

        (2.1)运算符重载为 类成员函数 语法格式如下:           

                                返回值类型   类名::operator  运算符形参表){

                                                  函数体;

                                }


         其中:

                 (1)“返回值类型”为函数执行结束后返回的数据类型。

                 (2)operator为关键字,其后为要重载的运算符“+”。

                 (3)参数列表为要操作的 数据类型。

(2.2)运算符重载为 非类成员函数 语法格式如下:           

                                返回值类型   operator  运算符形参表){

                                                  函数体;

                                }


         其中:

                 (1)“返回值类型”为函数执行结束后返回的数据类型。

                 (2)operator为关键字,其后为要重载的运算符“+”。

                 (3)参数列表为要操作的 数据类型。

3.代码实现

#include<iostream>
#include<string.h>
using namespace std;
 
class Date{
	public:
		//无参构造
		Date(){}
		//有参构造
		Date(int year,int month,int day){
			this->year=year;
			this->month=month;
			this->day=day;
		}
		//录入信息
		void InDateInformation(int year,int month,int day){
			this->year=year;
			this->month=month;
			this->day=day;
		} 
		//显示信息
		void OutDateInformation(){
			cout<<"出生日期:"<<year<<","<<month<<","<<day;
			cout<<endl;
		} 
		//析构函数
		~Date(){} 
	
	private:
		int year;//年
		int month;//月
		int day;//日 
};


class People{
	public:
		//无参构造函数
		People(){}
		//== 、= 重载声明
		int operator==(const People &p2);
		People & operator=(const People &p);
		
		//信息录入
		void input(){
			int year,month,day;
			cout<<"姓名:";
			cin>>name;
			cout<<"编号:";
			cin>>number;
			cout<<"性别:";
			cin>>sex;
			cout<<"身份证号:";
			cin>>id;
			cout<<"出生日期(用空格隔开):";
			cin>>year;
			cin>>month;
			cin>>day;
			birthday.InDateInformation(year,month,day);
		}
		//信息显示
		void display(){
			cout<<endl
			<<"姓名:"<<name<<endl
			<<"编号:"<<number<<endl
			<<"性别:"<<sex<<endl
			<<"身份证号:"<<id<<endl;
			birthday.OutDateInformation();
			
		}
		//析构函数
		~People(){} 
	private:
		Date birthday;//内嵌日期类对象
		char name[11];//姓名
		char number[7];//编号
		char sex;//性别 (f:女, m:男)
		char id[16];//身份证号
};

//实现 ==重载
int People::operator==(const People &p2){
	int is=strcmp(id,p2.id)==0 ? 1:0;
	return is;
}

//实现 =重载
People & People::operator=(const People &p){
	if(this==&p){
		return *this;
	}
	strcpy(name,p.name);
	strcpy(number,p.number);
	sex=p.sex;
	strcpy(id,p.id);
	birthday=p.birthday;
}

int main(){
	People p1;//创建对象
	People p2;
	cout<<"1.录入1信息:"<<endl;
	p1.input();
	cout<<endl; 
	cout<<"2.录入2信息:"<<endl;
	p2.input();
	cout<<endl;
	if(p1==p2){
		cout<<"1.2 id相同"<<endl;
	}else{
		cout<<"1.2 id不同"<<endl;
	}
	return 0;
}

4.运行结果

 

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等日出看彩虹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值