【无标题】

 

#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()
    {
        delete 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;
    }
    void show()
    {
        cout << " name = " << name<< " color = " << color << " age = " << *age << endl;

    }
};
class Dog:public animal
{
private:
    int *leg;
public:
    void speak()
    {
        cout << "朱文涛在狗叫!" << endl;
    }
    Dog(){}
    Dog(string name,string color,int age,int leg):animal(name,color,age),leg(new int(leg))
    {
        cout << "子类的构造" << endl;
    }
    ~Dog()
    {
        delete leg;
        cout << "子类的析构" << endl;
    }
    Dog(const Dog &other):animal(other),leg(new int(*(other.leg)))
    {
        cout << "子类的拷贝构造" << endl;
    }
    Dog &operator=(const Dog &other)
    {
        if(this != &other)
        {
            animal::operator=(other);
            leg=new int(*(other.leg));
            cout << "子类的拷贝赋值" << endl;
        }
        return *this;
    }
    void show()
    {
        animal::show();
        cout << "leg = " << *leg << endl;
    }
};

int main()
{
    animal a1("diandian","white",6);
    a1.show();

    Dog g1("朱文涛","白色儿",88,4);
    g1.show();
    return 0;
}

 

 

#include <iostream>

using namespace std;

class human
{
private:
    string name;
public:
    human() {}
    human(string name):name(name)
    {
        cout << "我是讲解员:" << name << endl;
    }
    virtual void speak() = 0;
    virtual ~human()
    {
        cout << "等一哈,喝个水" << endl;
    }
};
class elephant:public human
{
private:
    string name;
public:
    elephant() {}
    elephant(string n,string name):human(n),name(name){}
    void speak()
    {
        cout << "这是我们园内最出名的非洲象,叫" << name << ",他最喜欢吃煎饼" << endl;
    }
};
class monkey:public human
{
private:
    string name;
public:
    monkey (){}
    monkey(string n,string name):human(n),name(name){}
    void speak()
    {
        cout << "接下来让我们看看大明星——白皮黑毛猴子,名为:" << name << ",他十分喜欢偷别的动物的桃子" << endl;
    }


};
int main()
{
    elephant e1("Dj","时兴隆");
    human *p = &e1;
    p->speak();
    monkey m1("Dj","猪文涛");
    p = &m1;
    p->speak();
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值