C++day4作业

news2025/4/24 1:22:43

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

#include <iostream>
using namespace std;

class Person
{
    int age;
    string &name;
public:
    //构造函数
    void show();
    void show1();
    Person(string &a):name(a){}
    Person(int a,string &b):age(a),name(b){}
    //拷贝构造函数
    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);
    }
    //算数运算符
    Person operator+(Person &other);//类内定义
    friend Person operator+(Person a1,Person a2);//类外定义
    Person operator-(Person &other);//类内定义
    friend Person operator-(Person a1,Person a2);//类外定义
    Person operator*(Person &other);//类内定义
    friend Person operator*(Person a1,Person a2);//类外定义
    Person operator/(Person &other);//类内定义
    friend Person operator/(Person a1,Person a2);//类外定义
    Person operator%(Person &other);//类内定义
    friend Person operator%(Person a1,Person a2);//类外定义
    //条件运算符
    bool operator>(Person &other);//类内定义
    friend bool operator>(Person a1,Person a2);//类外定义
    bool operator>=(Person &other);//类内定义
    friend bool operator>=(Person a1,Person a2);//类外定义
    bool operator<(Person &other);//类内定义
    friend bool operator<(Person a1,Person a2);//类外定义
    bool operator<=(Person &other);//类内定义
    friend bool operator<=(Person a1,Person a2);//类外定义
    bool operator==(Person &other);//类内定义
    friend bool operator==(Person a1,Person a2);//类外定义
    bool operator!=(Person &other);//类内定义
    friend bool operator!=(Person a1,Person a2);//类外定义
    //逻辑运算符
    bool operator&&(Person &other);//类内定义
    friend bool operator&&(Person a1,Person a2);//类外定义
    bool operator||(Person &other);//类内定义
    friend bool operator||(Person a1,Person a2);//类外定义
    //自增自减运算符
    Person operator++(int);//类内定义后置++
    friend Person operator++(Person &other,int);//类外定义后置++
    Person operator++();//类内定义前置++
    friend Person operator++(Person &other);//类外定义前置++
    Person operator--(int);//类内定义后置++
    friend Person operator--(Person &other,int);//类外定义后置--
    Person operator--();//类内定义前置--
    friend Person operator--(Person &other);//类外定义前置--
    //插入提取运算符
    friend ostream &operator<<(ostream &out,Person &other);
    friend istream &operator>>(istream &in,Person &other);


    //析构函数
    ~Person(){}
};

class Stu
{
    double *score;
public:
    void show();
    //构造函数
    Stu(){}
    Stu(double score):score(new double(score)){}

    //拷贝构造函数
    Stu(const Stu& other):score(new double(*other.score)){}
    //拷贝赋值函数
    Stu &operator=(const Stu &other)
    {
        *(this->score) = *(other.score);
        return (*this);
    }

    //析构函数
    ~Stu(){}
};

void Person::show()
{
    cout << "age= " << age << endl;
    cout << "name=" << name << endl;
}
void Person::show1()
{
    cout << "age= " << age << endl;
}

void Stu::show()
{
    cout << "*score= " << *score << endl;
}

Person Person::operator+(Person &other)
{
    string str("zhangsan");
    Person temp(str);
    temp.age=this->age+other.age;
    //temp.name=this->name+other.name;
    return temp;
}
Person operator+(Person a1,Person a2)
{
    string str("aaa");
    Person temp(str);
    temp.age=a1.age+a2.age;
    //temp.name=a1.name+a2.name;
    return temp;
}
Person Person::operator-(Person &other)
{
    string str("zhangsan");
    Person temp(str);
    temp.age=this->age-other.age;
    //temp.name=this->name+other.name;
    return temp;
}
Person operator-(Person a1,Person a2)
{
    string str("aaa");
    Person temp(str);
    temp.age=a1.age-a2.age;
    //temp.name=a1.name+a2.name;
    return temp;
}
Person Person::operator*(Person &other)
{
    string str("zhangsan");
    Person temp(str);
    temp.age=this->age*other.age;
    //temp.name=this->name+other.name;
    return temp;
}
Person operator*(Person a1,Person a2)
{
    string str("aaa");
    Person temp(str);
    temp.age=a1.age*a2.age;
    //temp.name=a1.name+a2.name;
    return temp;
}
Person Person::operator/(Person &other)
{
    string str("zhangsan");
    Person temp(str);
    temp.age=this->age/other.age;
    //temp.name=this->name+other.name;
    return temp;
}
Person operator/(Person a1,Person a2)
{
    string str("aaa");
    Person temp(str);
    temp.age=a1.age/a2.age;
    //temp.name=a1.name+a2.name;
    return temp;
}
Person Person::operator%(Person &other)
{
    string str("zhangsan");
    Person temp(str);
    temp.age=this->age%other.age;
    //temp.name=this->name+other.name;
    return temp;
}
Person operator%(Person a1,Person a2)
{
    string str("aaa");
    Person temp(str);
    temp.age=a1.age%a2.age;
    //temp.name=a1.name+a2.name;
    return temp;
}

bool operator>(Person a1,Person a2)
{

    return a1.age > a2.age;

}
bool Person::operator>(Person &a1)
{

    return this->age > a1.age;
}
bool operator>=(Person a1,Person a2)
{

    return a1.age >= a2.age;

}
bool Person::operator>=(Person &a1)
{

    return this->age >= a1.age;
}
bool operator<=(Person a1,Person a2)
{

    return a1.age <= a2.age;

}
bool Person::operator<=(Person &a1)
{

    return this->age <= a1.age;
}
bool operator<(Person a1,Person a2)
{

    return a1.age < a2.age;

}
bool Person::operator<(Person &a1)
{

    return this->age < a1.age;
}
bool operator==(Person a1,Person a2)
{

    return a1.age == a2.age;

}
bool Person::operator==(Person &a1)
{

    return this->age == a1.age;
}
bool operator!=(Person a1,Person a2)
{

    return a1.age != a2.age;

}
bool Person::operator!=(Person &a1)
{

    return this->age >= a1.age;
}

bool Person::operator&&(Person &other)
{
    return this->age && other.age;
}
bool operator&&(Person a1,Person a2)
{
    return a1.age && a2.age;
}
bool Person::operator||(Person &other)
{
    return this->age || other.age;
}
bool operator||(Person a1,Person a2)
{
    return a1.age || a2.age;
}

Person Person::operator++(int)
{
    string str="zhangsan";
    Person temp(str);
    temp.age = this->age++;
    return temp;
}

Person operator++(Person &other,int)
{
    string str="zhangsan";
    Person temp(str);
    temp.age=other.age++;
    return temp;
}
Person Person::operator++()
{
    ++(this->age);
    return (*this);
}
Person operator++(Person &other)
{
    ++(other.age);
    return other;
}
Person Person::operator--(int)
{
    string str="zhangsan";
    Person temp(str);
    temp.age = this->age--;
    return temp;
}

Person operator--(Person &other,int)
{
    string str="zhangsan";
    Person temp(str);
    temp.age=other.age--;
    return temp;
}
Person Person::operator--()
{
    --(this->age);
    return (*this);
}
Person operator--(Person &other)
{
    --(other.age);
    return other;
}

ostream &operator<<(ostream &out,Person &other)
{
    out << other.age << endl;
    return out;
}
istream &operator>>(istream &out,Person &other)
{
    out >> other.age;
    return out;
}

int main()
{
    cout << "---person构造函数---" << endl;
    string str="shangsan";
    Person a1(1,str);
    a1.show();
    cout << "---person拷贝构造函数---" << endl;
    Person a2=a1;
    a2.show();
    cout << "---person拷贝赋值函数---" << endl;
    Person a3(str);
    a3=a1;
    a3.show();

    cout << "---Stu构造函数---" << endl;
    Stu b1(100);
    b1.show();
    cout << "---stu拷贝构造函数---" << endl;
    Stu b2=b1;
    b2.show();
    cout << "---stu拷贝赋值函数---" << endl;
    Stu b3;
    b3=b1;
    b3.show();

    string str1("hello");
    string str2(" world");
    Person a4(10,str1);
    Person a5(50,str2);
    Person a6=operator+(a4,a5);
    a6.show1();
    a6=operator-(a4,a5);
    a6.show1();
    a6=operator*(a4,a5);
    a6.show1();
    a6=operator/(a4,a5);
    a6.show1();
    a6=operator%(a4,a5);
    a6.show1();
    bool c1=operator>(a4,a5);
    bool c2=a4.operator>(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    c1=operator>=(a4,a5);
    c2=a4.operator>=(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    c1=operator<(a4,a5);
    c2=a4.operator<(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    c1=operator<=(a4,a5);
    c2=a4.operator<=(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    c1=operator==(a4,a5);
    c2=a4.operator==(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    c1=operator!=(a4,a5);
    c2=a4.operator!=(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    c1=operator&&(a4,a5);
    c2=a4.operator&&(a5);
    cout << "c1= " << c1 << endl;
    cout << "c2= " << c2 << endl;
    cout << "aaaa" << endl;
    Person a7=a4.operator++();
    a7.show1();
    a7=operator++(a4);
    a7.show1();
    a7=a5.operator++(10);
    a7.show1();
    a7=operator++(a5,10);
    a7.show1();

    a7=a4.operator--();
    a7.show1();
    a7=operator--(a4);
    a7.show1();
    a7=a5.operator--(10);
    a7.show1();
    a7=operator--(a5,10);
    a7.show1();

    cout<<a4 << a5 << endl;
    operator<<(cout,a4);
    operator<<(cout,a5);

    Person a8(str);
    cin>>a8;
    a8.show1();
    operator>>(cin,a8);
    a8.show1();

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1345719.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Matlab:K-means算法

K-means算法是一种常见的聚类算法&#xff0c;它将一组数据划分为K个不同的簇&#xff0c;以最小化每个簇内部数据点与簇中心之间的平方距离的总和为目标实现聚类。 1、基本步骤&#xff1a; 1.选择要划分的簇数K&#xff1b; 2.选择K个数据点作为初始的聚类中心&#xff1b…

探索 3D 图形处理的奥秘

最近一年多来&#xff0c;在 3Dfx、Intel 们的狂轰滥炸中&#xff0c;在 Quake、古墓丽影们的推波助澜下&#xff0c;三维图形已经成为计算机迷眼中的又一个热点。3D 世界到底是怎样的神奇&#xff0c;我们又是怎样享受它的乐趣呢&#xff1f;就让我们来一探究竟吧。 图形基础…

51单片机的中断相关知识

51单片机的中断相关知识点 一、中断概念和功能 概念 程序执行过程中CPU会遇到一些特殊情况&#xff0c;是正在执行的程序被“中断”&#xff0c;cpu中止原来正在执行的程序&#xff0c;转到处理异常情况或特殊事件的程序去执行&#xff0c;结束后再返回到原被中止的程序处(断…

使用SecoClient软件连接L2TP

secoclient软件是华为防火墙与友商设备进行微屁恩对接的一款软件,运行在windows下可以替代掉win系统自带的连接功能,因为win系统自带的连接功能总是不可用而且我照着网上查到的各种方法调试了很久都调不好,导致我一度怀疑是我的服务没搭建好,浪费了大把时间去研究其他搭建方案 …

Spring Boot快速搭建一个简易商城项目【完成登录功能且优化】

完成登录且优化&#xff1a; 未优化做简单的判断&#xff1a; 全部异常抓捕 优化&#xff1a;返回的是json的格式 BusinessException&#xff1a;所有的错误放到这个容器中&#xff0c;全局异常从这个类中调用 BusinessException&#xff1a; package com.lya.lyaspshop.exce…

dev express 15.2图表绘制性能问题(dotnet绘图表)

dev express 15.2 绘制曲线 前端代码 <dxc:ChartControl Grid.Row"1"><dxc:XYDiagram2D EnableAxisXNavigation"True"><dxc:LineSeries2D x:Name"series" CrosshairLabelPattern"{}{A} : {V:F2}"/></dxc:XYDi…

TOGAF架构开发方法

TOGAF针对架构开发方法定义了一系列阶段和步骤&#xff0c;这些阶段和步骤对架构的迭代过程进行了详细、标准的描述。 企业架构的项目过程 一、预备阶段&#xff08;Preliminary&#xff09; 1、目标 预备阶段的目标是&#xff1a; 对组织的背景和环境进行审查&#xff08;调…

C# 使用ZXing.Net生成带Logo的二维码

写在前面 这是ZXing.Net类库的系列文章&#xff0c;实现在二维码中间插入一个logo图标 C# 使用ZXing.Net生成二维码和条码-CSDN博客 C# 使用ZXing.Net识别二维码和条码-CSDN博客 代码实现 该段代码主体来自其他文章&#xff0c;贴在这做个记录 /// <summary> /// 生成…

基于Python的B站排行榜大数据分析与可视化系统

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长 QQ 名片 :) 1. 项目简介 本文介绍了一项基于Python的B站排行榜大数据分析与可视化系统的研究。通过网络爬虫技术&#xff0c;系统能够自动分析B站网址&#xff0c;提取大量相关文本信息并存储在系统中。通过对这些信息进行…

【Kubernetes】kubectl 常用命令

kubectl 常用命令 1.基础命令2.部署命令3.集群管理命令4.故障诊断与调试命令5.高级命令6.设置命令7.其他命令 kubectl 是 Kubernetes 提供的命令行管理工具。通过使用 kubectl&#xff0c;可以管理和操作 Kubernetes。 1.基础命令 命令 说明 create通过文件名或标准输入创建 …

微信小程序开发系列-09自定义组件样式特性

微信小程序开发系列目录 《微信小程序开发系列-01创建一个最小的小程序项目》《微信小程序开发系列-02注册小程序》《微信小程序开发系列-03全局配置中的“window”和“tabBar”》《微信小程序开发系列-04获取用户图像和昵称》《微信小程序开发系列-05登录小程序》《微信小程序…

ArkUI中自定义组件的生命周期

文章概叙 本文主要是介绍下在作为page以及component的时候的生命周期&#xff0c;以及调用API等应该在哪个生命周期使用。 书接上回 之前的博客已经结束了对底部栏的操作&#xff0c;现在开始需要关注到具体内容的对接了。 而开发的第一步&#xff0c;我们对页面的生命周期…

SpringAMQP的使用方式

MQ介绍 MQ&#xff0c;中文是消息队列&#xff08;MessageQueue&#xff09;&#xff0c;字面来看就是存放消息的队列。也就是事件驱动架构中的Broker。 比较常见的MQ实现&#xff1a; ActiveMQ RabbitMQ RocketMQ Kafka 几种常见MQ的对比&#xff1a; RabbitMQActiveM…

x-cmd pkg | gum - 很好看的终端 UI 命令行工具

目录 简介首次用户功能特点Bubbles 与 Lip Gloss进一步探索 简介 gum 由 Charm 组织于 2022 年使用 Go 语言开发。旨在帮助用户编写 Shell 脚本与 dotfiles 时提供一系列快捷使用&#xff0c;可配置&#xff0c;可交互&#xff0c;美观的 Terminal UI 组件。 首次用户 使用 x…

lv13 内核与用户空间

一、内核空间和用户空间 为了彻底解决一个应用程序出错不影响系统和其它app的运行&#xff0c;操作系统给每个app一个独立的假想的地址空间&#xff0c;这个假想的地址空间被称为虚拟地址空间&#xff08;也叫逻辑地址&#xff09;&#xff0c;操作系统也占用其中固定的一部分…

嵌入式开发——ADC开发

学习目标 了解ADC开发流程掌握采样方式能够使用ADC进行芯片内部通道进行采样能够使用ADC对外部电路进行采样学习内容 GD32F4的ADC 特点: 16个外部模拟输入通道;1个内部温度传感通道(VSENSE);1个内部参考电压输入通道(VREFINT);1个外部监测电池VBAT供电引脚输入通道。ADC开…

IntelliJ IDEA [警告] pom的依赖中出现警告Provides transitive vulnerable dependency

文章目录 1. 现象2. 为什么出现警告3. 如何对待呢4. 解决5. 解决的好处总结 1. 现象 在我们的工程 pom.xml 中的依赖中&#xff0c;所依赖的 spring-boot-starter-web 出现了警告。 依赖内容 <dependency><groupId>org.springframework.boot</groupId><…

手拉手Springboot获取yml配置文件信息

环境介绍 技术栈 springboot3 软件 版本 mysql 8 IDEA IntelliJ IDEA 2022.2.1 JDK 17 Spring Boot 3.1.7 配置文件说明&#xff1a;启动配置文件优先级&#xff1a;properties高于yml 配置文件application.yml yml是 JSON 的超集&#xff0c;简洁而强大&#xf…

如何在TypeScript中定义未知结构的对象类型?

一个大多数人在学习TypeScript时都会遇到的问题&#xff0c;你能想到多少解决方案呢? 你在学习TypeScript时遇到过类似的错误吗? 要解决这个错误&#xff0c;一个非常暴力的方法是使用 any 类型: let user: any {}user.id "TS001";user.name "Bytefer&quo…

Vue 框架前导:详解 Ajax

Ajax Ajax 是异步的 JavaScript 和 XML。简单来说就是使用 XMLHttpRequest 对象和服务器通信。可以使用 JSON、XML、HTML 和 text 文本格式来发送和接收数据。具有异步的特性&#xff0c;可在不刷新页面的情况下实现和服务器的通信&#xff0c;交换数据或者更新页面 01. 体验 A…