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;
}
相关推荐
人道领域10 分钟前
【LeetCode刷题日记】131.分割回文串,动态规划优化
java·开发语言·leetcode
z落落22 分钟前
C# 接口 interface (多接口实现、类+接口、成员重名)
java·开发语言
王老师青少年编程33 分钟前
信奥赛C++提高组csp-s之搜索进阶(迭代加深IDDFS)
c++·csp·信奥赛·csp-s·提高组·iddfs·埃及分数
liulilittle1 小时前
我从 BBRv1 到 KCC 的思考
网络·c++·tcp/ip·计算机网络·tcp·bbr·通信
落羽的落羽1 小时前
【项目】JsonRpc框架——开发实现1(细节功能、字段定义、抽象层、具象层)
linux·服务器·网络·c++·人工智能·算法·机器学习
handler011 小时前
【算法】并查集(普通/扩展/带权)模板与例题
数据结构·c++·笔记·算法·c·图论·查并集
知识的宝藏1 小时前
Xpaht self::div 轴语法
开发语言
keykey6.1 小时前
卷积神经网络(CNN):让AI学会“看“
开发语言·人工智能·深度学习·机器学习
IsJunJianXin2 小时前
谷歌搜索cookie NID逆向生成
开发语言·python·google搜索·sgss·nid-cookie·算法生成nid·google-cookie
weikecms2 小时前
美团霸王餐报名API接口
java·开发语言