C++_day6

思维导图:

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;
protected:

public:
    Animal()
    {
        cout << "Animal::无参构造函数" << endl;
    }
    Animal(string name,string color,int age):name(name),color(color),age(new int(age))
    {
        cout << "Animal::有参构造函数" << endl;
    }
    ~Animal()
    {
        cout << "Animal::析构函数" << endl;
        delete age;
    }
    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;
    }
    void show()
    {
        cout << name << " " << color << " " << *age << " ";
    }
};
class Dog:public Animal
{
private:
    int *count;
public:
    void speak()
    {
        cout << "汪汪汪~" << endl;
    }
    Dog()
    {
        cout << "Dog::无参构造函数" << endl;
    }
    Dog(string name,string color,int age,int count):Animal(name,color,age),count(new int(count))
    {
        cout << "Dog::有参构造函数" << endl;
    }
    ~Dog()
    {
        cout << "Dog::析构函数" << endl;
        delete count;
    }
    Dog(const Dog &other):Animal(other),count(new int(*other.count))
    {
        cout << "Dog::拷贝构造函数" << endl;
    }
    Dog &operator=(const Dog &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            count = new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        Animal::show();
        cout << *count << endl;
    }
};
int main()
{
    Dog d1;
    Dog d2("小黑","黑色",5,4);
    d2.show();
    Dog d3(d2);
    d3.show();
    d1=d3;
    d1.show();
    return 0;
}

运行结果:

3.编程题
以下是一个简单的比喻,将多态概念与生活中的实际情况相联系:
比喻:动物园的讲解员和动物表演
想象一下你去了一家动物园,看到了许多不同种类的动物,如狮子、大象、猴子等。现在,动物园里有一位讲解员,他会为每种动物表演做简单的介绍。
在这个场景中,我们可以将动物比作是不同的类,而每种动物表演则是类中的函数。而讲解员则是一个基类,他可以根据每种动物 的特点和表演,进行相应的介绍。
具体过程如下
定义一个基类Animal,其中有一个虚函数perform (),用于在子类中实现不同的表演行为 

 

#include <iostream>

using namespace std;

class Animal
{
private:
    string name;
    string color;
    int *age;
protected:

public:
    Animal()
    {
        cout << "Animal::无参构造函数" << endl;
    }
    Animal(string name,string color,int age):name(name),color(color),age(new int(age))
    {
        cout << "Animal::有参构造函数" << endl;
    }
    ~Animal()
    {
        cout << "Animal::析构函数" << endl;
        delete age;
    }
    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;
    }
    virtual void perform() = 0;
    void show()
    {
        cout << name << " " << color << " " << *age << " ";
    }
};
class Elephant:public Animal
{
private:
    int *count;
public:
    void perform()
    {
        cout << "喷水  吃香蕉" << endl;
    }
    Elephant()
    {
        cout << "Elephant::无参构造函数" << endl;
    }
    Elephant(string name,string color,int age,int count):Animal(name,color,age),count(new int(count))
    {
        cout << "Elephant::有参构造函数" << endl;
    }
    ~Elephant()
    {
        cout << "Elephant::析构函数" << endl;
        delete count;
    }
    Elephant(const Elephant &other):Animal(other),count(new int(*other.count))
    {
        cout << "Elephant::拷贝构造函数" << endl;
    }
    Elephant &operator=(const Elephant &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            count = new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        Animal::show();
        cout << *count << endl;
    }
};
class Lion:public Animal
{
private:
    int *count;
public:
    void perform()
    {
        cout << "钻火圈" << endl;
    }
    Lion()
    {
        cout << "Lion::无参构造函数" << endl;
    }
    Lion(string name,string color,int age,int count):Animal(name,color,age),count(new int(count))
    {
        cout << "Lion::有参构造函数" << endl;
    }
    ~Lion()
    {
        cout << "Lion::析构函数" << endl;
        delete count;
    }
    Lion(const Lion &other):Animal(other),count(new int(*other.count))
    {
        cout << "Lion::拷贝构造函数" << endl;
    }
    Lion &operator=(const Lion &other)
    {
        if(this != &other)
        {
            Animal::operator=(other);
            count = new int(*other.count);
        }
        cout << "Dog::拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        Animal::show();
        cout << *count << endl;
    }
};
int main()
{
    Elephant e1("大象","五彩",6,4);
    Lion l1("狮子","棕黄色",4,4);
    e1.perform();
    l1.perform();
    return 0;
}

#include <iostream> #include <cstdio> #include <cstring> using namespace std; const char* Haab_month_name[] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"}; int Tzolkin_day_number[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7}; const char *Tzolkin_day_name[] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; struct Date { int day; int month; int year; }; // 把哈布历转换成天数 int haab_to_days(int day, const char* month, int year) { int total_days = day + (year * 365); int month_index; for (month_index = 0; month_index < 19; ++month_index) { if (strcmp(Haab_month_name[month_index], month) == 0) { break; } } total_days += month_index * 20; return total_days; } // 把天数转换成托尔金历 void days_to_tzolkin(int total_days, int* day_number, const char** day_name) { int tzolkin_day_count = total_days % 13; int tzolkin_day_index = tzolkin_day_count - 1; if (tzolkin_day_index < 0) tzolkin_day_index = 12; int tzolkin_day_number = Tzolkin_day_number[tzolkin_day_index]; const char *tzolkin_day_name = Tzolkin_day_name[(total_days % 20)]; *day_number = tzolkin_day_number; *day_name = tzolkin_day_name; } int main() { int N; cin >> N; cout << N << endl; for (int i = 0; i < N; ++i) { Date date; char dot; scanf("%d%c%d%s%d", &date.day, &dot, &date.month, Haab_month_name, &date.year); int total_days = haab_to_days(date.day, Haab_month_name, date.year); int tzolkin_day_number; const char *tzolkin_day_name; days_to_tzolkin(total_days, &tzolkin_day_number, &tzolkin_day_name); printf("%d %s %d\n", tzolkin_day_number, tzolkin_day_name, date.year); } return 0; }
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值