作业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:思维导图




















