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

天津网站建设网页设计公司网络维护公司

天津网站建设网页设计公司,网络维护公司,最有效的招商方式,如何用ps做网站首页图片编程1: 封装一个动物的基类,类中有私有成员:姓名,颜色,指针成员年纪 再封装一个狗这样类,共有继承于动物类,自己拓展的私有成员有:指针成员:腿的个数(整型 …

编程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.hkea.cn/news/710822/

相关文章:

  • 直播网站开发要多久排行榜前十名
  • 网站备案完才能建站吗企业建站公司
  • 网站开发外包合同西安网站优化公司
  • 2022网页设计尺寸规范和要求怎么做seo关键词优化
  • 北京大学两学一做网站十大收益最好的自媒体平台
  • 网站开发服务费企业网站建设的一般要素
  • 台州企业网站制作公司郴州网站推广
  • 如何做移动端网站邮件营销
  • 网站制作佛山crm管理系统
  • 网站综合营销方案设计网页设计教程
  • 东莞做网站制作宁波技术好的企业网站制作
  • 广州做网站公司哪家好如何注册一个网站
  • 网站备案协议书互联网营销师证书含金量
  • 广州企业网站建设报价免费推广网站大全
  • 宁波网站排名怎么提交网址让百度收录
  • 杭州 手机网站建设活动营销
  • 加网络网站建设工作室做一个企业网站大概需要多少钱
  • 张家港优化网站seo百度网盘下载
  • 烟台有没有做网站网站安全
  • 网站建设与制作设计公司惠州seo代理商
  • 东营新闻网今日头条常州网站seo
  • 东莞全网合一网站黄页引流推广网站软件免费
  • wordpress的数据库在那里百度seo如何快速排名
  • wordpress手机客服代码免费seo快速排名工具
  • web网站开发作品关键词歌词图片
  • 汕头行业网站seo培训公司
  • 网站背景图片优化关键词歌曲免费听
  • 郑州做网站哪家专业我要发布信息
  • 西安做网站优化的公司石家庄seo按天扣费
  • 2022年西安封城通知自动app优化下载