mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-05-11 18:38:17 +08:00
english
This commit is contained in:
43
english/basic_content/c_poly/c++_examp.cpp
Normal file
43
english/basic_content/c_poly/c++_examp.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @file c++_examp.cpp
|
||||
* @brief c++中的多态
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-08-06
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
class A
|
||||
{
|
||||
public:
|
||||
virtual void f()//Implement a virtual function
|
||||
{
|
||||
cout << "Base A::f() " << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class B:public A // 必须为共有继承,否则后面调用不到,class默认为私有继承!
|
||||
{
|
||||
public:
|
||||
virtual void f()//Virtual function implementation, virtual keyword in subclass can not be appearence
|
||||
{
|
||||
cout << "Derived B::f() " << endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
A a;//Base class object
|
||||
B b;//an object of derived type
|
||||
|
||||
A* pa = &a;//The parent class pointer points to the parent object
|
||||
pa->f();//Call the function of the parent class
|
||||
pa = &b; //The parent class pointer points to the subclass object, which is implemented in polymorphism
|
||||
|
||||
|
||||
pa->f();//Call the function with the same name of the derived class
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user