Skip to content

Auto技术

专注于汽车制造技术分享

Primary Menu
  • 文章首页
  • 汽车制造
    • 制造体系
    • 工业仿真
    • 工业控制
    • 工业设备
    • 工艺方案
  • 杂七杂八
  • 技术干货
  • 项目管理
  • 营销技术
  • 网赚联盟
  • 读书学习
  • 文章合集
  • 社交媒体
    • 知乎
    • 微信小程序
    • 微信公众号
  • Home
  • 技术干货
  • 技术干货十四:WSL Ubuntu Linux C++类与对象
  • 技术干货

技术干货十四:WSL Ubuntu Linux C++类与对象

Auto 2019-08-30
2277

WSL Ubuntu Linux C++类与对象

1 C++类与对象

This article explains how to read/write to structs and classes. The following code shows lines of code that do the job.
本篇讲解如何读/写结构体和类。下面的代码显展示了读/写结构体和类的方法

Robot_Struct robot_1;
Robot_Class robot_2;

robot_1.id = 2;
robot_1.robot_name = "Mobile robot";

robot_2.id = 3;
robot_3.robot_name = "Humanoid robot";

Similar to the struct instance, we can create an instance of a class and that is called an object.

与结构体实例类似,我们可以创建一个类的实例,称为对象。

Let’s look at Robot_Class robot_2; here, robot_2 is an object and robot_1 is an instance of the structure. Using this instance or object, we can access each variable and function. We can use the . operator to access each variable. The struct and class variables are accessed by using the . operator. If you use struct or class pointers, you have to use the -> operator to access each variable. The following code is an example.

让我们来看看Robot_Class robot_2;在这里,robot_2是一个对象,而robot_1是结构体的一个实例。使用这个实例或对象,我们可以访问每个变量和函数。我们可以使用.运算符访问每个变量。使用.运算符可以访问结构和类变量。如果使用结构或类指针,则必须使用->运算符来访问每个变量。例如下面这串代码。

Creating a C++ Object and Accessing Object by Reference
创建C++对象并通过引用访问对象

Robot_Class *robot_2;
robot_2 = new Robot_Class;
robot_2->id - 2;
robot_2->name = "Humanoid Robot";

The new operator allocates memory for the C++ object. We can access the functions inside the class and print all values by using the . operator. The following code shows how to do that.

新操作符为C++对象分配内存。我们可以访问类中的函数,并使用.运算符打印所有值。下面的代码显示了如何执行此操作。

cout<<"ID="<<robot_1.id<<"\t"<<"Robot Name"<<robot_1.
robot_name<<endl;
cout<<"ID="<<robot_2.id<<"\t"<<"Robot Name"<<robot_2.
robot_name<<endl;
robot_2.move_robot();
robot_2.stop_robot();

We can save the code as class_struct.cpp, and compile it by using the following command.
我们可以将代码另存为class_struct.cpp,并使用以下命令编译它。

另存为class_struct.cpp 参考文章WSL Ubuntu Linux OOP基本概念中的代码,将其另存为class_struct.cpp 而后编译即可。

g++ class_struct.cpp -o class_struct

./class_struct

Output of the program.JPG

Continue Reading

Previous: 技术干货十三:WSL Ubuntu Linux OOP基本概念
Next: 工业设备二:Automotive Industry Conveyors System 汽车工业输送系统

智能推荐

公众号流量主那些事儿(一)

    公众号流量主那些事儿(一)

    Auto 2019-12-14 3708
    世界上最小的焊装线/ 1982

      世界上最小的焊装线/ 1982

      Auto 2019-09-25 4557
      Friction Stir Welding (FSW) 搅拌摩擦焊

        Friction Stir Welding (FSW) 搅拌摩擦焊

        Auto 2019-09-24 4327
        Friction Welding 摩擦焊

          Friction Welding 摩擦焊

          Auto 2019-09-23 3868
          Exothermic welding 铝热焊

            Exothermic welding 铝热焊

            Auto 2019-09-17 3810
            Follow Us
            • 猜你喜欢
            • 随机文章
            • 热门文章
            • Friction Stir Welding (FSW) 搅拌摩擦焊
              • 工艺方案

              Friction Stir Welding (FSW) 搅拌摩擦焊

            • Friction Welding 摩擦焊
              • 工艺方案

              Friction Welding 摩擦焊

            • Exothermic welding 铝热焊
              • 工艺方案

              Exothermic welding 铝热焊

            • Advanced Product Quality Planning 产品质量先期策划(APQP)
              • 制造体系

              Advanced Product Quality Planning 产品质量先期策划(APQP)

            • 技术干货一:Win10 开启WSL(Windows Subsystem for Linux)并安装Ubuntu 18.04 LTS
              • 技术干货

              技术干货一:Win10 开启WSL(Windows Subsystem for Linux)并安装Ubuntu 18.04 LTS

            • Ultrasonic Welding 超声波焊接
              • 工艺方案

              Ultrasonic Welding 超声波焊接

            • 工业仿真二:Tecnomatix PDPS软件安装
              • 工业仿真

              工业仿真二:Tecnomatix PDPS软件安装

            • 工业设备一:柔性主焊线工艺及关键设备
              • 工业设备

              工业设备一:柔性主焊线工艺及关键设备

            • 技术干货一:Win10 开启WSL(Windows Subsystem for Linux)并安装Ubuntu 18.04 LTS
              • 技术干货

              技术干货一:Win10 开启WSL(Windows Subsystem for Linux)并安装Ubuntu 18.04 LTS

            文章精选

            公众号流量主那些事儿(一)2 min read
            • 网赚联盟

            公众号流量主那些事儿(一)

            Auto2019-12-14 2 3708
            世界上最小的焊装线/ 19822 min read
            • 汽车制造

            世界上最小的焊装线/ 1982

            Auto2019-09-25 2 4557
            Friction Welding 摩擦焊2 min read
            • 工艺方案

            Friction Welding 摩擦焊

            Auto2019-09-23 2 3868
            Resistance Welding 电阻焊2 min read
            • 工艺方案

            Resistance Welding 电阻焊

            Auto2019-09-12 2 3357
            Copyright © 2019. | Auto技术 | 鄂ICP备17030239号-1.