C++_day6

思维导图:

2试编程
封装一个动物的基类,类中有私有成员: 姓名,颜色,指针成员年纪
再封装一个狗这样类,共有继承于动物类,自己拓展的私有成员有:指针成员:腿的个数(整型 int count),共有成员函数:会叫: void speak()
要求:分别完成基类和派生类中的:构造函数、析构函数、拷贝构造函数、拷贝赋值函数
eg
Dog d1;
Dog d2(.....);
Dog d3(d2);
d1 = d3,

#include <iostream>

using namespace std;

class Animal
{
private:
    string name;
    string color;
    int *age;
protected:

public:
    Animal()
    {
        cout << "Animal::无参构造函数" << endl;
    }
    Animal(string name,string color,int age):name(name),color(color),age(new int(age))
    {
        cout << "Animal::有参构造函数" << endl;
    }
    ~Animal()
    {
        cout << "Animal::析构函数" << endl;
        delete age;
    }
    Animal(const Animal &other):name(other.name),color(other.color),age(new int(*other.age))
    {
        cout << "Animal::拷贝构造函数" << endl;
    }
    Animal &operator=(const Animal &other)
    {
        if(this != &other)
        {
            name = other.name;
            color = other.color;
            age = new int(*other.age);
        }
        cout << "Animal::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        cout << name << " " << color << " " << *age << " ";
    }
};
class Dog:public Animal
{
private:
    int *count;
public:
    void speak()
    {
        cout << "汪汪汪~" << endl;
    }
    Dog()
    {
        cout << "Dog::无参构造函数" << endl;
    }
    Dog(string name,string color,int age,int count):Animal(name,color,age),count(new int(count))
    {
        cout << "Dog::有参构造函数" << endl;
    }
    ~Dog()
    {
        cout << "Dog::析构函数" << endl;
        delete count;
    }
    Dog(const Dog &other):Animal(other),count(new int(*other.count))
    {
        cout << "Dog::拷贝构造函数" << endl;
    }
    Dog &operator=(const Dog &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            count = new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        Animal::show();
        cout << *count << endl;
    }
};
int main()
{
    Dog d1;
    Dog d2("小黑","黑色",5,4);
    d2.show();
    Dog d3(d2);
    d3.show();
    d1=d3;
    d1.show();
    return 0;
}

运行结果:

3.编程题
以下是一个简单的比喻,将多态概念与生活中的实际情况相联系:
比喻:动物园的讲解员和动物表演
想象一下你去了一家动物园,看到了许多不同种类的动物,如狮子、大象、猴子等。现在,动物园里有一位讲解员,他会为每种动物表演做简单的介绍。
在这个场景中,我们可以将动物比作是不同的类,而每种动物表演则是类中的函数。而讲解员则是一个基类,他可以根据每种动物 的特点和表演,进行相应的介绍。
具体过程如下
定义一个基类Animal,其中有一个虚函数perform (),用于在子类中实现不同的表演行为 

 

#include <iostream>

using namespace std;

class Animal
{
private:
    string name;
    string color;
    int *age;
protected:

public:
    Animal()
    {
        cout << "Animal::无参构造函数" << endl;
    }
    Animal(string name,string color,int age):name(name),color(color),age(new int(age))
    {
        cout << "Animal::有参构造函数" << endl;
    }
    ~Animal()
    {
        cout << "Animal::析构函数" << endl;
        delete age;
    }
    Animal(const Animal &other):name(other.name),color(other.color),age(new int(*other.age))
    {
        cout << "Animal::拷贝构造函数" << endl;
    }
    Animal &operator=(const Animal &other)
    {
        if(this != &other)
        {
            name = other.name;
            color = other.color;
            age = new int(*other.age);
        }
        cout << "Animal::拷贝赋值函数" << endl;
        return *this;
    }
    virtual void perform() = 0;
    void show()
    {
        cout << name << " " << color << " " << *age << " ";
    }
};
class Elephant:public Animal
{
private:
    int *count;
public:
    void perform()
    {
        cout << "喷水  吃香蕉" << endl;
    }
    Elephant()
    {
        cout << "Elephant::无参构造函数" << endl;
    }
    Elephant(string name,string color,int age,int count):Animal(name,color,age),count(new int(count))
    {
        cout << "Elephant::有参构造函数" << endl;
    }
    ~Elephant()
    {
        cout << "Elephant::析构函数" << endl;
        delete count;
    }
    Elephant(const Elephant &other):Animal(other),count(new int(*other.count))
    {
        cout << "Elephant::拷贝构造函数" << endl;
    }
    Elephant &operator=(const Elephant &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            count = new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        Animal::show();
        cout << *count << endl;
    }
};
class Lion:public Animal
{
private:
    int *count;
public:
    void perform()
    {
        cout << "钻火圈" << endl;
    }
    Lion()
    {
        cout << "Lion::无参构造函数" << endl;
    }
    Lion(string name,string color,int age,int count):Animal(name,color,age),count(new int(count))
    {
        cout << "Lion::有参构造函数" << endl;
    }
    ~Lion()
    {
        cout << "Lion::析构函数" << endl;
        delete count;
    }
    Lion(const Lion &other):Animal(other),count(new int(*other.count))
    {
        cout << "Lion::拷贝构造函数" << endl;
    }
    Lion &operator=(const Lion &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            count = new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        Animal::show();
        cout << *count << endl;
    }
};
int main()
{
    Elephant e1("大象","五彩",6,4);
    Lion l1("狮子","棕黄色",4,4);
    e1.perform();
    l1.perform();
    return 0;
}

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

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

相关文章

7.测试教程-自动化测试selenium-2

文章目录 1.webdriver API1.1元素的定位1.2id定位1.3name 定位1.4tag name 定位和class name 定位1.5CSS 定位(常用)1.5.1概念1.5.2实操1.5.3语法 1.6XPath 定位1.6.1概念1.6.2实操1.6.3语法 1.7link text定位1.8Partial link text 定位1.9一个简单的测试实战1.10CSS定位和XPat…

【人工智能】英文学习材料03(每日一句)

&#x1f33b;个人主页&#xff1a;相洋同学 &#x1f947;学习在于行动、总结和坚持&#xff0c;共勉&#xff01; 目录 Chain Rule (链式法则) Dimensionality Reduction (降维) Long Short-Term Memory (LSTM) (长短期记忆网络) Gradient Explosion (梯度爆炸) Gradie…

Java项目:63 ssm网上花店设计+vue

作者主页&#xff1a;舒克日记 简介&#xff1a;Java领域优质创作者、Java项目、学习资料、技术互助 文中获取源码 项目介绍 系统具备友好性且功能完善。管理员登录进入后台之后&#xff0c;主要完成花材选择管理&#xff0c;用户管理&#xff0c;鲜花管理&#xff0c;鲜花出入…

计算机网络实践学习 思科实验31:配置思科DHCP

思科实验31&#xff1a;配置思科DHCP 实验拓扑图实验目标实验步骤实验配置 实验拓扑图 实验目标 配置思科设备作为DHCP服务器 实验步骤 配置OSPF路由协议配置R1为DHCP服务器配置DHCP中继&#xff0c;使得PC3可以获得地址全网通信测试 实验配置 1、配置R1为DHCP服务器&…

React Native: could not connect to development server

问题&#xff1a; 运行模拟器错误&#xff1a;无法连接到开发服务器 原因分析&#xff1a; 1、确认模拟器连接状态&#xff0c;是连接成功的 查看进程的端口占用&#xff0c;也没问题 lsof -i tcp:8081 kill pid2、检查包服务器是否运行正常 连接真机进行调试发现真机是正常…

【力扣精选算法100道】——带你了解(数组模拟栈)算法

目录 &#x1f4bb;比较含退格的字符串 &#x1f388;了解题意 &#x1f388;分析题意 &#x1f6a9;栈 &#x1f6a9;数组模拟栈 &#x1f388;实现代码 844. 比较含退格的字符串 - 力扣&#xff08;LeetCode&#xff09; &#x1f4bb;比较含退格的字符串 &#x1f3…

查看网卡和网关命令

ifconfig&#xff08;接口配置&#xff09; 是一个网络管理工具&#xff0c;它用于配置和查看 Linux 操作系统中网络接口的状态&#xff0c;使用ifconfig&#xff0c;您可以分配 IP 地址、启用或禁用接口、管理 ARP 缓存、路由等。 ping命令是个使用频率极高的网络诊断工具。…

win32汇编弹出对话框

之前书上有一个win32 asm 的odbc例子&#xff0c;它有一个窗体&#xff0c;可以执行sql&#xff1b;下面看一下弹出一个录入数据的对话框&#xff1b; 之前它在.code段包含2个单独的asm文件&#xff0c;增加第三个&#xff0c;增加的这个里面是弹出对话框的窗口过程&#xff0…

01初识Python

一、Python 简介 二、为什么要学Python&#xff1f; 三、Python 安装 四、输出第一条指令 五、总结 一、Python 简介 Python是一种高级编程语言&#xff0c;由Guido van Rossum于1991年创建。它具有简单易学的语法结构&#xff0c;被广泛应用于Web开发、数据科学、人工智…

LeetCode刷题记录:(11)组合(初识回溯算法)

leetcode传送通道 暂时记录&#xff0c;这篇没啥营养&#xff0c;不用看了 class Solution {List<List<Integer>> result new ArrayList<>(); // 存所有组合List<Integer> path new LinkedList<>(); //存每一个组合public List<List<Int…

高效使用git流程分享

准备 假设你已经 clone 了当前仓库&#xff0c;并且你的终端位置已经位于仓库目录中。 查询状态 查询状态常用的命令有 git status 和 git branch。 前者用于查询更改文件情况&#xff0c;后者用于展示所有分支。 chatbot-system$ git status On branch develop Your bran…

基于SpringBoot和Vue的图书个性化推荐系统的设计与实现

今天要和大家聊的是一款基于SpringBoot和Vue的图书个性化推荐系统。 &#x1f495;&#x1f495;作者&#xff1a;李同学 &#x1f495;&#x1f495;个人简介&#xff1a;混迹在java圈十年有余&#xff0c;擅长Java、微信小程序、Python、Android等&#xff0c;大家有这一块的…

Spring-3

目录 Spring AOP和AspectJ AOP 在Spring AOP 中&#xff0c;关注点和横切关注的区别 Spring 框架中的单例 Bean 是线程安全的吗 Spring 是怎么解决循环依赖的&#xff1f; 事务隔离级别 事务的传播级别 Spring 事务实现方式 Spring框架的事务管理有哪些优点 事务注解的…

【鸿蒙HarmonyOS开发笔记】如何使用图片插帧将低像素图片清晰放大

开发UI时&#xff0c;当我们的原图分辨率较低并且需要放大显示时&#xff0c;图片会模糊并出现锯齿。如下图所示 这时可以使用interpolation()方法对图片进行插值&#xff0c;使图片显示得更清晰。该方法的参数为ImageInterpolation枚举类型&#xff0c;可选的值有: ImageInte…

Git 仓库瘦身与 LFS 大文件存储

熟悉 Git 的小伙伴应该都知道随着 Git 仓库维护的时间越来越久&#xff0c;追踪的文件越来越多&#xff0c;git 存储的 objects 数量会极其庞大&#xff0c;每次从远程仓库 git clone 的时候都会墨迹很久。如果我们不小心 git add 了一个体积很大的文件&#xff0c;且 git push…

从历年315曝光案例,看APP隐私合规安全

更多网络安全干货内容&#xff1a;点此获取 ——————— 随着移动互联网新兴技术的发展与普及&#xff0c;移动APP的应用渗透到人们的衣食住行方方面面&#xff0c;衍生出各类消费场景的同时&#xff0c;也带来了无数的个人隐私数据泄露、网络诈骗事件。 历年来&#xff…

python 调用redis创建查询key

部署redis apiVersion: apps/v1 # 描述api版本&#xff0c;默认都用这个 kind: Deployment # 资源类型&#xff0c;可以配置为pod&#xff0c;deployment&#xff0c;service&#xff0c;statefulset等等 metadata: # deployment相关的元数据&#xff0c;用于描述deployment的…

分享一个不错的three.js开源项目

项目将three.js相关内容封装为相应库 很值得学习&#xff0c;可以模仿项目学习three.js vue-vite-three.js threejs-park: 基于vue3&#xff0c;threeJS智慧园区 threejs-park

Java八股文(XXL-JOB)

Java八股文のXXL-JOB XXL-JOB XXL-JOB xxl-job 是什么&#xff1f;它的主要作用是什么&#xff1f; xxl-job 是一款分布式任务调度平台&#xff0c;用于解决分布式系统中的定时任务和异步任务调度问题。 它提供了任务的注册、调度、执行和监控等功能&#xff0c;能够帮助开发者…

测试进阶必备,这5款http接口自动化测试工具不要太香~

现在市场上能做接口自动化测试的工具有很多&#xff0c;一搜一大把&#xff0c;让人眼花缭乱。我们去选择对应实现方式时&#xff0c;不管是框架体系还是成熟稳定的工具&#xff0c;核心目的都是期望引入的技术能在最低投入的情况下达到最优效果。 那么我们选择依据出来了&…
最新文章