This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/SoftwareDesign/Lab1/1-3a/main.cpp
2022-04-26 14:51:03 +08:00

33 lines
No EOL
797 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @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;
}