linux技术干货,技术干货十三:WSL Ubuntu Linux OOP基本概念

本文介绍了C++中的面向对象编程(OOP)概念,包括类与结构体的区别,并通过示例代码详细展示了如何定义类及类中的成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WSL Ubuntu Linux OOP基本概念

If you already know C structures, then learning about OOP concepts will not take much time. In C structures, we can group different data types—such as integer, float, and string—into a single, user-defined data type. Similar to structures, C++ has an enhanced version of structs that has a provision to define functions. This enhanced struct version is called the C++ class. Each instance of the C++ Class is called an object. An object is simply a copy of the actual class. There are several properties associated with objects, which are called object-oriented programming concepts. The main OOP concepts are explained with C++ code next.

如果我们熟悉C语言的结构,那么学习OOP就不需要花太多时间。在C语言结构中,我们可以将不同的数据类型(如integer、float和string)分组为单个用户定义的数据类型。C++与之有相似的结构,但是与C语言相比又有增强的功能,那就是具有定义函数的作用。这个增强的结构版本被称为C++类。C++类的每个实例都被称为对象,对象只是实际类的副本。有几个与对象相关联的属性,被称为面向对象编程概念。对于OOP概念的详细讲解,我们采用C++代码来进行解释。

3935375423.jpg

1 类与结构体的区别

Before going through the OOP concepts, let’s look at the basic differences between a struct and a class. The following code example helps us to differentiate them.

在学习OOP概念概念之前,我们先看看结构和类之间的基本区别。下面这个代码示例将帮助区分它们。#include

#include

using namespace std;

struct Robot_Struct

{

int id;

int no_wheels;

string robot_name;

};

class Robot_Class

{

public:

int id;

int no_wheels;

string robot_name;

void move_robot();

void stop_robot();

};

void Robot_Class::move_robot()

{

cout<

}

void Robot_Class::stop_robot()

{

cout<

}

int main()

{

Robot_Struct robot_1;

Robot_Class robot_2;

robot_1.id = 2;

robot_1.robot_name = "Mobile robot";

robot_2.id = 3;

robot_2.robot_name = "Humanoid robot";

cout<

robot_name<

cout<

robot_name<

robot_2.move_robot();

robot_2.stop_robot();

return 0;

}

This code defines a struct and a class. The struct name is Robot_Struct and the class name is Robot_Class.

此代码定义结构体和类。结构体名称为Robot_Struct,类名称为Robot_Class。

The following code shows how to define a structure. It defines a struct with variables such as id, name, and the number of wheels.

下面这段代码展示了怎样定义结构体。他用id, robot_name, no_wheels等变量定义了一个结构体。struct Robot_Struct

{

int id;

int no_wheels;

string robot_name;

};

As you know, a struct has a name, and the declaration of all the variables are inside it. Let’s check the definition of a class.

如您所知,结构有一个名称,并且所有变量的声明都在其中。让我们查看一下类的定义。class Robot_Class

{

public:

int id;

int no_wheels;

string robot_name;

void move_robot();

void stop_robot();

};

So, what is the difference between the two? A struct can only define a different variables, but a class can define different variables and declare functions too. The class shown declares two functions along with the variables. So where is the definition of each function? We can either define the function inside the class or outside the class. The

standard practice is to keep the definition external to the class definition to keep the class definition short.

那么,这两者有什么区别呢?结构体只能定义不同的变量,但类也可以定义不同的变量和声明函数。所示的类声明了两个函数以及变量。那么每个函数的定义在哪里呢?我们可以在类内部或类外部定义函数。标准的做法是将函数定义在类外部,以保持类定义简单明了。

The following code shows the definitions of functions mentioned inside the class.

下面的代码显示了类中提到的函数的定义。void Robot_Class::move_robot()

{

cout<

}

void Robot_Class::stop_robot()

{

cout<

}

In the function definition, the first term is the return data type, followed by the class name, and then the function name followed by ::, which states that the function is inside the class. Inside the function definition, we can add our code. This particular code prints a message. You have seen the function definition inside a class. The next step is to learn how to read/write to variables and functions.

在函数定义中,第一个术语是返回数据类型,后跟类名,然后是函数名,后跟::,表示函数在类中。在函数定义中,我们可以添加代码。图示的代码表示输出一条消息。您已经看到了类中的函数定义。下一步就是学习如何读/写变量和函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值