求c++程序,要包含多态,封装体和继承的

来源:百度知道 编辑:UC知道 时间:2024/05/02 04:40:01
里面要包含要包含多态,封装体和继承,什么都可以,最好能运行的,在线等啊,拜托了,别给求面积的那种,简单点的好,给高分!
大哥啊,你这个没输出啊,我要看看结果的啊,你改下,改了,马上给你分!拜托了,谢谢了!
输出什么都可以,有意义就可以,主要是输出的要直观,要有这3个要求的特点,谢谢了!

我把这程序改一下, C++的多态包抱函数的重载和覆盖.
animal的构造函数有两个同名,这个是重载.
基类中的print()定义为虚函数, dog中对它进行重写就是
覆盖.

#include <iostream.h>

class animal
{
int heavy;//体重
public:
animal(int h):heavy(h)
{ printf("class animal was created with parameter heavy %d.",heavy)
}

animal()
{heavy=0;
printf("class animal was created with parameter heavy %d.",heavy)}
virtual print() //声时为虚函数
{
printf("This is base class");
}
};

class dog: public animal
{
public:
dog(int h=0):animal(h){}
void print() // 重写虚函数
{
printf("this is super class");
}
};

main()
{
animal c;
dog d;
dog d(10);

animal *prt = &c;
prt->print();
*prt = &d;
prt->print();
}

#include <iostream.h>

class animal
{
int heavy;//体重
publi