1. 题目描述
    对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等;“=”运算符实现people类对象的赋值操作。
  2. 代码如下
#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. 测试截图

C++重载运算符练习--对people类重载“= =”运算符和“=”运算符的更多相关文章

  1. C++11运算符重载详解与向量类重载实例(<<,>>,+,-,*等)

    1. C++运算符重载介绍 C ++ 中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作.这时就必须在C ++ 中重新定义这些运算符,赋予已 ...

  2. YTU 2443: C++习题 复数类--重载运算符3+

    2443: C++习题 复数类--重载运算符3+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1368  解决: 733 题目描述 请编写程序,处理一个复数与一个double数相加的运 ...

  3. YTU 2441: C++习题 复数类--重载运算符2+

    2441: C++习题 复数类--重载运算符2+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 847  解决: 618 题目描述 定义一个复数类Complex,重载运算符"+ ...

  4. YTU 2440: C++习题 复数类--重载运算符+,-,*,/

    2440: C++习题 复数类--重载运算符+,-,*,/ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1189  解决: 774 题目描述 定义一个复数类Complex,重载运算符& ...

  5. YTU 2439: C++习题 复数类--重载运算符+

    2439: C++习题 复数类--重载运算符+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1022  解决: 669 题目描述 定义一个复数类Complex,重载运算符"+ ...

  6. ostream类重载的operator<<()函数

    ostream类重载了operator<<()以识别不同的类型,如: int short  long unsigned int unsigned short unsigned long f ...

  7. 初探C++运算符重载学习笔记&lt;2&gt; 重载为友元函数

    初探C++运算符重载学习笔记 在上面那篇博客中,写了将运算符重载为普通函数或类的成员函数这两种情况. 以下的两种情况发生.则我们须要将运算符重载为类的友元函数 <1>成员函数不能满足要求 ...

  8. php面向对象 封装继承多态 接口、重载、抽象类、最终类总结

    1.面向对象 封装继承多态  接口.重载.抽象类.最终类 面向对象 封装继承多态  首先,在解释面向对象之前先解释下什么是面向对象? [面向对象]1.什么是类? 具有相同属性(特征)和方法(行为)的一 ...

  9. 15.C++-操作符重载、并实现复数类

    首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...

随机推荐

  1. spring shiro 集成

    1.向spring项目中添加shiro相关的依赖 <dependency> <groupId>commons-logging</groupId> <artif ...

  2. 【Python】Java程序员学习Python(七)— 文本类详解(字符串、str)

    如果一个女孩子喜欢看龙猫,那么请珍惜她,呵护她 任何一门语言,字符串总是最基本也是最需要掌握的一个变量,想想入门的Hello World,输出的就是字符串. 官方文档:https://docs.pyt ...

  3. Pig类型转换

    users.data的内容如下: lisg 28 75 dengsl 24 88 强制类型转换 users = load '/users.data' fehed = foreach users gen ...

  4. Storm监控文件夹变化 统计文件单词数量

    监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  5. select2加载远程数据示例

    核心js $("#query_pack_code").select2({ language: "zh-CN", allowClear: true, width: ...

  6. LeetCode题解之Rotate Array

    1.题目描述 2.代码 void rotate(vector<int>& nums, int k) { ) return ; && (k / nums.size() ...

  7. linux上设置mysql编码

    linux下设置mysql编码 linux下设置mysql编码 首先查找MySql的cnf文件的位置: [root@flyHome gaoxiang]# find / -iname '*.cnf' - ...

  8. Oracle EBS 配置文件取值

    SELECT op.profile_option_id, tl.profile_option_name, tl.user_profile_option_name, lv.level_id, lv.文件 ...

  9. 安装Linux Centos系统硬盘分区方法

    一.硬盘回顾 无论是安装Windows还是Linux操作系统,硬盘分区都是整个系统安装过程中最为棘手的环节.硬盘一般分为IDE硬盘.SCSI硬盘和SATA硬盘三种,在Linux系统中,IDE接口的硬盘 ...

  10. Ajax 请求下载 Execl 文件

    通过Ajax请求下载Execl 的问题,掉进一个坑里半个多小时,特此来记录一下 . 起初  我误以为是后台的问题,然而调试了一下并不是这样的,也不会报错,且进入了success 函数. 以下的事件及请 ...