33 lines
797 B
C++
33 lines
797 B
C++
|
/*
|
|||
|
* @Author: iR
|
|||
|
* @Date: 2022-03-15 07:30:23
|
|||
|
* @LastEditors: iR
|
|||
|
* @LastEditTime: 2022-03-15 17:05:13
|
|||
|
* @FilePath: \Code\1-3a\main.cpp
|
|||
|
* @Description:
|
|||
|
* @Mail: i@iridium.cyou
|
|||
|
*/
|
|||
|
|
|||
|
#include <iostream>
|
|||
|
using namespace std;
|
|||
|
class Parent
|
|||
|
{ // 基类
|
|||
|
public:
|
|||
|
void f1() { cout << "This is parent-f1" << endl; }
|
|||
|
virtual void f2() { cout << "This is parent-f2" << endl; } // 声明f2是虚函数
|
|||
|
} parent;
|
|||
|
class Child : public Parent
|
|||
|
{ // 派生类
|
|||
|
public:
|
|||
|
void f1() { cout << "This is child-f1" << endl; }
|
|||
|
void f2() { cout << "This is child-f2" << endl; }
|
|||
|
} child;
|
|||
|
|
|||
|
int main(int argc, char *argv[])
|
|||
|
{
|
|||
|
Parent *p = new Child; // 定义一个基类指针
|
|||
|
p->f1(); // 执行哪个f1?
|
|||
|
p->f2(); // 执行哪个f2?
|
|||
|
delete p;
|
|||
|
return 0;
|
|||
|
}
|