C++多态例子_虚函数

#include<iostream>
using namespace std;

class parent {
public:
	virtual void fun() {
		cout << "我是父类虚函数" << endl;
	}
	void fun2() {
		cout << "我是父类普通函数" << endl;
	}
};
class child :public parent {
public:
	virtual void fun() {
		cout << "我是子类虚函数" << endl;
	}
	void fun2() {
		cout << "我是子类普通函数" << endl;
	}
};

int main() {
	parent* ptr = new child();
	ptr->fun();//调用子类函数
	ptr->fun2();//调用父类函数
	system("pause");
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/yichengming/p/11427811.html