C++类和对象——多态详解

本文详细介绍了C++中的多态概念,包括基本语法、原理剖析,以及纯虚函数、抽象类和虚析构的运用。通过示例展示了如何在计算机类、制作饮品和电脑组装场景中实现多态功能。
摘要由CSDN通过智能技术生成

目录

1.多态的基本语法

2.多态的原理剖析

示例:计算机类 

3.纯虚函数和抽象类

示例:制作饮品

4.虚析构和纯虚析构 

示例:电脑组装


1.多态的基本语法

代码示例:

#include<bits/stdc++.h>
using namespace std;

class music
{
public:
//加virtual实现晚绑定
	virtual void listen()
	{
		cout << "listen music" << endl;
	}
};

class rock : public music
{
public:
	void listen()
	{
		cout << "listen rock" << endl;
	}
};

//不加virtual是地址早绑定
//如果想要听rock就应该让地址晚绑定,或者说在运行阶段进行绑定
void dolisten(music &m)
{
	m.listen();
}

int main()
{
	rock r;
	dolisten(r);
	
	return 0;
}

 

2.多态的原理剖析

空类的大小为1 

加了virtual后会变为4或8个字节,这恰好是一个指针的大小

#include<bits/stdc++.h>
using namespace std;

class music
{
public:
//加virtual实现晚绑定
	virtual void listen()
	{
		cout << "listen music" << endl;
	}
};

class rock : public music
{
public:
	void listen()
	{
		cout << "listen rock" << endl;
	}
};

//不加virtual是地址早绑定
//如果想要听rock就应该让地址晚绑定,或者说在运行阶段进行绑定
void dolisten(music &m)
{
	m.listen();
}

int main()
{
	rock r;
	cout << sizeof(rock) << endl;
	
	return 0;
}

 

vfptr是一个虚函数(表)指针

当子类重写父类的虚函数,子类中的虚函数表位置会替换成子类的虚函数地址

简单理解:就是父类会将它的函数继承给它的子类,但当子类使用virtual(多态后)会将父类继承给它的覆盖掉

多态使用条件:父类的指针或者引用指向子类对象

示例:计算机类 

#include<bits/stdc++.h>
using namespace std;

class calculator
{
public:

	virtual int getresult()
	{
		return 0;
	}
	
	int x;
	int y;
};

class addcalculator : public calculator
{
public:

	int getresult()
	{
		return x + y;
	}
};

class subcalculator : public calculator
{
public:

	int getresult()
	{
		return x - y;
	}
};

int main()
{
	//多态实现计算机的加法和减法
	calculator *c = new addcalculator;
	c -> x = 50,c -> y = 60;
	cout << c -> getresult() << endl;
	delete c;
	
	c = new subcalculator;
	c -> x = 60,c -> y = 50;
	cout << c -> getresult() << endl;
	delete c;
	
	return 0;
}

 

3.纯虚函数和抽象类

 

 

#include<bits/stdc++.h>
using namespace std;

class base
{
public:
	
	virtual void func() = 0;
};

class son : public base
{
public:

	virtual void func()
	{
		cout << "func函数调用" << '\n';
	}
};

int main()
{
	base *b = new son;
	b -> func();
	delete b;
	
	return 0;
}

 

示例:制作饮品

 

#include<bits/stdc++.h>
using namespace std;

class drinking
{
public:

	//煮水
	virtual void boil() = 0;
	
	//冲泡
	virtual void brew() = 0;
	
	//倒入杯中
	virtual void pourintocup() = 0;
	
	//加入佐料
	virtual void addseasoning() = 0;
	
	//制作饮品
	void makedrinking()
	{
		boil();
		brew();
		pourintocup();
		addseasoning();
	}
};

class coffee : public drinking
{
public:
	//煮水
	virtual void boil(){
		cout << "烧开水" << endl;
	}
	
	//冲泡
	virtual void brew(){
		cout << "加咖啡粉" << endl;
	}
	
	//倒入杯中
	virtual void pourintocup(){
		cout << "倒入杯中" << endl;
	}
	
	//加入佐料
	virtual void addseasoning(){
		cout << "加糖,加牛奶" << endl;
	}
	
};

void make(drinking *d)
{
	d -> makedrinking();
	delete d;
}

int main()
{
	//开始做咖啡
	make(new coffee);
	
	return 0;
}

 

4.虚析构和纯虚析构 

#include<bits/stdc++.h>
using namespace std;

class animal
{
public:
	animal()
	{
		cout << "animal构造函数调用" << endl;
	}
	
	//利用虚析构解决父类指针释放子类对象时不干净的问题
	// virtual ~animal()
	// {
		// cout << "animal虚析构函数调用" << endl;
	// }
	
	virtual ~animal() = 0;
	
	//纯虚析构,需要声明也需要实现
	//有了纯虚析构之后,这个类也属于抽象类,无法实例化对象
	virtual void speak() = 0;
};

animal::~animal()
{
	cout << "animal的纯虚构函数调用" << endl;
}

class cat : public animal
{
public:
	cat(string name)
	{
		cout << "cat构造函数调用" << endl;
		this -> name = new string(name);
	}
	
	virtual void speak()
	{
		cout << *name << "在说话" << endl;
	}
	~cat()
	{
		cout << "cat析构函数调用" << endl;
		if(name != NULL)
		{
			delete name;
			name = NULL;
		}
	}
	string *name;
};

int main()
{
	animal *a = new cat("Tom");
	a -> speak();
	delete a;
	
	return 0;
}

 

 

示例:电脑组装

#include<bits/stdc++.h>
using namespace std;

class cpu
{
public:

	virtual void calculator() = 0;
};

class videocard
{
public:

	virtual void display() = 0;
};

class memory
{
public:

	virtual void storage() = 0;
};

class computer
{
public:

	computer(cpu *c,videocard *v,memory *m)
	{
		this -> c = c;
		this -> v = v;
		this -> m = m;
	}
	
	void make()
	{
		c -> calculator();
		v -> display();
		m -> storage();
	}
	
	~computer()
	{
		if(c != NULL)
		{
			delete c;
			c = NULL;
		}
		
		if(v != NULL)
		{
			delete v;
			v = NULL;
		}
		
		if(m != NULL)
		{
			delete m;
			m = NULL;
		}
	}
	
private:

	cpu *c;
	videocard *v;
	memory *m;
};

class intelcpu : public cpu
{
public:

	virtual void calculator()
	{
		cout << "intelcpu" << endl;
	}
};

class amd : public videocard
{
public:

	virtual void display()
	{
		cout << "amdvideocard" << endl;
	}
};

class kingston : public memory
{
public:

	virtual void storage()
	{
		cout << "kingstonmemory" << endl;
	}
};

int main()
{
	cpu *c = new intelcpu;
	videocard *v = new amd;
	memory *m = new kingston;
	
	computer *com = new computer(c,v,m);
	com -> make();
	delete com;
	
	return 0;
}

 

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏箱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值