C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
- 题目描述
对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等;“=”运算符实现people类对象的赋值操作。 - 代码如下
#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;
}
- 测试截图
C++重载运算符练习--对people类重载“= =”运算符和“=”运算符的更多相关文章
- C++11运算符重载详解与向量类重载实例(<<,>>,+,-,*等)
1. C++运算符重载介绍 C ++ 中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作.这时就必须在C ++ 中重新定义这些运算符,赋予已 ...
- YTU 2443: C++习题 复数类--重载运算符3+
2443: C++习题 复数类--重载运算符3+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 1368 解决: 733 题目描述 请编写程序,处理一个复数与一个double数相加的运 ...
- YTU 2441: C++习题 复数类--重载运算符2+
2441: C++习题 复数类--重载运算符2+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 847 解决: 618 题目描述 定义一个复数类Complex,重载运算符"+ ...
- YTU 2440: C++习题 复数类--重载运算符+,-,*,/
2440: C++习题 复数类--重载运算符+,-,*,/ 时间限制: 1 Sec 内存限制: 128 MB 提交: 1189 解决: 774 题目描述 定义一个复数类Complex,重载运算符& ...
- YTU 2439: C++习题 复数类--重载运算符+
2439: C++习题 复数类--重载运算符+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 1022 解决: 669 题目描述 定义一个复数类Complex,重载运算符"+ ...
- ostream类重载的operator<<()函数
ostream类重载了operator<<()以识别不同的类型,如: int short long unsigned int unsigned short unsigned long f ...
- 初探C++运算符重载学习笔记<2> 重载为友元函数
初探C++运算符重载学习笔记 在上面那篇博客中,写了将运算符重载为普通函数或类的成员函数这两种情况. 以下的两种情况发生.则我们须要将运算符重载为类的友元函数 <1>成员函数不能满足要求 ...
- php面向对象 封装继承多态 接口、重载、抽象类、最终类总结
1.面向对象 封装继承多态 接口.重载.抽象类.最终类 面向对象 封装继承多态 首先,在解释面向对象之前先解释下什么是面向对象? [面向对象]1.什么是类? 具有相同属性(特征)和方法(行为)的一 ...
- 15.C++-操作符重载、并实现复数类
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
随机推荐
- OpenGL学习--03--矩阵
Model--View--Projection 1.tutorial03.cpp // Include standard headers #include <stdio.h> #inclu ...
- mkdir failed for img Read-only file system
最简单的方法就是打开模拟起,然后 windows-->show view-->file explorer-->mnt-->sdcard (最好在该目录下重新创建个文件夹)选中文 ...
- ida不错的插件记录
IDASkins 地址 https://github.com/zyantific/IDASkins 作用 ida黑色皮肤插件 IDAFuzzy 地址 https://github.com/Ga-ryo ...
- web.xml里welcome-file欢迎页面配置及web.xml简介
web项目欢迎页面的配置 <welcome-file-list> <welcome-file>/WEB-INF/index.html</welcome-file> ...
- zookeeper应用 - FIFO 队列 分布式队列
使用ZooKeeper实现的FIFO队列,这个队列是分布式的. package fifo; import java.util.Collections; import java.util.List; i ...
- jsp到java后台中文乱码问题
---首先描述一下我的情况,我的jsp 设置了编码格式 <%@ page language="java" contentType="text/html; ch ...
- apache ftp server的简单入门(数据库验证)
apache的简单校验分为两种,一直是前面提到的properties的校验,具体参考:apache ftp server的简单入门(properties验证) 今天来说一种数据库的校验,这种方式在项目 ...
- 使用 Azure CLI 管理 Azure 磁盘
Azure 虚拟机使用磁盘来存储 VM 操作系统.应用程序和数据. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置. 本教程介绍如何部署和管理 VM 磁盘. 学习内容: OS 磁盘和临时 ...
- Flask的数据库连接池 DBUtils
Flask是没有ORM的操作的,如果在flask中连接数据库有两种方式 一.pymysql 二.SQLAlchemy 是python操作数据库的以一个库,能够进行orm映射官网文档 sqlchemy ...
- 铁乐学Python_day03-字符串常用操作方法
文:铁乐与猫 2018-3-20 1)字符串首个字母大写,其它字母也会转换成小写: S.capitalize() -> str 记忆方法:capital(大写字母) def capitalize ...