C++day4作业

  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.mfbz.cn/a/280939.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

利用idea+ jclasslib插件查看和分析 Java 类文件的字节码

jclasslib介绍 jclasslib 插件是一个用于 IntelliJ IDEA 的工具&#xff0c;它允许开发者在集成开发环境&#xff08;IDE&#xff09;内直接查看和分析 Java 类文件的字节码。这个插件尤其对于想要深入了解 Java 字节码、类加载机制、以及 Java 虚拟机&#xff08;JVM&#xf…

第4课 FFmpeg读取本地mp4文件并显示

在上节课&#xff0c;我们使用FFmpeg实现了一个最简单的rtmp播放器&#xff0c;它看起来工作正常。这节课&#xff0c;我们尝试让它来播放本地的mp4文件试试。 1.将原rtmp地址修改为本地mp4地址&#xff1a; const char *inFileName "d:\\mp4\\dtz.mp4"; 调试运…

阿里后端实习二面

阿里后端实习二面 记录面试题目&#xff0c;希望可以帮助到大家 类加载的流程&#xff1f; 类加载分为三个部分&#xff1a;加载、连接、初始化 加载 类的加载主要的职责为将.class文件的二进制字节流读入内存(JDK1.7及之前为JVM内存&#xff0c;JDK1.8及之后为本地内存)&…

GO学习记录 —— 创建一个GO项目

文章目录 前言一、项目介绍二、目录介绍三、创建过程1.引入Gin框架、创建main2.加载配置文件3.连接MySQL、redis4.创建结构体5.错误处理、返回响应处理 前言 代码地址 下载地址&#xff1a;https://github.com/Lee-ZiMu/Golang-Init.git 一、项目介绍 1、使用Gin框架来创建项…

[Angular] 笔记 21:@ViewChild

chatgpt: 在 Angular 中&#xff0c;ViewChild 是一个装饰器&#xff0c;用于在组件类中获取对模板中子元素、指令或组件的引用。它允许你在组件类中访问模板中的特定元素&#xff0c;以便可以直接操作或与其交互。 例如&#xff0c;如果你在模板中有一个子组件或一个具有本地…

【shell】命令行自动补全(compgen、complete、compopt)

目录 用途 小例子 说明 进阶-多级补齐 Bash自动补齐原理 用途 自编写的Shell脚本/命令&#xff0c;很多时候都需要输入一定的参数。当参数较多而且较复制的时候&#xff0c;如果能使用Tab键补全就显得非常的便利。 小例子 例如&#xff0c;我们自定义一个命令 footest function…

使用flutter开发windows桌面软件读取ACR22U设备的nfc卡片id,5分钟搞定demo

最近有个需求&#xff0c;要使用acr122u读卡器插入电脑usb口&#xff0c;然后读取nfc卡片的id&#xff0c;并和用户账号绑定&#xff0c;调研了很多方式&#xff0c;之前使用rust实现过一次&#xff0c;还有go实现过一次&#xff0c;然后使用electron的时候遇到安装pcsc-lite失…

ORACLE P6 v23.12 最新虚拟机(VM)全套系统环境分享

引言 根据上周的计划&#xff0c;我简单制作了两套基于ORACLE Primavera P6 最新发布的23.12版本预构建了虚拟机环境&#xff0c;里面包含了全套P6 最新版应用服务 此虚拟机仅用于演示、培训和测试目的。如您在生产环境中使用此虚拟机&#xff0c;请先与Oracle Primavera销售代…

python常见报错信息!错误和异常!附带处理方法

作为 Python 初学者&#xff0c;在刚学习 Python 编程时&#xff0c;经常会看到一些报错信息。 Python 有两种错误很容易辨认&#xff1a;语法错误和异常。 Python assert&#xff08;断言&#xff09;用于判断一个表达式&#xff0c;在表达式条件为 false 的时候触发异常。 …

【UE5蓝图】读取本地json文件修改窗口大小

效果 插件 蓝图 1.判断文件存在 2.1文件不存在&#xff0c;生成文件 {"ResolutionX":540, "ResolutionY":960} 2.2文件存在&#xff0c;直接读取 3.设置窗口大小 遇到的坑 1.分辨率太大&#xff0c;导致效果不理想&#xff0c;建议先往小填写。 2.选对…

QT 利用开源7z 实现解压各种压缩包,包括进度条和文件名的显示(zip,7z,rar,iso等50多种格式)

想做一个winRAR一样的解压软件吗?很简单,利用开源的7z库就能实现。我看网上其他人说的方法不敢苟同,误人子弟。以前自己在项目中使用过7z,这次又有需要,就想记录下来。如果你研究过如何用7z的话,一定知道7z的每一个GUID都代表了一种格式,50多种GUID也就有50多个格式,最…

Unity坦克大战开发全流程——开始场景——音效数据逻辑

开始场景——音效数据逻辑 从这里开始到后面的三小节我们都将干一件很重要的事——数据存储&#xff0c;只有实现了数据存储才能在再次进入游戏时保持游戏数据不被丢失。 类图分析&#xff1a;数据管理类是一个大类&#xff0c;它其中关联了两个类&#xff08;这两个类都是数据…

迭代归并:归并排序非递归实现解析

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《数据结构&算法》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! &#x1f4cb; 前言 归并排序的思想上我们已经全部介绍完了&#xff0c;但是同时也面临和快速排序一样的问题那就是递…

Go语言中的性能考虑和优化

优化您的Go代码以达到最佳性能 性能优化是软件开发的关键方面&#xff0c;无论您使用哪种编程语言。在这篇文章中&#xff0c;我们将探讨Go语言中的性能考虑和优化&#xff0c;Go是一种以其效率而著称的静态类型和编译语言。我们将深入探讨三个关键领域&#xff1a;分析并发代…

用CSS中的动画效果做一个转动的表

<!DOCTYPE html> <html lang"en"><head><meta charset"utf-8"><title></title><style>*{margin:0;padding:0;} /*制作表的样式*/.clock{width: 500px;height: 500px;margin:0 auto;margin-top:100px;border-rad…

【Electron】webview 实现网页内嵌

实现效果&#xff1a; 当在输入框内输入某个网址后并点击button按钮 , 该网址内容就展示到下面 踩到的坑&#xff1a;之前通过web技术实现 iframe 标签内嵌会出现 同源策略&#xff0c;同时尝试过 vue.config.ts 内配置跨域项 那样确实 是实现啦 但不知道如何动态切换 tagert …

深入浅出Java虚拟机

文章目录 总体图类装载子系统一、类的加载过程一、加载二、链接三、初始化 二、类的加载器 运行时数据区一、程序计数器(ProgramCounter)二、虚拟机栈( Java Stack )三、本地方法栈&#xff08; Native Method Stack &#xff09;四、堆内存&#xff08;Direct Memory&#xff…

51单片机的中断相关知识

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

【Android12】Android Framework系列---tombstone墓碑生成机制

tombstone墓碑生成机制 Android中程序在运行时会遇到各种各样的问题&#xff0c;相应的就会产生各种异常信号&#xff0c;比如常见的异常信号 Singal 11&#xff1a;Segmentation fault表示无效的地址进行了操作&#xff0c;比如内存越界、空指针调用等。 Android中在进程(主要…

Screenshot-to-code开源项目mac上实践

github上的开源项目&#xff0c;看介绍可以将设计ui图片转换为 HTML 和 CSS 源码地址&#xff1a; GitCode - 开发者的代码家园 我的mac安装了2.7和3.11&#xff0c;就用3吧直接上代码 安装 pip3 install keras tensorflow pillow h5py jupyter 报错 ERROR: Could not in…