覆盖代码简示

重载、覆盖、隐藏  

基类使用virtual 后子类可以省略不写,但是仍然存在,建议不这样写

#include "pch.h"
#include <iostream>
using namespace std;


class A
{
public:
	virtual void base()
	{
		cout << "base A" << endl;
	}
};
class B :public A
{
public :
	void base()
	{
		cout << "base B" << endl;
	}
};
int main()
{
	A a;
	B b;
	a.base();
	b.base();

	A *p1 = new A;
	B *p2 = new B;
	p1->base();
	p2->base();
	
	// B *p3 = new A; 错误
	// p3->base();

	A *p4 = new B; // 多态的实现(其中的一中),基类可以调用子类的函数
	p4->base();

	system("pause");
    std::cout << "Hello World!\n"; 
}

结果:

base A
base B
base A
base B
base B
请按任意键继续. . .

注释:最后一个可以看到结果(多态),上述代码会出现内存泄露的问题,就不改了。故意留下,原因是虚析构函数

 

往复式全覆盖路径规划是一种常用的路径规划算法,它可以在给定的地图上找到一条覆盖所有目标点的最短路径。以下是一个简单的往复式全覆盖路径规划的代码示例: ```python import numpy as np def calculate_distance(point1, point2): # 计算两个点之间的距离 return np.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) def find_shortest_path(points): # 初始化路径和总距离 path = [] total_distance = 0 # 选择起始点 current_point = points[0] path.append(current_point) # 循环遍历剩余的目标点 while len(points) > 1: # 初始化最短距离和下一个目标点的索引 shortest_distance = float('inf') next_point_index = -1 # 遍历剩余的目标点,找到距离当前点最近的点 for i in range(1, len(points)): distance = calculate_distance(current_point, points[i]) if distance < shortest_distance: shortest_distance = distance next_point_index = i # 更新当前点和路径 current_point = points[next_point_index] path.append(current_point) total_distance += shortest_distance # 移除已经访问过的目标点 del points[next_point_index] # 返回最短路径和总距离 return path, total_distance # 测试代码 points = [(0, 0), (1, 1), (2, 2), (3, 3)] shortest_path, total_distance = find_shortest_path(points) print("最短路径:", shortest_path) print("总距离:", total_distance) ``` 这段代码实现了一个简单的往复式全覆盖路径规划算法。它首先定义了一个计算两个点之间距离的函数`calculate_distance`,然后通过遍历目标点来找到最短路径。在每次循环中,它选择距离当前点最近的目标点,并更新当前点和路径。最后,它返回最短路径和总距离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ma_Hong_Kai

微信 2936729162

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

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

打赏作者

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

抵扣说明:

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

余额充值