#include <iostream>
using namespace std;
class Vehicle
{
public:
void run() const { cout << "run a vehicle. "<<endl; } //(2) run()为虚函数
};
class Car: public Vehicle
{
public:
void run() const {cout << "run a car. "<<endl; }
};
class Airplane: public Vehicle
{
public:
void run() const {cout << "run a airplane. "<<endl;}
};
int main()
{
cout<<"(a) 直接用对象访问成员函数: "<<endl;
Vehicle v;
v.run();
Car car;
Airplane airplane;
car.run();
airplane.run();
cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;
Vehicle *vp;
vp=&car;
vp->run();
vp=&airplane;
vp->run();
system("pause");
return 0;
}
(任务1.2)如果将Vehicle类的定义修改为虚函数,其余不变,请写出程序的执行结果,并在上机时对照理解
class Vehicle
{public:
virtual void run() const { cout << "run a vehicle. "<<endl; } //(2) run()为虚函数
};
(任务1.3)如果将Vehicle类的定义修改为纯虚函数,找出main()函数中将使编译出错的行删除(或改为注释),请写出程序的执行结果,并在上机时对照理解
class Vehicle
{public:
virtual void run() const = 0; //(3) run()为纯虚函数
};
(任务1.4)提交博文,记录实验过程和结果,用自己的话概括你对虚函数、多态性和抽象类的理解。
【任务2】下面给出了基类Animal和main()函数。
(任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。
(任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p = new Animal();将出错,将此行删除。
(任务2.3)每一个Animal的派生类都有一个“名字”数据成员,改造上面的程序,将这一数据成员作为抽象类Animal数据成员被各派生类使用。
#include "iostream"
#include<string>
using namespace std;
class Animal
{
public:
virtual void cry() {cout<<"不知哪种动物,让我如何学叫?"<<endl;}
};
int main( )
{
Animal *p;
p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处)
Mouse m("Jerry"); p=&m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!
Cat c("Tom"); p=&c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!
Dog d("Droopy"); p=&d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!
Giraffe g("Gill"); p=&g; p->cry(); //输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来!
system("pause");
return 0;
}
【任务3】写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的mian()函数,求出定义的几个几何体的面积和。
int main()
{
Circle c1(12.6),c2(4.9); //建立Circle类对象c1,c2,参数为圆半径
Rectangle r1(4.5,8.4),r2(5.0,2.5); //建立Rectangle类对象r1,r2,参数为矩形长、宽
Triangle t1(4.5,8.4),t2(3.4,2.8); //建立Triangle类对象t1,t2,参数为三角形底边长与高
Shape *pt[6]={&c1,&c2,&r1,&r2,&t1,&t2}; //定义基类指针数组pt,各元素指向一个派生类对象
double areas=0.0; //areas为总面积
for(int i=0; i<6; i++)
{
areas=areas+pt[i]->area();
}
cout<<"totol of all areas="<<areas<<endl; //输出总面积
system("pause");
return 0;
}
1.2
#include <iostream>
using namespace std;
class Vehicle
{
public:
virtual void run() const { cout << "run a vehicle. "<<endl; } //(2) run()为虚函数
};
class Car: public Vehicle
{
public:
void run() const {cout << "run a car. "<<endl; }
};
class Airplane: public Vehicle
{
public:
void run() const {cout << "run a airplane. "<<endl;}
};
int main()
{
cout<<"(a) 直接用对象访问成员函数: "<<endl;
Vehicle v;
v.run();
Car car;
Airplane airplane;
car.run();
airplane.run();
cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;
Vehicle *vp;
vp=&car;
vp->run();
vp=&airplane;
vp->run();
system("pause");
return 0;
}
1.3
#include <iostream>
using namespace std;
class Vehicle
{
public:
virtual void run() const = 0; //(3) run()为纯虚函数
};
class Car: public Vehicle
{
public:
void run() const {cout << "run a car. "<<endl; }
};
class Airplane: public Vehicle
{
public:
void run() const {cout << "run a airplane. "<<endl;}
};
int main()
{
cout<<"(a) 直接用对象访问成员函数: "<<endl;
/*Vehicle v;
v.run();*/
Car car;
Airplane airplane;
car.run();
airplane.run();
cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;
Vehicle *vp;
vp=&car;
vp->run();
vp=&airplane;
vp->run();
system("pause");
return 0;
}
1.4
虚函数:就是能够让你运用指针或者引用时,调用函数时虽然指针的类不发生改变,但当指向的对象发生改变时,他所调用的函数也随之发生改变。
多态性:在不同的类中能够用同一个名字,并且不发生覆盖。
抽象类:只要包含纯虚函数的类都是抽象类。