虚函数与调用和重写基类成员函数
来源:互联网 发布:数据库登录界面代码 编辑:程序博客网 时间:2024/06/09 23:06
//Overriding Boss
//Demonstrates calling and overriding base member functions
#include <iostream>
using namespace std;
class Enemy
{
public:
Enemy(int damage = 10);
void virtual Taunt() const; //made virtual to be overridden
void virtual Attack() const; //made virtual to be overridden
private:
int m_Damage;
};
Enemy::Enemy(int damage):
m_Damage(damage)
{}
void Enemy::Taunt() const
{
cout << "The enemy says he will fight you.\n";
}
void Enemy::Attack() const
{
cout << "Attack! Inflicts " << m_Damage << " damage points.";
}
class Boss : public Enemy
{
public:
Boss(int damage = 30);
void virtual Taunt() const; //optional use of keyword virtual
void virtual Attack() const; //在将一个成员函数声明为虚函数,使得成员函数能如预期用于对象的指针与引用
}; //因为在派生类Boss中重写,于是在基类中声明为虚函数
Boss::Boss(int damage):
Enemy(damage) //显示调用基类enemy的构造函数,并将damage传递给它
{}
void Boss::Taunt() const //override base class member function 此处不必显示声明virtual
{
cout << "The boss says he will end your pitiful existence.\n";
}
void Boss::Attack() const //override base class member function
{
Enemy::Attack(); //显式调用基类的Attack()成员函数
cout << " And laughs heartily at you.\n";
}
int main()
{
cout << "Enemy object:\n";
Enemy anEnemy;
anEnemy.Taunt();
anEnemy.Attack();
cout << "\n\nBoss object:\n";
Boss aBoss;
aBoss.Taunt();
aBoss.Attack();
return 0;
}
//Demonstrates calling and overriding base member functions
#include <iostream>
using namespace std;
class Enemy
{
public:
Enemy(int damage = 10);
void virtual Taunt() const; //made virtual to be overridden
void virtual Attack() const; //made virtual to be overridden
private:
int m_Damage;
};
Enemy::Enemy(int damage):
m_Damage(damage)
{}
void Enemy::Taunt() const
{
cout << "The enemy says he will fight you.\n";
}
void Enemy::Attack() const
{
cout << "Attack! Inflicts " << m_Damage << " damage points.";
}
class Boss : public Enemy
{
public:
Boss(int damage = 30);
void virtual Taunt() const; //optional use of keyword virtual
void virtual Attack() const; //在将一个成员函数声明为虚函数,使得成员函数能如预期用于对象的指针与引用
}; //因为在派生类Boss中重写,于是在基类中声明为虚函数
Boss::Boss(int damage):
Enemy(damage) //显示调用基类enemy的构造函数,并将damage传递给它
{}
void Boss::Taunt() const //override base class member function 此处不必显示声明virtual
{
cout << "The boss says he will end your pitiful existence.\n";
}
void Boss::Attack() const //override base class member function
{
Enemy::Attack(); //显式调用基类的Attack()成员函数
cout << " And laughs heartily at you.\n";
}
int main()
{
cout << "Enemy object:\n";
Enemy anEnemy;
anEnemy.Taunt();
anEnemy.Attack();
cout << "\n\nBoss object:\n";
Boss aBoss;
aBoss.Taunt();
aBoss.Attack();
return 0;
}
- 虚函数与调用和重写基类成员函数
- C++继承类和基类之间成员函数和虚函数调用机制
- C++继承类和基类之间成员函数和虚函数调用机制
- 成员函数的重载/重写与隐藏
- 成员函数调用虚函数
- 类成员函数调用
- 类成员函数的重载、重写、和覆盖区别
- 类成员函数的重载、重写、和覆盖区别
- 虚函数/静态成员/常成员函数重写
- 构造函数调用成员函数&成员函数是虚函数
- 类的成员函数和全局函数的互相调用
- setTimeout调用类成员函数
- C++调用类成员函数
- 成员函数中调用虚函数
- 函数指针调用类的成员函数
- 函数指针调用类的成员函数
- 虚成员函数和非虚成员函数调用方式有什么不同?
- C++类对象内存模型与成员函数调用分析
- eclispse错误 Access restriction: The type XXXX is not accessible due to restriction on require
- sscanf用法详解-hdu2072
- jdk,tomcat安装时环境变量的配置
- vhdl中延时器的编写
- [关于Unity崩溃原因的说明转载文章]
- 虚函数与调用和重写基类成员函数
- construct2游戏引擎介绍
- Java连接池
- C++ Primer总结
- iOS开发何如在调试的时候轻松找到程序在哪里崩溃
- ubuntu 文件系统结构
- I/O多路复用 select模型
- android note
- UVa 10391 - Compound Words