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++-操作符重载、并实现复数类
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
随机推荐
- View的setTag和getTag方法
---恢复内容开始--- public View getView(int position, View convertView, ViewGroup parent) { Msg msg =getIte ...
- Python 操作数据库pymysql
import pymysql #添加数据 conn , user='root', passwd='', db='yyy') #更改获取数据结果的数据类型,默认是元组,可以改为字典等:#cursor=c ...
- 转载:http://www.cnblogs.com/double-K/p/6926367.html
sql专家感悟:不可说的感悟-——十年老技术转型(一) http://www.cnblogs.com/double-K/p/6926367.html
- npm、webpack、Gulp 中文教程
按顺序阅读 1.npm 模块管理器 2.package.json 文件 3.npm 模块安装机制简介 4.npm scripts 使用指南 5.CommonJS 规范 随着 es6 模块化特性的出现, ...
- B-树特征
在m阶B-树的定义中,要求: 1.树中每个节点至多有m棵子树. 2.若根节点不是叶子节点,则至少有两棵子树. 3.除根之外的所有非终端节点至少有棵子树.
- SQL语句大全教程
创建数据库 CREATE DATABASE DBNAME 删除数据库 DROP DATABASE DBNAME Ø 基本常用查询 --selectselect * from student; --al ...
- [翻译] JFMinimalNotifications
JFMinimalNotifications This is an iOS UIView for presenting a beautiful notification that is highly ...
- Celery学习---Celery 分布式队列介绍及安装
Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, ...
- 基于php-fpm的配置详解
php5.3自带php-fpm/usr/local/php/etc/php-fpm.confpid = run/php-fpm.pidpid设置,默认在安装目录中的var/run/php-fpm.pi ...
- How To create extension in Hybris(创建Hybris的扩展)
How To create extension in Hybris What is an extension? An extension is an encapsulated piece of the ...