在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    「DG數(shù)據(jù)圈聊ROS 2 Humble」EP27: C++與C的區(qū)別

    機器人操作系統(tǒng)ROS 2 Humble中可以通過使用C++編寫收發(fā)程序。消息的發(fā)送和接受,讓不同組件間的消息傳遞成為可能,通過獲取消息,對環(huán)境以及執(zhí)行指令進行了解,機器人就會明確下一步的行為。

    這里給出ROS 2 Humble C++消息收發(fā)程序的實例代碼, 然后介紹一個C++與C的區(qū)別列表。

    想了解C的小伙伴,可以看看我之前的系列文章: 「DG數(shù)據(jù)圈聊ROS 2 Humble」EP21 到 EP26.

    本文主要分以下幾個部分:

    • ROS 2 Humble – C++ Publisher node 代碼
    • ROS 2 Humble – C++ Subscript node 代碼
    • C++ 與 C語言的區(qū)別

    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語言的區(qū)別

    先介紹一下C++ 與 C的6大主要區(qū)別,然后會給出一個詳細的列表。

    1 介紹

    C 由 Dennis Ritchie 于 1969 年左右在 AT&T 貝爾實驗室開發(fā)。

    C++ 由 Bjarne Stroustrup 于 1979 年開發(fā)。

    2 語言類型

    C 是過程編程。

    C++ 支持過程和面向?qū)ο蟮木幊谭妒健?/p>

    3 OOP 功能支持

    由于 C 不支持 OOP 概念,因此它不支持多態(tài)性、封裝和繼承。

    C++ 支持多態(tài)、封裝和繼承,因為它是一種面向?qū)ο蟮木幊陶Z言

    4 數(shù)據(jù)安全

    由于 C 不支持封裝,因此數(shù)據(jù)表現(xiàn)為自由實體,可以由外部代碼操作。

    C++可通過封裝隱藏數(shù)據(jù),以確保按預期使用數(shù)據(jù)結(jié)構(gòu)和運算符。

    5 驅(qū)動型

    C 一般稱為函數(shù)驅(qū)動語言。

    C++ 被稱為對象驅(qū)動語言。

    6 支持的功能

    C 不支持函數(shù)和運算符重載,也沒有命名空間功能和引用變量功能。

    C++ 支持函數(shù)和運算符重載,還具有命名空間功能和引用變量功能。

    以下是softwaretestinghelp給出的C與C++具體區(qū)別列表:

    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編程的基礎(chǔ)知識,關(guān)于ROS 2接下來的實戰(zhàn)介紹,以后有機會再一點點介紹。

    歡迎點贊關(guān)注哦。

    本文作者:頭條號DG數(shù)據(jù)圈,公眾號德國數(shù)據(jù)圈

    參考資料:

  • 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
  • 鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年6月17日 21:07
    下一篇 2022年6月17日 21:09

    相關(guān)推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時間:周一至周五,10:30-18:30,節(jié)假日休息