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.hqwc.cn/news/325059.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

canvas设置图形的阴影

查看专栏目录 canvas示例教程100专栏&#xff0c;提供canvas的基础知识&#xff0c;高级动画&#xff0c;相关应用扩展等信息。canvas作为html的一部分&#xff0c;是图像图标地图可视化的一个重要的基础&#xff0c;学好了canvas&#xff0c;在其他的一些应用上将会起到非常重…

微信小程序:flex布局实现换行

1、关键代码.wxml&#xff1a; <view class"pay margin-top-40"><view class"info"><view class"pay-info-title margin-left-22 flex-start"> 请选择充值金额</view><view class"flex-wrap margin-top-20&quo…

如何在 Ubuntu 20.04 上安装和使用 Docker

前些天发现了一个人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;最重要的屌图甚多&#xff0c;忍不住分享一下给大家。点击跳转到网站。 如何在 Ubuntu 20.04 上安装和使用 Docker 介绍 Docker是一个可以简化容器中应用程序进程管理过程的应用程序。…

数字后端设计实现 | 数字后端PR工具Innovus中如何创建不同高度的row?

吾爱IC社区星球学员问题&#xff1a;Innovus后端实现时两种种不同高度的site能做在一个pr里面吗&#xff1f; 答案是可以的。 Innovus支持在同一个设计中中使用不同的row&#xff0c;但需要给各自子模块创建power domain。这里所说的不同高度的row&#xff0c;有两种情况。 1…

基础面试题整理1

1.面向对象的特点 继承&#xff08;复用性&#xff09;、封装&#xff08;复用性&#xff09;、多态&#xff08;可移植性、灵活性&#xff09; 2.ArrayList与LinkedList区别 ArrayList和LinkedList都是实现了List接口 ArrayList底层是动态数组 LinkedList底层是链表&#…

【EI会议征稿通知】2024年人工智能与电力系统国际学术会议(AIPS 2024)

2024年人工智能与电力系统国际学术会议&#xff08;AIPS 2024&#xff09; 2024 International Conference on Artificial Intelligence and Power System 2024年人工智能与电力系统国际学术会议 (AIPS 2024) 将于2024年04月19日-21日在中国成都召开。AIPS 2024将围绕“人工智…

Python实现某城市从站点API获取天气状况示例(Crossin教室实例24)

一、要点说明&#xff1a; 根据站点当前API数据是由‘\r’字符连接的字符串的特点&#xff0c;主要用到了字符串的split()方法。此方法参数就是‘\r’。函数返回值是被分隔的字符串的列表。通过使用列表索引就可以分项取到天气数据。 二、示例代码&#xff1a; import reque…

操作系统期末复习知识点

目录 一.概论 1.操作系统的介绍 2.特性 3.主要功能 4.作用 二.进程的描述与控制 1.进程的定义 2.特性 3.进程的创建步骤 4.基本状态转化 5.PCB的作用 6.进程与线程的比较 三.进程同步 1.同步的概念&#xff08;挺重要的&#xff09; 2.临界区 3.管程和进程的区…

Spring Boot 整合 Caffeine 本地缓存及 Spring Cache 注解的使用

Spring Boot 整合 Caffeine 本地缓存及 Spring Cache 注解的使用 介绍 在现代的Web应用程序中&#xff0c;缓存是提高性能和响应速度的重要手段之一。Spring Boot提供了对缓存的良好支持&#xff0c;并且可以轻松地整合Caffeine本地缓存作为缓存提供者。结合Spring Cache注解…

DDIA 第十一章:流处理

本文是《数据密集型应用系统设计》&#xff08;DDIA&#xff09;的读书笔记&#xff0c;一共十二章&#xff0c;我已经全部阅读并且整理完毕。 采用一问一答的形式&#xff0c;并且用列表形式整理了原文。 笔记的内容大概是原文的 1/5 ~ 1/3&#xff0c;所以你如果没有很多时间…

RocketMQ MQClientInstance、生产者实例启动源码分析

&#x1f52d; 嗨&#xff0c;您好 &#x1f44b; 我是 vnjohn&#xff0c;在互联网企业担任 Java 开发&#xff0c;CSDN 优质创作者 &#x1f4d6; 推荐专栏&#xff1a;Spring、MySQL、Nacos、Java&#xff0c;后续其他专栏会持续优化更新迭代 &#x1f332;文章所在专栏&…

C语言编译器(C语言编程软件)完全攻略(第二十四部分:Turbo C 2.0使用教程(使用Turbo C 2.0编写C语言程序))

介绍常用C语言编译器的安装、配置和使用。 二十四、Turbo C 2.0使用教程&#xff08;使用Turbo C 2.0编写C语言程序&#xff09; 首先&#xff0c;我们给出一段完整的C语言代码&#xff1a; #include <stdio.h> int main() { puts("hello&#xff0c;world!"…