浅谈浅谈C++ 基类指针和子类指针的相互赋值基类指针和子类指针的相互赋值
首先,给出基类animal和子类fish
//==============================================================
// animal.h
//
// author : zwq
// describe: 非虚函数情况下,将子类指针赋给积累指针,验证最终调用
// 基类函数还是子类函数。
//==============================================================
#ifndef ANIMAL_H
#define ANIMAL_H
//===============================================================
//
// animal
// 动物基类
//
//===============================================================
class animal
{
public:
void breathe(); // 非虚函数
};
//===============================================================
//
// animal
// 鱼类,集成于动物基类
//
//===============================================================
class fish : public animal
{
public:
void breathe(); // 非虚函数
};
#endif
#include "StdAfx.h"
#include <iostream>
#include "Animal.h"
using namespace std;
//===============================================================
//
// animal
// 动物基类
//
//===============================================================
void animal::breathe()
{
cout << "animal breathe" << endl;
}
//===============================================================
//
// animal
// 鱼类,集成于动物基类
//
//===============================================================
void fish::breathe()
{
cout << "fish bubble" << endl;
}