标签:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:d.cpp
*作 者:张旺华
*完成日期:2015年5月27日
*版 本 号:v1.0
*/
#include <iostream>
using namespace std;
class Vehicle //交通工具
{
public:
void run() const
{ cout << "run a vehicle. "<<endl; }
};
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();
return 0;
}
运行结果:
知识点应用及心得:
直接用对象访问成员函数,采用静态关系直接调用本身成员函数,
指向基类的指针访问成员函数:,由于基类中run()不是虚函数,
Vehicle *vp说明,vp是指向基类的,在调用是必定调用基类成员函数
标签:
原文地址:http://blog.csdn.net/wh201458501106/article/details/46045165