当前位置: 首页 > news >正文

C++作业day6

编程1:

封装一个动物的基类,类中有私有成员:姓名,颜色,指针成员年纪

再封装一个狗这样类,共有继承于动物类,自己拓展的私有成员有:指针成员:腿的个数(整型 int count),共有成员函数:会叫:void speak()

要求:分别完成基类和派生类中的:构造函数、析构函数、拷贝构造函数、拷贝赋值函数

#include <iostream>using namespace std;//封装 动物 类  -->基类
class Animal
{
private:string name;string colour;int *age;
public:Animal(){}Animal(string name,string colour,int age):name(name),colour(colour),age(new int(age)){cout << "基类::有参构造函数" << endl;}Animal(const Animal &other):name(other.name),colour(other.colour),age(new int(*other.age)){cout << "基类::拷贝构造函数" << endl;}Animal &operator=(const Animal &other){if(this != &other){name = other.name;colour = other.colour;age = new int(*other.age);}cout << "基类::拷贝赋值函数" << endl;return *this;}~Animal(){delete age;age = nullptr;cout << "基类::析构函数"  <<  endl;}
};//封装  狗  类  public 继承 动物 类   --> 派生类
class Dog:public Animal
{
private:int *number;  //腿的个数
public:Dog() {}Dog(string name,string colour,int age,int number):Animal(name,colour,age),number(new int(number)){cout << "派生类::有参构造函数" << endl;}Dog(const Dog &other):Animal(other),number(new int(*other.number)){cout << "派生类::拷贝构造函数" << endl;}Dog &operator=(const Dog &other){if(this != &other){Animal::operator=(other);number = new int(*other.number);}cout << "派生类::拷贝赋值函数" << endl;return *this;}void speak(){cout <<  "汪!汪!汪!" << endl;}~Dog(){delete number;number = nullptr;cout << "派生类::析构函数"  <<  endl;}
};int main()
{Dog d1("旺财","黑色",3,4);d1.speak();Dog d2 = d1;Dog d3;d3 = d1;return 0;
}

 

编程2:

以下是一个简单的比喻,将多态概念与生活中的实际情况相联系:

比喻:动物园的讲解员和动物表演

想象一下你去了一家动物园,看到了许多不同种类的动物,如狮子、大象、猴子等。现在,动物园里有一位讲解员,他会为每种动物表演做简单的介绍。

在这个场景中,我们可以将动物比作是不同的类,而每种动物表演则是类中的函数。而讲解员则是一个基类,他可以根据每种动物的特点和表演,进行相应的介绍。

具体过程如下:

定义一个基类 Animal,其中有一个虛函数perform(),用于在子类中实现不同的表演行为。

#include <iostream>using namespace std;//封装 讲解员  基类
class Commentator
{
public:virtual void perform() = 0;   //纯虚函数virtual ~Commentator(){}   //虚解析函数
};//封装  Lion 派生类
class Lion : public Commentator
{
public:void perform()      // 重写 perform 函数{cout << "狮子正在睡觉" << endl;}
};//封装 Elephant 派生类
class Elephant : public Commentator
{
public:void perform()   // 重写 perform 函数{cout << "大象正在吃香蕉" << endl;}
};//封装 Monkey 派生类
class Monkey : public Commentator
{
public:void perform()      // 重写 perform 函数{cout << "猴子正在爬树" << endl;}
};int main()
{Commentator *p1 = new Lion;     //基类的指针指向派生类LionCommentator *p2 = new Elephant; //基类的指针指向派生类ElephantCommentator *p3 = new Monkey;   //基类的指针指向派生类Monkeyp1->perform(); p2->perform(); p3->perform();   //通过父类指针调用基类中的重写函数delete  p1;  delete  p2;  delete  p3;delete  p1;  delete  p2;   delete  p3;return 0;
}

思维导图

http://www.lryc.cn/news/326382.html

相关文章:

  • mysql的单表、多表查询和数据类型
  • 中间件-消息队列
  • 一文get,最容易碰上的接口自动化测试问题汇总
  • Oracle:ORA-01830错误-更改数据库时间格式
  • 树状数组及应用
  • HarmonyOS 应用开发案例
  • 【C++ leetcode】双指针(专题完结)
  • 动态代理大总结
  • 理解Harris角点检测的数学原理
  • ETIM -国际贸易的产品分类标准
  • MySQL高阶SQL语句
  • 聊聊CSS
  • C语言 青蛙跳台阶问题
  • 【Django开发】前后端分离美多商城项目第3篇:用户部分,1. 后端接口设计:【附代码文档】
  • DHCP snooping、DHCP安全及威胁防范
  • 用eclipse创建Web项目,通过Servlet实现Web访问的功能。
  • tools.jar下载 Unable to create schema compiler
  • 【0278】checkpointer 共享内存(CheckpointerShmem)初始化(3)
  • 算法打卡day29|贪心算法篇03|Leetcode 1005.K次取反后最大化的数组和、134. 加油站、135. 分发糖果
  • 【hexo博客6】自定义域名 购买、配置、更新部署
  • Django使用pyJwt进行token校验
  • ❤️算法笔记❤️-(每日一刷-26、删除有序数组的重复项)
  • 银河麒麟系统安装设备类型选择lvm简单模式之后,数据写入导致失败导致系统重启无法正常加载
  • Mybatis-核心配置文件 / Mybatis增删改查
  • Nginx(面试)
  • net::ERR_SSL_PROTOCOL_ERROR
  • BaseDao封装增删改查(超详解)
  • 【Python操作基础】——元组
  • 光伏投融资该如何计算?
  • 【更新中】Leetcode中遇到的最短路径算法