多态成立的三个条件

#include"iostream"
using namespace std;

class Parent01
{
public:
	virtual void func() {
		cout << "父类的func" << endl;
	}
};

class Child01 :public Parent01//1.继承
{
public:
	virtual void func() {//2.重写
		cout << "子类的func" << endl;
	}
};

void objTest(Parent01* p) {//3.父类指针指向子类对象
	p->func();
}

int main() {
	Child01 c1;
	objTest(&c1);
	Parent01 p1;
	objTest(&p1);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/itswrj/article/details/88619514