作业--day38

news/2024/7/24 10:24:16 标签: c++

1.定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。

#include <iostream>
#include <iomanip>

using namespace std;

class Person{
private:
    int *age;
    string &name;
public:
    Person(int age, string &name):age(new int(age)),name(name){
        cout << "调用Person构造函数" << endl;
    }
    Person(const Person &other):age(new int(*(other.age))), name(other.name){
        cout << "调用Person拷贝构造函数" << endl;
    }
    Person &operator=(const Person &other){
        *age = *(other.age);
        cout << "调用Person拷贝赋值函数" << endl;
        return *this;
    }
    ~Person(){
        delete age;
        cout << "调用Person析构函数" << endl;
    }
    int get_age();
    string get_name();
};

int Person::get_age(){
    return *age;
}

string Person::get_name(){
    return name;
}

class Stu{
private:
    double *score;
    Person p1;
public:
    Stu(double score, int age, string &name):score(new double(score)), p1(age,name){
        cout << "调用Stu构造函数" << endl;
    }
    Stu(const Stu &other):score(new double(*(other.score))), p1(other.p1){
        cout << "调用Stu拷贝构造函数" << endl;
    }
    Stu &operator=(const Stu &other){
        *score = *(other.score);
        p1 = other.p1;
        cout << "调用Stu拷贝赋值函数" << endl;
        return *this;
    }
    ~Stu(){
        delete score;
        cout << "调用Stu析构函数" << endl;
    }
    void show();
};

void Stu::show(){
    cout << "score = " << *score << " age = " << p1.get_age() << " name = " << p1.get_name() << endl;
}

int main(){

    string name = "张三";
    Stu a(78.4, 18, name);
    a.show();

}

在这里插入图片描述

思维导图

在这里插入图片描述


http://www.niftyadmin.cn/n/5291961.html

相关文章

SpringBoot入门到精通-Spring Boot Jasypt Encrypt 演示

这是 spring boot 应用程序,展示了如何使用 Jasypt 加密值。 关于Jasypt Jasypt 为您提供简单的单向(摘要)和双向加密技术。开放 API,可与任何 JCE 提供程序一起使用,而不仅仅是默认的 Java VM 提供程序。Jasypt 可以很容易地与 Bouncy Castle 等知名提供商一起使用。了解…

【办公技巧】怎么批量提取文件名到excel

Excel是大家经常用来制作表格的文件&#xff0c;比如输入文件名&#xff0c;如果有大量文件需要输入&#xff0c;用张贴复制或者手动输入的方式还是很费时间的&#xff0c;今天和大家分享如何批量提取文件名。 打开需要提取文件名的文件夹&#xff0c;选中所有文件&#xff0c…

解决ERROR! MySQL is running but PID file could not be found.

前言 当查询mysql状态、或者重启MySQL发现如下错误。 这个意思是说&#xff0c;根据配置查找不到pid文件。 解决 1. 查看pid文件在哪里 ps aux | grep mysql我的在&#xff1a;pid-file/usr/local/mysql/data/mysqld.local.pid 2. 查看读取配置顺序 mysqld --verbose --h…

Python(七)—— 内置模块

16. 内置模块一 16.1 序列化模块 1、定义&#xff1a; 序列化的本质就是将一种数据结构&#xff08;如字典、列表&#xff09;等转换成一个特殊的序列&#xff08;字符串或者bytes&#xff09;的过程就叫做序列化 2、作用&#xff1a; 序列化除了可以解决写入文件的问题&a…

Spire.Office 8.12.2 for .NET

Spire.Office 8.12.2 发布。在此版本中&#xff0c;Spire.Doc支持Word到PCL和PostScript转换中的文本整形以及确定文档是否加密&#xff1b;Spire.Presentation支持将母版页转换为图像&#xff1b;Spire.PDFViewer支持在WinForm项目中使用Ctrl滚轮实现界面缩放效果。此外&#…

测试-FastJSON和Jackson-JSON库

文章目录 FastJSONJackson注意 FastJSON 直接读代码&#xff1a; Log4j2 public class FastJsonTest {Testpublic void test1() {// JSON转对象-parseObjectString jsonString "{\"name\":\"张三\",\"age\":18}";Student student …

Qt+Opencv:人脸检测

话接上一篇&#xff0c;我们仍使用在上篇《QtOpencv&#xff1a;Qt中部署opencv》创建的Qt项目来测试opencv提供的sample。 在正式开始本篇之前&#xff0c;我们先说做一下准备工作&#xff1a; 一、opencv官方文档 学习最权威和最可靠的方式&#xff0c;就是阅读官方文档和…

k8s之部署kubeadm

master 20.0.0.71&#xff08;4核8G&#xff09; docker、kubelet、kubectl、kubeadm、flannel node1 20.0.0.73&#xff08;最少2核4G&#xff09; docker、kubelet、kubectl、kubeadm、flannel node2 20.0.0.74&#xff08;最少2核4G&#xff09; docker、kubelet、kube…