C++——多态

1.面向对象中期望的行为

(1)根据实际的对象类型判断如何调用重写函数

(2)父类指针或引用指向父类对象,则调用父类中定义的函数,指向子类对象则调用子类中定义的重写函数

2.多态概念:

(1)根据实际的对象类型决定函数调用的具体目标

(2)同样的调用语句在实际运行时有多种不同的表现形态

3.C++与C语言的区别是,C++支持多态,而C语言不支持

(1)通过使用virtual关键字对多态进行支持

(2)被virtual声明的函数被重写后具有多态特性

(3)被virtual声明的函数叫做虚函数

复制代码
#include <iostream>
using namespace std;
class Parent {
public:
	virtual void print() {
		cout << "I'm Parent" << endl;
	}
};
class Child :public Parent {
public:
	void print() {
		cout << "I'm Child" << endl;
	}
};
void how_to_print(Parent* p) {
	p->print();
}
int main() {
	Child c;
	Parent p;
	c.print(); // I'm Child
	p.print(); //I'm Parent
	how_to_print(&p); //I'm Parent
	how_to_print(&c); //I'm Child

	return 0;
}

多态的意义:

(1)在程序运行过程中展现出动态的特性

(2)函数重写必须多态实现,否则没有意义

(3)多态是面向对象组件化程序设计的基础特性

静态联编:在程序的编译期间就能确定具体的函数调用,如函数重载

动态联编:在程序实际运行后才能确定具体的函数调用,如函数重写

复制代码
#include <iostream>
using namespace std;
class Parent {
public:
	virtual void func() {
		cout << "void func()" << endl;
	}
	virtual void func(int i) {
		cout << "void func(int i)" << i << endl;
	}
	virtual void func(int i, int j) {
		cout << "void func(int i, int j):" << "(" << i << "," << j << ")" << endl;
	}
};
class Child :public Parent {
public:
	void func(int i , int j) {
		cout << "void func(int i, int j):" << i + j << endl;
	}
	void func(int i, int j, int k) {
		cout << "void func(int i, int j, int k):" << i + j + k << endl;
	}
};
void run(Parent* p) {
	p->func(1, 2); // 展现多态的特性 动态联编
}
int main() {
	Parent p;
	p.func(); //静态联编
	p.func(1); //静态联编
	p.func(1, 2); //静态联编

	cout << endl;

	Child c;
	c.func(1, 2); //静态联编
	cout << endl;
	
	run(&p);
	run(&c);
	return 0;
}

运行结果:

void func()

void func(int i)1

void func(int i, int j):(1,2)

void func(int i, int j):3

void func(int i, int j):(1,2)

void func(int i, int j):3

示例:

复制代码
#include <iostream>
using namespace std;

class Boss {
public:
	int fight() {
		int ret = 10;
		cout << "Boss::fight():" << ret << endl;
		return ret;
	}
};
class Master {
public:
	virtual int eightSwordkill() {
		int ret = 8;
		cout << "Master::eightSwordkill():" << ret << endl;
		return ret;
	}
};
class NewMaster:public Master {
public:
	int eightSwordkill() {
		int ret = Master::eightSwordkill()*2;
		cout << "NewMaster::eightSwordkill():" << ret << endl;
		return ret;
	}
};
void field_pk(Master* master, Boss* boss) {
	int k = master->eightSwordkill();
	int b = boss->fight();
	if (k < b) {
		cout << "Master is killed" << endl;
	}
	else {
		cout << "Boss is killed" << endl;
	}
}
int main() {
	Master master;
	Boss boss;
	cout << "Master vs Boss" << endl;
	field_pk(&master, &boss);
	cout << "NewMaster vs Boss" << endl;
	NewMaster newMaster;
	field_pk(&newMaster, &boss);
	return 0;
}
相关推荐
yiangL_8 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
JosieBook8 小时前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
Huangjin007_9 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
geovindu10 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk816311 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾11 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
炸膛坦客11 小时前
单片机/C/C++八股:(三十三)取别名
c语言·c++·单片机
听情歌落俗12 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力12 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
ShineWinsu12 小时前
对于Qt:QFontDialog的解析
c++·qt·面试·gui·字体·客户端·qfontdialog