testvirtualclass.cpp

来自「< Visual C++数据库经典开发实例精解>>的实例源码」· C++ 代码 · 共 33 行

CPP
33
字号
// TestVirtualClass.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"
class A{
public:
	virtual void outnews(int n){
		cout<<"基类A中输出的数:"<<n<<"\n";
	}
	virtual void outnews2(int n){
		cout<<"基类A中输出的数:"<<n<<"\n";
	}
};
class B:public A{
public:
	void outnews(int n){
		cout<<"子类B中输出的数:"<<n<<"\n";		
	}
};
int main(int argc, char* argv[])
{
	B objb;
	A obja,*a_ptr;
	a_ptr=&obja;
	a_ptr->outnews(100);//调用基类outnews函数
	a_ptr=&objb;
	a_ptr->outnews(100);//调用子类outnews函数	
	a_ptr->outnews2(200);//调用基类outnews2函数
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?