当前位置: 首页 > news >正文

C++ day4

目录

思维导图

定义一个Person类,私有成员int age,string &name,定义一个Stu类,包含私有成员double *score,写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数,完成对Person的运算符重载(算术运算符、条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)


思维导图

定义一个Person类,私有成员int age,string &name,定义一个Stu类,包含私有成员double *score,写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数,完成对Person的运算符重载(算术运算符、条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)

#include <iostream>using namespace std;
class Person
{int age;string &name;
public:Person(int age,string &name):age(age),name(name){cout << "Person的构造函数" << endl;}~Person(){cout << "Person的析构函数"<< endl;}Person(const Person &other):age(other.age),name(other.name){cout << "Person的拷贝构造函数" << endl;}Person &operator=(const Person &other){this->age=other.age;this->name=other.name;cout << "Person的拷贝赋值函数" << endl;return *this;}Person operator+(Person &other);Person operator-(Person &other);Person operator*(Person &other);Person operator/(Person &other);friend Person operator%(Person &c1,Person &c2);friend bool operator>(Person &c1,Person &c2);friend bool operator<(Person &c1,Person &c2);friend bool operator==(Person &c1,Person &c2);friend bool operator>=(Person &c1,Person &c2);friend bool operator<=(Person &c1,Person &c2);friend bool operator!=(Person &c1,Person &c2);friend bool operator&&(Person &c1,Person &c2);friend bool operator||(Person &c1,Person &c2);void operator()();friend Person operator++(Person &c1);friend Person operator--(Person &c1);Person operator++(int);Person operator--(int);friend ostream &operator<<(ostream &out,Person &c1);friend istream &operator>>(istream &out,Person &c1);
};
string n="";
Person temp(0,n);
//+运算符重载
Person Person::operator+(Person &other)
{temp.age = this->age+other.age;temp.name=this->name;return temp;
}
//-运算符重载
Person Person::operator-(Person &other)
{temp.age = this->age-other.age;temp.name=this->name;return temp;
}
//*运算符重载
Person Person::operator*(Person &other)
{temp.age = this->age*other.age;temp.name=this->name;return temp;
}
//  /运算符重载
Person Person::operator/(Person &other)
{temp.age = this->age/other.age;temp.name=this->name;return temp;
}
//%运算符重载
Person operator%(Person &c1,Person &c2)
{temp.age=c1.age%c2.age;temp.name=c1.name;return temp;
}
//>运算符重载
bool operator>(Person &c1,Person &c2)
{return c1.age>c2.age;
}
//<运算符重载
bool operator<(Person &c1,Person &c2)
{return c1.age<c2.age;
}
//==运算符重载
bool operator==(Person &c1,Person &c2)
{return c1.age==c2.age;
}
//>=运算符重载
bool operator>=(Person &c1,Person &c2)
{return c1.age>=c2.age;
}
//<=运算符重载
bool operator<=(Person &c1,Person &c2)
{return c1.age<=c2.age;
}
//!=运算符重载
bool operator!=(Person &c1,Person &c2)
{return c1.age!=c2.age;
}
//&&运算符重载
bool operator&&(Person &c1,Person &c2)
{return c1.age&&c2.age;
}
//||运算符重载
bool operator||(Person &c1,Person &c2)
{return c1.age||c2.age;
}
//()运算符重载
void Person::operator()()
{cout << "hello" << endl;
}
//前自增
Person operator++(Person &c1)
{++(c1.age);return c1;
}
//前自减
Person operator--(Person &c1)
{--(c1.age);return c1;
}
//后自增
Person Person::operator++(int)
{temp.age=this->age++;temp.name=this->name;return temp;
}
//后自减
Person Person::operator--(int)
{temp.age=this->age--;temp.name=this->name;return temp;
}
//cout运算符重载
ostream &operator<<(ostream &out,Person &c1)
{out << c1.age << " " << c1.name;return out;
}
//cin运算符重载
istream &operator>>(istream &in,Person &c1)
{in >> c1.age;in >> c1.name;return in;
}class Stu
{double *score;
public:Stu(double score):score(new double(score)){cout << "Stu的构造函数" << endl;}~Stu(){delete score;cout << "Stu的析构函数"<< endl;}Stu(const Stu &other):score(new double(*(other.score))){cout << "Stu的拷贝构造函数" << endl;}Stu &operator=(const Stu &other){*(this->score)=*(other.score);cout << "Stu的拷贝赋值函数" << endl;return *this;}
};
int main()
{string name="zhangsan";Person s1(12,name);return 0;
}

相关文章:

  • MoE模型性能还能更上一层楼?一次QLoRA微调实践
  • 【蓝方】黑客笔记日常(1):受病毒攻击的最好处理方法
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • 超级详细Git操作 之git log 命令的参数详解
  • 详解Oracle数据库的启动
  • 使用 Process Explorer 和 Windbg 排查软件线程堵塞问题
  • C++ Qt开发:Charts与数据库组件联动
  • 北斗卫星助力智慧公园实现智能化管理
  • 基于Java+SpringBoot+vue+elementUI私人健身教练预约管理系统设计实现
  • 0111qt
  • 【Docker Compose】案例分享
  • maven的scop作用域依赖问题导致idea社区版报错
  • 【椒盐玉兔】GPTs Store 商店的TOP100 自定义GPT使用报告
  • 数据结构栈、队列、链表、散列表
  • js_BOMDomAjax
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • Computed property XXX was assigned to but it has no setter
  • go append函数以及写入
  • iOS 系统授权开发
  • mysql innodb 索引使用指南
  • Redis中的lru算法实现
  • sessionStorage和localStorage
  • socket.io+express实现聊天室的思考(三)
  • Windows Containers 大冒险: 容器网络
  • 关于List、List?、ListObject的区别
  • 欢迎参加第二届中国游戏开发者大会
  • 记录一下第一次使用npm
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 数据结构java版之冒泡排序及优化
  • 我从编程教室毕业
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • #1015 : KMP算法
  • #数学建模# 线性规划问题的Matlab求解
  • #我与Java虚拟机的故事#连载15:完整阅读的第一本技术书籍
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (3)STL算法之搜索
  • (4)(4.6) Triducer
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (独孤九剑)--文件系统
  • (二)JAVA使用POI操作excel
  • (二)学习JVM —— 垃圾回收机制
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (九)One-Wire总线-DS18B20
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (四)模仿学习-完成后台管理页面查询
  • (算法)Game
  • (一)Thymeleaf用法——Thymeleaf简介
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • **PHP二维数组遍历时同时赋值
  • .NET Core IdentityServer4实战-开篇介绍与规划