技术干货十五:Class Access Modifier & C++ Inheritance 类访问修饰符与C++继承
Class Access Modifier & C++ Inheritance 类访问修饰符与C++继承
1 Class Access Modifier 类访问修饰符
Inside the class, you may have seen a keyword called public:
. It is called an access modifier. The following snippet code of the access modifier used in the previous article.
在类内部,我们可以看到一个名为public:
的关键字。它被称为访问修饰符。以下这段代码片段在以前文章中出现过。详细参考WSL Ubuntu Linux OOP基本概念中完整代码。
class Robot_Class
{
Public:
int id;
int no_wheels;
This feature is also called data hiding. By setting the access modifier, we can limit the usage of functions defined inside it. There are three types of access modifiers in a class.
此功能也称为数据隐藏。通过设置访问修饰符,我们可以限制在其中定义的函数的使用。类中有三种类型的访问修饰符。
public:
A public member can access from anywhere outside the class within a program. We can directly access the public variable without even writing functions.
public:
可以从程序中的类之外的任何地方访问。我们可以直接访问public变量,甚至无需编写函数。private:
Variables or functions cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.
private:
无法访问变量或函数,甚至无法从类外部查看。只有类和friend函数可以访问私有成员。protected:
Access is very similar to private members, but the difference is the child class can access the members. The concepts of child class/ derived class are discussed in the upcoming section.
protected:
访问与private非常相似,但区别在于子类可以访问成员。下一节将讨论子类/派生类的概念。
Access modifiers help you group variables, which you can keep visible or hidden in the class.
访问修饰符有助于对变量分组,这些变量可以在类中保持可见或隐藏。
2 C++ Inheritance C++继承
Inheritance is another important concept in OOP. If you have two or more classes, and you want to have the functions inside those classes in a new class, you can use the inheritance property. By using the inheritance property, you can reuse the function inside the existing classes in a new class. The new class that is going to inherit an existing class is called a derived class. The existing class is called a base class.
继承是OOP中的另一个重要概念。如果您有两个或多个类,并且您希望在一个新类中包含这些类中的函数,则可以使用继承属性。通过使用继承属性,可以在新类中重用现有类内的函数。将要继承现有类的新类称为派生类。现有类称为基类。
A class can be inherited through public, protected, or private inheritance
. The following explains each type of inheritance.
可以通过public,protected或private继承
继承类。 以下解释了每种类型的继承。
Public inheritance:
When we derive a class from a public base class, the public members of the base class become public members of the derived class, and protected members of the base class become protected members of the derived class. The private members of the base class can never be accessed in the derived class. It can access through calls to the public and protected members of the base class.
Public 继承:
当我们从公共基类派生类时,基类的公共成员成为派生类的公共成员,基类的受保护成员成为派生类的受保护成员。基类的私有成员永远不能在派生类中访问。它可以通过调用访问基类的公共成员和受保护成员。Protected inheritance:
When we inherit using the protected base class, the public and protected members of the base class become protected members of the derived class.
Protected 继承:
当我们使用受保护的基类继承时,基类的公共成员和受保护成员将成为派生类的受保护成员。Private inheritance:
When deriving from a private base class, public and protected members of the base class become private members of the derived class.
Private 继承:
从私有基类派生时,基类的公共成员和受保护成员将成为派生类的私有成员。
The following code is example of C++ Public Inheritance
以下代码是C ++公共继承的示例
#include <iostream>
#include <string>
using namespace std;
class Robot_Class
{
public:
int id;
int no_wheels;
string robot_name;
void move_robot();
void stop_robot();
};
class Robot_Class_Derived: public Robot_Class
{
public:
void turn_left();
void turn_right();
};
void Robot_Class::move_robot()
{
cout<<"Moving Robot"<<endl;
}
void Robot_Class::stop_robot()
{
cout<<"Stopping Robot"<<endl;
}
void Robot_Class_Derived::turn_left()
{
cout<<"Robot Turn left"<<endl;
}
void Robot_Class_Derived::turn_right()
{
cout<<"Robot Turn Right"<<endl;
}
int main()
{
Robot_Class_Derived robot;
robot.id = 2;
robot.robot_name = "Mobile robot";
cout<<"Robot ID="<<robot.id<<endl;
cout<<"Robot Name="<<robot.robot_name<<endl;
robot.move_robot();
robot.stop_robot();
robot.turn_left();
robot.turn_right();
return 0;
}
So in this example we are creating a new class called Robot_Class_Derived
, which is derived from a base class called Robot_Class
. The public inheritance is done using a public keyword followed by the base class name (see the following code). There should be a :
after the derived class name, followed by a public keyword and a base class name.
所以在这个例子中,我们创建了一个名为Robot_Class_Derived
的新类,它是从一个名为Robot_Class
的基类派生而来的。公共继承是使用一个公共关键字加上基类名来完成的(请参阅下面的代码)。派生类名后面应该有一个:
,后跟一个public关键字和一个基类名。
class Robot_Class_Derived: public Robot_Class
{
public:
void turn_left();
void turn_right();
};
If you chose public inheritance
, you can access the public and protected variables and functions
of the base class; in this case, Robot_Class
.
如果您选择public inheritance
,您可以访问基类的公共和受保护的变量和函数
;在本例中,是Robot_Class
。
We are using the same class that we used in the first example. The definition of each function in the derived class is given in the following code.
我们使用的是与第一个示例中使用的相同的类。派生类中每个函数的定义在以下代码中给出。
void Robot_Class_Derived::turn_left()
{
cout<<"Robot Turn left"<<endl;
}
void Robot_Class_Derived::turn_right()
{
cout<<"Robot Turn Right"<<endl;
}
Now let’s look at how to access the functions inside the derived class (see the following code)
现在让我们看一下如何访问派生类中的函数(参见下面的代码)
Robot_Class_Derived robot;
robot.id = 2;
robot.robot_name = "Mobile robot";
cout<<"Robot ID="<<robot.id<<endl;
cout<<"Robot Name="<<robot.robot_name<<endl;
robot.move_robot();
robot.stop_robot();
robot.turn_left();
robot.turn_right();
Here we are creating an object of Robo_Class_Derived
called robot
. If you go through the code, you can understand that we didn’t declare id
and robot_name
variables in the Robo_Class_Derived
, but it was defined in the Robo_Class
. Using inheritance property, we can access the variable of Robo_Class
inside its derived class.
在这里,我们创建了一个名为robot
的Robo_Class_Derived
对象。如果我们浏览代码,可以理解我们没有在Robo_Class_Derived
中声明id
和robot_name
变量,但它们是在Robo_Class
中定义的。使用继承属性,我们可以在其派生类中访问Robo_Class
的变量。
Let’s look at the output of the code. We can save this code as class_inherit.cpp
and compile it by using the following command.
让我们看看代码的输出。我们可以将这段代码保存为class_inherit.cpp
然后使用下面的命令编译。
g++ class_inherit.cpp -o class_inherit
./class_inherit
This gives you the output shown in the following Figure, without showing any errors. This means that the public inheritance is working fine.
这将给出如下图所示的输出,没有显示任何错误。这意味着公共继承能正常工作。
If you look at the output, we are getting all the messages from functions, defined in the base class and the derived class. We can also access the base class variables and set the values.
如果查看输出,我们将从函数中获取所有消息,这些消息在基类和派生类中定义。我们还可以访问基类变量并设置值。