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;
}