单位做网站需要多少钱,贵州建设厅考试网站二建成绩查询,百度一下百度网页版进入,公司做网站的优势作业1#xff1a;编程题#xff1a;
以下是一个简单的比喻#xff0c;将多态概念与生活中的实际情况相联系#xff1a;
比喻#xff1a;动物园的讲解员和动物表演
想象一下你去了一家动物园#xff0c;看到了许多不同种类的动物#xff0c;如狮子、大象、猴子等。现在…作业1编程题
以下是一个简单的比喻将多态概念与生活中的实际情况相联系
比喻动物园的讲解员和动物表演
想象一下你去了一家动物园看到了许多不同种类的动物如狮子、大象、猴子等。现在动物园里有一位讲解员他会为每种动物表演做简单的介绍。
在这个场景中我们可以将动物比作是不同的类而每种动物表演则是类中的函数。而讲解员则是一个基类他可以根据每种动物的特点和表演进行相应的介绍。
具体过程如下
定义一个基类 Animal其中有一个虛函数perform)用于在子类中实现不同的表演行为。
代码
#include iostreamusing 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 iostreamusing 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思维导图