「DG数据圈聊ROS 2 Humble」EP27: C++与C的区别

机器人操作系统ROS 2 Humble中可以通过使用C++编写收发程序。消息的发送和接受,让不同组件间的消息传递成为可能,通过获取消息,对环境以及执行指令进行了解,机器人就会明确下一步的行为。

这里给出ROS 2 Humble C++消息收发程序的实例代码, 然后介绍一个C++与C的区别列表

想了解C的小伙伴,可以看看我之前的系列文章: 「DG数据圈聊ROS 2 Humble」EP21 到 EP26.

本文主要分以下几个部分:

  • ROS 2 Humble – C++ Publisher node 代码
  • ROS 2 Humble – C++ Subscript node 代码
  • C++ 与 C语言的区别

ROS 2 Humble – C++ Publisher node 代码

#include #include #include #include #include “rclcpp/rclcpp.hpp”#include “std_msgs/msg/string.hpp”using namespace std::chrono_literals;/* This example creates a subclass of Node and uses std::bind() to register a* member function as a callback from the timer. */class MinimalPublisher : public rclcpp::Node{ public: MinimalPublisher() : Node(“minimal_publisher”), count_(0) { publisher_ = this->create_publisher(“topic”, 10); timer_ = this->create_wall_timer( 500ms, std::bind(&MinimalPublisher::timer_callback, this)); } private: void timer_callback() { auto message = std_msgs::msg::String(); message.data = “Hello, world! ” + std::to_string(count_++); RCLCPP_INFO(this->get_logger(), “Publishing: ‘%s'”, message.data.c_str()); publisher_->publish(message); } rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher::SharedPtr publisher_; size_t count_;};int main(int argc, char * argv[]){ rclcpp::init(argc, argv); rclcpp::spin(std::make_shared()); rclcpp::shutdown(); return 0;}

ROS 2 Humble – C++ Subscript node 代码

#include #include “rclcpp/rclcpp.hpp”#include “std_msgs/msg/string.hpp”using std::placeholders::_1;class MinimalSubscriber : public rclcpp::Node{ public: MinimalSubscriber() : Node(“minimal_subscriber”) { subscription_ = this->create_subscription( “topic”, 10, std::bind(&MinimalSubscriber::topic_callback, this, _1)); } private: void topic_callback(const std_msgs::msg::String & msg) const { RCLCPP_INFO(this->get_logger(), “I heard: ‘%s'”, msg.data.c_str()); } rclcpp::Subscription::SharedPtr subscription_;};int main(int argc, char * argv[]){ rclcpp::init(argc, argv); rclcpp::spin(std::make_shared()); rclcpp::shutdown(); return 0;}

C++ 与 C语言的区别

先介绍一下C++ 与 C的6大主要区别,然后会给出一个详细的列表。

1 介绍

C 由 Dennis Ritchie 于 1969 年左右在 AT&T 贝尔实验室开发。

C++ 由 Bjarne Stroustrup 于 1979 年开发。

2 语言类型

C 是过程编程。

C++ 支持过程和面向对象的编程范式。

3 OOP 功能支持

由于 C 不支持 OOP 概念,因此它不支持多态性、封装和继承。

C++ 支持多态、封装和继承,因为它是一种面向对象的编程语言

4 数据安全

由于 C 不支持封装,因此数据表现为自由实体,可以由外部代码操作。

C++可通过封装隐藏数据,以确保按预期使用数据结构和运算符。

5 驱动型

C 一般称为函数驱动语言。

C++ 被称为对象驱动语言。

6 支持的功能

C 不支持函数和运算符重载,也没有命名空间功能和引用变量功能。

C++ 支持函数和运算符重载,还具有命名空间功能和引用变量功能。

以下是softwaretestinghelp给出的C与C++具体区别列表:

No

Characteristics

C

C++

1

Type of programming

Procedural language

Object-Oriented programming language.

2

Programming Approach

Top-down approach

Bottom-up approach

3

Application development

Good for embedded devices, system-level coding etc.

Good for networking, server-side applications, gaming, etc.

4

File Extension

.c

.cpp

5

Compatibility with each other

Not Compatible with C++.

Compatible with C as C++ is a subset of C.

6

Compatibility with other languages

Not compatible

Compatible

7

Ease of coding

Allows us to code everything.

Comes with highly advanced Object-Oriented concepts.

8

Data Security

Negligible

High

9

Program pision

Program pided into functions.

Program pided into classes and objects.

10

Standard I/O operations

scanf/printf

cin/cout

11

Focus/emphasis

Emphasizes on functions and/or processes.

Emphasizes on data rather than functions.

12

The main() function

Can call main through other functions.

Not possible to call main from any point.

13

Variables

To be declared at the beginning of the function.

Can be declared anywhere in the program.

14

Global variables

Multiple declarations

No multiple declarations.

15

Reference Variables and pointers

Only Pointers

Both

16

Enumerations

Only integer types.

Distinct type

17

Strings

Supports only char[]

Supports string class which is immutable.

18

Inline function

Not supported

Supported

19

Default arguments

Not supported

Supported

20

Structures

Cannot have functions as structure members.

Can have functions as structure members.

21

Classes and Objects

Not supported

Supported

22

Data Types

Only built-in and primitive data types are supported. No Boolean and string types.

Boolean and string types supported in addition to built-in data types.

23

Function overloading

Not supported

Supported

24

Inheritance

Not supported

Supported

25

Functions

Does not support functions with default arrangements.

Supports functions with default arrangements.

26

Namespace

Not supported

Supported

27

Source code

Free-format

Originally taken from C plus object-oriented.

28

Abstraction

Not present

Present

29

Information hiding

Not supported

Supported

30

Encapsulation

Not supported

Supported

31

Polymorphism

Not supported

Supported

32

Virtual function

Not supported

Supported

33

GUI programming

Using the Gtk tool.

Using the Qt tools.

34

Mapping

Cannot easily map data and functions.

Data and functions can be easily mapped.

35

Memory management

Malloc(), calloc(), free() functions.

New() and delete() operators.

36

Default headers

Stdio.h

iostream header

37

Exception/error handling

No direct support.

Supported

38

Keywords

Supports 32 keywords.

Supports 52 keywords.

39

Templates

Not supported

Supported

今天就介绍到这里。

接下来打算再介绍一下Python3编程的基础知识,关于ROS 2接下来的实战介绍,以后有机会再一点点介绍。

欢迎点赞关注哦。

本文作者:头条号DG数据圈,公众号德国数据圈

参考资料:

  • https://docs.ros.org/en/humble/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html
  • https://www.tutorialspoint.com/difference-between-c-and-cplusplus
  • https://www.softwaretestinghelp.com/c-vs-cpp/
  • https://softwareengineering.stackexchange.com/questions/113295/when-to-use-c-over-c-and-c-over-c
  • 郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
    (0)
    用户投稿
    上一篇 2022年6月17日
    下一篇 2022年6月17日

    相关推荐

    联系我们

    联系邮箱:admin#wlmqw.com
    工作时间:周一至周五,10:30-18:30,节假日休息