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

网站做收藏本站那样产品推广渠道

网站做收藏本站那样,产品推广渠道,wordpress会员模块,基金会网站建设方案作业1:编程题: 以下是一个简单的比喻,将多态概念与生活中的实际情况相联系: 比喻:动物园的讲解员和动物表演 想象一下你去了一家动物园,看到了许多不同种类的动物,如狮子、大象、猴子等。现在…

作业1:编程题:

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

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

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

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

具体过程如下: 

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

代码:

#include <iostream>using namespace std;//封装一个 动物 的基类
class Animal
{
private:string type;    //动物种类string color;   //动物颜色
public://无参构造函数Animal() {}//有参构造函数Animal(string type, string color):type(type), color(color){cout << "Animal::有参构造函数" << endl;}//表演行为virtual void perform(){cout << "杂技表演" << endl;}//虚析构函数:正确引导子类释放自己的空间virtual ~Animal(){cout << "Animal::析构函数" << endl;}
};class Dog:public Animal
{
private:string name;    //狗的名字int age;        //狗的年龄
public://无参构造函数Dog() {}//有参构造函数Dog(string type, string color, string name, int age):Animal(type, color), name(name), age(age){cout << "Dog::有参构造函数" << endl;}//表演行为void perform(){cout << " Dog::摇尾巴 " << endl;}//析构函数~Dog(){cout << "Dog::析构函数" << endl;}
};
//封装一个 猫 这样的类   公有继承 动物 这个基类
class Cat:public Animal
{
private:string name;    //猫的名字int age;        //猫的年龄
public://无参构造函数Cat() {}//有参构造函数Cat(string type, string color, string name, int age):Animal(type, color), name(name), age(age){cout << "Cat::有参构造函数" << endl;}//表演行为void perform(){cout << " Cat::抓老鼠 " << endl;}//析构函数~Cat(){cout << "Cat::析构函数" << endl;}
};int main()
{//用狗这样的类实例化一个 d1 对象,并自动调用构造函数初始化Dog d1("狗", "黄色", "大黄", 5);//定义一个父类的指针,指向子类Animal *p = &d1;cout << "--------狗的行为--------" << endl;p->perform();cout << "-----------------------" << endl;//用猫这样的类实例化一个 c1 对象,并自动调用构造函数初始化Cat c1("猫", "白色", "大白", 5);p = &c1;cout << "--------猫的行为--------" << endl;p->perform();cout << "-----------------------" << endl;return 0;
}

效果图:

作业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;         //动物的年龄(指针成员)
public://无参构造Animal(){}//有参构造Animal(string name, string color, int age):name(name), color(color), age(new int(age)){cout << "父类::有参构造函数" << endl;}//拷贝构造函数Animal(const Animal &other):name(other.name), color(other.color), age(new int(*other.age)){cout << "父类::拷贝构造函数" << endl;}//拷贝赋值函数Animal &operator=(const Animal &other){if(this != &other){name = other.name;color = other.color;age = new int(*other.age);}cout << "父类::拷贝赋值函数" << endl;return *this;}//析构函数~Animal(){delete age;age = nullptr;cout << "父类::析构函数" << endl;}};
//封装一个 狗 类    公有继承 动物 类
class Dog:public Animal
{
private:int *count;         //狗腿的个数(指针成员)
public://无参构造函数Dog(){}//有参构造函数Dog(int count, string name, string color, int age):Animal(name, color, age),count(new int(count)){cout << "子类::有参构造函数" << endl;}//拷贝构造函数Dog(const Dog &other):Animal(other), count(new int(*other.count)){cout << "子类::拷贝构造函数" << endl;}//拷贝赋值函数Dog &operator=(const Dog &other){if(this != &other){Animal::operator=(other);count = new int(*other.count);}cout << "子类::拷贝赋值函数" << endl;return *this;}//析构函数~Dog(){delete count;count = nullptr;cout << "子类::析构函数" << endl;}//行为函数void speak(){cout << "汪汪汪" << endl;}
};int main()
{//用狗这样的类实例化一个对象Dog d1;                         //自动调用无参构造函数Dog d2(4,"小黑", "黑", 5);       //自动调用有参构造函数d2.speak();Dog d3(d2);                     //自动调用拷贝构造函数d1 = d3;                        //自动调用拷贝赋值函数return 0;
}

效果图:

作业3:思维导图

http://www.hkea.cn/news/311184/

相关文章:

  • 有什么推广方法优化大师电脑版官方
  • 自己做网站的服务器上海网站建设公司排名
  • 跳蛋塞逼做多的视频网站百度广告联盟官网
  • 房地产网站开发文档企业查询
  • 做emu对网站有什么要求十大免费无代码开发软件
  • 扬州专业做网站做关键词优化
  • 宿州网站建设贰聚思诚信网站服务器
  • 用照片做模板下载网站好百度爱采购官方网站
  • 微网站建设套餐网络营销是做什么的
  • 徐州有哪些做网站苏州网站建设费用
  • 统一企业信息管理系统网站直通车怎么开效果最佳
  • 武汉外贸网站建设公司外链相册
  • java做网站的权限管理seo描述是什么
  • 招聘网最新招聘信息网武汉seo计费管理
  • 直播软件开发源码重庆seo顾问
  • 公司网站如何做宣传百度视频推广怎么收费
  • 淄博市 网站建设报价郑州seo外包阿亮
  • 网络服务商是指什么网站优化排名工具
  • 网站优化的分析比较好的品牌策划公司有哪些
  • 国外比较好的资源网站电商运营推广是做什么的
  • 佛山房地产网站建设seo实战培训王乃用
  • 如何做可以赚钱的网站关键词如何快速排名
  • 深圳品牌做网站公司有哪些百度app推广
  • 重庆建设行业信息网站搜狗登录入口
  • 同仁行业网站建设报价北京做的好的seo公司
  • 陕西自助建站做网站郑州外语网站建站优化
  • 小型企业网站系统cilimao磁力猫最新版地址
  • 铁岭网站建设移动网站广东网站seo
  • 网站模板插件sem和seo
  • 用wordpress制作网站模板沈阳seo