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

C ++类

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

#include <iostream>using namespace std;class person
{int age;string &name;
public:person(string &a):age(18),name(a){}person(int a,string &b):age(a),name(b){}~person(){}person(const person &other):age(other.age),name(other.name){}person &operator=(const person&other){this->age=other.age;this->name=other.name;return *this;}void show(){cout<<"age:"<<age<<endl<<"name:"<<name<<endl;}person operator+(const person&other){int age=this->age+other.age;static string name;name = this->name+other.name;return person(age,name);}bool operator==(person &other){if((this->age==other.age)&&(this->name==other.name))return true;elsereturn false;}bool operator>(person &other){if(this->age>other.age)return true;elsereturn false;}bool operator&&(person &other){return (this->age&&other.age)||(this->name==other.name);}int getage(){return age;}friend ostream &operator<<(ostream &out,person&c1);friend istream &operator>>(istream &in,person &c1);
};
ostream &operator<<(ostream &out,person&c1)
{out<<"age:"<<c1.age<<endl<<"name:"<<c1.name<<endl;return out;
}
istream &operator>>(istream &in,person &c1)
{in>>c1.age>>c1.name;return in;
}
class stu
{double *score;person p1;
public:stu(string &a):score(new double(12.5)),p1(a){}stu(double a,string &b):score(new double(a)),p1(b){}~stu(){delete score;}stu(const stu&other):score(new double(*(other.score))),p1(other.p1){}stu &operator=(const stu&other){*(this->score)=*(other.score);this->p1=other.p1;return *this;}void show(){cout<<"score:"<<*score<<endl;p1.show();}
};
int main()
{string a="zhangsan";string b="lisi";string c="wangwu";stu a1(a);a1.show();cout<<endl;person p1(a);cout<<p1<<endl;person p2(b);cout<<p2<<endl;person p3(c);cout<<p3<<endl;person p4=(p1+p2);p4.show();cout<<endl;cout<<(p1==p2)<<endl;cout<<(p1>p2)<<endl;cout<<(p1&&p2)<<endl;return 0;
}

http://www.lryc.cn/news/274138.html

相关文章:

  • STM32疑难杂症
  • GIT使用简介
  • easycode 插件配置文件
  • elasticsearch系列四:集群常规运维
  • 6.6 会话与输入事件(三)
  • 【自动化测试总结】优点、场景、流程、项目人员构成
  • 杨中科 ASP.NETCore Rest
  • RTU数据采集终端
  • 双指针--- 数组元素的目标和
  • 你的网站或许不需要前端构建(二)
  • flutter 使用adb 同时连接 多个模拟器
  • 网络四元组
  • [实践总结] 限制正则表达式匹配次数/时间 防止DoS攻击
  • ffmpeg 5.0版本调试 ffmpeg 5.01 static版本
  • 应用在游戏机触摸屏中的触摸感应芯片
  • D-Link DES-108 交换机
  • VIT用于图像分类 学习笔记(附代码)
  • MongoDB Certified Associate Developer 认证考试心得
  • 基于Java车间工时管理系统(源码+部署文档)
  • 2024.1.5
  • 水库大坝安全监测设计与施工经验
  • 媒体捕捉-拍照
  • Typora+PicGo+Gitee构建云存储图片
  • 【话题】ChatGPT等大语言模型为什么没有智能2
  • 通过大量生物、地球、农业、气象、生态、环境科学领域中案例,一起探索如何优雅地使用大模型吧!
  • slf4j+logback源码加载流程解析
  • KVM虚拟机部署K8S重启后/etc/hosts内容丢失
  • Redis使用场景(五)
  • 【UnityShader入门精要学习笔记】(2)GPU流水线
  • CSS免费在线字体格式转换器 CSS @font-face 生成器