C++ day6

1.思维导图

2.试编程:

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

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

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

eg:Dog d1;

Dog d2(......);

Dog d3(d2);

di=d3;

#include <iostream>

using namespace std;

//封装一个 动物 类
class Zoon
{
private:
    string name;
    string color;
    int *age;
public:
    //无参构造函数
    Zoon()
    {
        cout << "Zoon::无参构造函数" << endl;
    }

    //有参构造函数
    Zoon(string name,string color,int age):name(name),color(color),age(new int(age))
    {
        cout << "Zoon::有参构造函数" << endl;
    }

    //深拷贝构造函数
    Zoon(const Zoon &other):name(other.name),color(other.color),age(new int(*other.age))
    {
        cout << "Zoon::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Zoon &operator=(const Zoon &other)
    {
        if(this!=&other)
        {
            name=other.name;
            color=other.color;
            age=new int(*other.age);
        }
        cout << "Zoon::拷贝赋值函数" << endl;
        return *this;
    }

    //析构函数
    ~Zoon()
    {
        delete age;
        cout << "Zoon::析构函数" << endl;
    }
    //展示
    void show()
    {
        delete age;
        age=nullptr;
        cout << "父类::show" <<endl;
        cout << "name=" << name << " color=" << color << " age=" << age << endl;
    }
};

//封装一个 狗 类
class Dog:public Zoon
{
private:
    int *count;        //腿的个数
public:
    //无参构造函数
    Dog()
    {
        cout << "Dog::无参构造函数" << endl;
    }

    //有参构造函数
    Dog(string name,string color,int age,int count):Zoon(name,color,age),count(new int(count))
    {
        cout << "Dog::有参构造函数" << endl;
    }

    //深拷贝构造函数
    Dog(const Dog &other):Zoon(other),count(new int(*other.count))
    {
        cout << "Dog::拷贝构造函数" << endl;
    }

    //拷贝赋值函数
    Dog &operator=(const Dog &other)
    {
        if(this!=&other)
        {
            Zoon::operator=(other);
            count=new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }

    //会叫
    void speak()
    {
        cout << "汪汪汪!" << endl;
    }

    //析构函数
    ~Dog()
    {
        delete count;
        cout << "Dog::析构函数" << endl;
    }
};
int main()
{
    Dog d1;
    Dog d2("贝贝","黄色",5,4);
    Dog d3(d2);
    d1=d3;
    return 0;
}

3.编程题:

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

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

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

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

具体过程如下:

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

#include <iostream>

using namespace std;

//封装 讲解员 类
class Animal
{
private:
    string description;
public:
    //无参构造函数
    Animal() {}

    //有参构造函数
    Animal(string description):description(description) {}

    //纯虚函数
    virtual void perform()=0;

    //虚析构函数
    virtual ~Animal() {}

};

//封装 狮子 类
class Lion:public Animal
{
private:
    int age;
public:
    //无参构造函数
    Lion() {}

    //有参构造函数
    Lion(string description,int age):Animal(description),age(age) {}

    //重写perform函数
    void perform()
    {
        cout << "狮子正在散步" << endl;
    }
};

//封装 大象 类
class Elephant:public Animal
{
private:
    int age;
public:
    //无参构造函数
    Elephant() {}

    //有参构造函数
    Elephant(string description,int age):Animal(description),age(age) {}

    //重写perform函数
    void perform()
    {
        cout << "大象正在顶绣球" << endl;
    }
};

//封装 猴子 类
class Monkey:public Animal
{
private:
    int age;

public:
    //无参构造函数
    Monkey() {}

    //有参构造函数
    Monkey(string description,int age):Animal(description),age(age) {}


    //重写perform函数
    void perform()
    {
        cout << "猴子正在摘桃子" << endl;
    }
};

int main()
{
    Lion l("百兽之王",3);
    Elephant e("身体很强壮",6);
    Monkey m("灵活",2);

    Animal *p=&l;
    p->perform();
    p=&e;
    p->perform();
    p=&m;
    p->perform();
    return 0;
}

相关推荐

  1. MSc CDA Take-Home

    2024-03-19 12:54:01       34 阅读
  2. CDA一级备考策略分享

    2024-03-19 12:54:01       12 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-03-19 12:54:01       23 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-03-19 12:54:01       20 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-03-19 12:54:01       17 阅读
  6. web server apache tomcat11-33-CDI

    2024-03-19 12:54:01       11 阅读
  7. Spring Data访问 MongoDB(十六)----CDI集成

    2024-03-19 12:54:01       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-19 12:54:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-19 12:54:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-19 12:54:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-19 12:54:01       20 阅读

热门阅读

  1. 面试题(补充)

    2024-03-19 12:54:01       22 阅读
  2. openxml对worksheet数值化

    2024-03-19 12:54:01       17 阅读
  3. uniapp 安卓 plus调用原生文件选择

    2024-03-19 12:54:01       19 阅读