3.18_C++_day6_作业

作业要求:

程序代码:

cpp 复制代码
#include <iostream>

using namespace std;


class Animal
{
private:
    string name;
    string color;
    int *age;
public:
    //无参构造函数
    Animal()
    {
        cout << "Animal::无参构造函数" << endl;
    }
    //有参构造函数
    Animal(string name,string color,int age):name(name),color(color),age(new int(age))
    {
        cout << "Animal::有参构造函数" << endl;
    }
    //析构函数
    ~Animal()
    {
        delete age;
        cout << "Animal::析构函数" << endl;
    }
    //拷贝构造函数
    Animal(const Animal &other):name(other.name),color(other.color),age(new int(*other.age))
    {
       cout << "Animal::拷贝构造函数" << endl;
    }
    //拷贝赋值函数
    Animal &operator=(const Animal &other)
    {
        if(this !=&other)
        {
            name = other.name;
            color = other.color;
            age = new int(*other.age);
        }
        cout << "Animal::拷贝赋值函数" << endl;
        return *this;
    }

};
class Dog:public Animal
{
private:
    int *legs;
public:
    //无参构造函数
    Dog()
    {
         cout << "Dog::无参构造" << endl;
    }
    //有参构造函数
    Dog(int legs,string name,string color,int age):Animal(name,color,age),legs(new int(legs))
    {
        cout << "Dog::有参构造函数" << endl;
    }
    //析构函数
    ~Dog()
    {
        delete legs;
        cout << "Dog::析构函数" << endl;
    }
    //拷贝构造函数
    Dog(const Dog &other):Animal(other),legs(new int(*other.legs))
    {
        cout << "Dog::拷贝构造构造" << endl;
    }
    //拷贝赋值函数
    Dog &operator=(const Dog &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            legs = new int(*other.legs);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void speak()
    {
        cout << "汪汪汪" << endl;
    }
};
int main()
{
    Dog d1;//实例化d1对象 不初始化 走无参构造
    Dog d2(4,"xiaohuang","yellow",3);//实例化d2 并初始化
    Dog d3(d2);//实例化d3 并使用d2给其初始
    d1 = d3;  //将d3 赋值给d1
    d1.speak();
    d2.speak();
    d3.speak();
    return 0;
}

运行结果:

作业要求:

程序代码:

cpp 复制代码
#include <iostream>

using namespace std;

//封装一个 讲解动物 类
class Animal
{
public:
    Animal(){}
    virtual void perform() = 0;
};


//封装一个 狮子 类 公有继承Animal
class Lion:public Animal
{
private:
    string name;
public:
    Lion(){}
    Lion(string name):name(name){}
    //重写虚函数
    void perform()
    {
        cout << name << ":跳舞" << endl;
    }
};
//封装一个 大象 类 共有继承Animal
class Elephant:public Animal
{
private:
    string name;
public:
    Elephant() {}
    Elephant(string name):name(name)
    {}
    //重写虚函数
    void perform()
    {
        cout << name << ":唱歌" << endl;
    }
};


//封装一个 猴子 类 共有继承Animal
class Monkey:public Animal
{
private:
    string name;
public:
    Monkey(){}
    Monkey(string name):name(name){}
    //重写虚函数
    void perform()
    {
        cout << name << ":杂耍" << endl;
    }
};


int main()
{
    //狮子类实例化一个对象 并初始化
    Lion l1("狮子");
    //使用基类指针 指向狮子子类对象l1
    Animal *p =&l1;
    p->perform();//展开狮子子类功能


    //大象类实例化对象
    Elephant e1("大象");
    p = &e1;
    p->perform();


    //猴子实例化对象
    Monkey m1("猴子");
    p = &m1;
    p->perform();

    return 0;
}

运行结果:

相关推荐
fouryears_234172 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~2 小时前
C#---StopWatch类
开发语言·c#
lifallen4 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研4 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
快乐的划水a4 小时前
组合模式及优化
c++·设计模式·组合模式
cui__OaO5 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
星星火柴9365 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
鱼鱼说测试6 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑6 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
CHEN5_026 小时前
【Java基础面试题】Java基础概念
java·开发语言