c语言面向对象编程中的类
Object Oriented programming is a programming style that is associated with the concept of Class, Objects and various other concepts revolving around these two, like Inheritance, Polymorphism, Abstraction, Encapsulation etc.
面向对象编程是一种与类,对象以及围绕这两个主题的各种其他概念相关的编程风格,例如继承,多态性,抽象,封装等。
In the video below, we have explained the basic concepts of Object Oriented Programming with help of a very easy to understand example. If you want to skip the video, everything is covered below as well.
在下面的视频中,我们通过一个非常易于理解的示例解释了面向对象编程的基本概念。 如果您想跳过视频,下面还将介绍所有内容。
演示地址
Let us try to understand a little about all these, through a simple example. Human Beings are living forms, broadly categorized into two types, Male and Female. Right? Its true. Every Human being(Male or Female) has two legs, two hands, two eyes, one nose, one heart etc. There are body parts that are common for Male and Female, but then there are some specific body parts, present in a Male which are not present in a Female, and some body parts present in Female but not in Males.
让我们尝试通过一个简单的示例来稍微了解一下所有这些内容。 人类是生命形式,大致分为男性和女性两种类型。 对? 这是真的。 每个人(男性或女性)都有两条腿,两只手,两只眼睛,一只鼻子,一颗心脏等。男性和女性都有一些共同的身体部位,但男性有一些特定的身体部位雌性中不存在,而雌性中有一些身体部位,雄性中不存在。
All Human Beings walk, eat, see, talk, hear etc. Now again, both Male and Female, performs some common functions, but there are some specifics to both, which is not valid for the other. For example : A Female can give birth, while a Male cannot, so this is only for the Female.
所有人类都走路,吃饭,看,说话,听等。现在,男性和女性都执行一些共同的功能,但是两者都有一些特定的规定,对另一个无效。 例如:女性可以生子,而男性则不能,所以这仅适用于女性。
Human Anatomy is interesting, isn't it? But let's see how all this is related to C++ and OOPS. Here we will try to explain all the OOPS concepts through this example and later we will have the technical definitons for all this.
人体解剖学很有趣,不是吗? 但是,让我们看看所有这些与C ++和OOPS有何关系。 在这里,我们将尝试通过此示例解释所有OOPS概念,稍后,我们将对所有这些进行技术定义。
类 (Class)
Here we can take Human Being as a class. A class is a blueprint for any functional entity which defines its properties and its functions. Like Human Being, having body parts, and performing various actions.
在这里,我们可以将人类作为一个类。 类是定义其属性和功能的任何功能实体的蓝图。 像人类一样,具有身体部位,并执行各种动作。
遗产 (Inheritance)
Considering HumanBeing a class, which has properties like hands, legs, eyes etc, and functions like walk, talk, eat, see etc. Male and Female are also classes, but most of the properties and functions are included in HumanBeing, hence they can inherit everything from class HumanBeing using the concept of Inheritance.
考虑到HumanBeing是一类,它具有诸如手,腿,眼睛等的属性,并且具有走路,说话,吃饭,看东西等功能。 Male和Female也是一类,但是HumanBeing中包含了大多数的属性和功能,因此它们可以使用继承概念继承类HumanBeing中的所有内容。
对象 (Objects)
My name is Abhishek, and I am an instance/object of class Male. When we say, Human Being, Male or Female, we just mean a kind, you, your friend, me we are the forms of these classes. We have a physical existence while a class is just a logical definition. We are the objects.
我的名字是Abhishek,我是Male类的实例/对象 。 当我们说“人类,男性或女性”时,我们的意思是一种,您,您的朋友,我,我们就是这些课程的形式。 当一个类只是一个逻辑定义时,我们就有了物理存在。 我们是对象。
抽象化 (Abstraction)
Abstraction means, showcasing only the required things to the outside world while hiding the details. Continuing our example, Human Being's can talk, walk, hear, eat, but the details are hidden from the outside world. We can take our skin as the Abstraction factor in our case, hiding the inside mechanism.
抽象意味着在隐藏细节的同时只向外界展示必需的东西。 继续我们的例子, 人类可以说话,走路,听,吃,但是细节对外界是隐藏的。 在我们的案例中,我们可以将皮肤作为抽象因素,隐藏内部机制。
封装形式 (Encapsulation)
This concept is a little tricky to explain with our example. Our Legs are binded to help us walk. Our hands, help us hold things. This binding of the properties to functions is called Encapsulation.
用我们的例子来解释这个概念有些棘手。 我们的双腿被束缚以帮助我们行走。 我们的双手,帮助我们握紧东西。 属性与函数的这种绑定称为封装。
多态性 (Polymorphism)
Polymorphism is a concept, which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done. Both the ways have different terms for them.
多态是一个概念,它使我们可以通过更改完成方式或更改完成使用的部件来重新定义其工作方式。 两种方式都有不同的用语。
If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is called Overloading.
如果我们用双手而不是腿走路,那么这里将改变用于执行某些动作的零件。 因此,这称为Overloading 。
And if there is a defined way of walking, but I wish to walk differently, but using my legs, like everyone else. Then I can walk like I want, this will be called as Overriding.
如果有明确的步行方式,但我希望以与众不同的方式,以不同的方式走路,但是要用我的腿。 然后,我可以按照自己的意愿行走,这称为“ 覆盖” 。
OOPS概念定义 (OOPS Concept Definitions)
Now, let us discuss some of the main features of Object Oriented Programming which you will be using in C++(technically).
现在,让我们讨论一下您将在C ++(技术上)中使用的面向对象编程的一些主要功能。
Objects 对象 Classes 班级 Abstraction 抽象化 Encapsulation 封装形式 Inheritance 遗产 Overloading 超载 Exception Handling 异常处理
对象 (Objects)
Objects are the basic unit of OOP. They are instances of class, which have data members and uses various member functions to perform tasks.
对象是OOP的基本单位。 它们是类的实例,具有数据成员并使用各种成员函数来执行任务。
类 (Class)
It is similar to structures in C language. Class can also be defined as user defined data type but it also contains functions in it. So, class is basically a blueprint for object. It declare & defines what data variables the object will have and what operations can be performed on the class's object.
它类似于C语言的结构。 也可以将类定义为用户定义的数据类型,但其中也包含函数。 因此,类基本上是对象的蓝图。 它声明并定义对象将拥有哪些数据变量以及可以对类的对象执行哪些操作。
抽象化 (Abstraction)
Abstraction refers to showing only the essential features of the application and hiding the details. In C++, classes can provide methods to the outside world to access & use the data variables, keeping the variables hidden from direct access, or classes can even declare everything accessible to everyone, or maybe just to the classes inheriting it. This can be done using access specifiers.
抽象是指仅显示应用程序的基本功能并隐藏细节。 在C ++中,类可以为外部世界提供访问和使用数据变量的方法,使变量不被直接访问隐藏,或者类甚至可以声明所有人都可以访问的所有内容,也可以仅声明继承它的类。 这可以使用访问说明符来完成。
封装形式 (Encapsulation)
It can also be said data binding. Encapsulation is all about binding the data variables and functions together in class.
也可以说是数据绑定。 封装就是将类中的数据变量和函数绑定在一起。
遗产 (Inheritance)
Inheritance is a way to reuse once written code again and again. The class which is inherited is called the Base class & the class which inherits is called the Derived class. They are also called parent and child class.
继承是一种反复使用一次编写的代码的方法。 继承的类称为基类,而继承的类称为Derived类。 他们也被称为父母和孩子班。
So when, a derived class inherits a base class, the derived class can use all the functions which are defined in base class, hence making code reusable.
因此,当派生类继承基类时,派生类可以使用基类中定义的所有功能,从而使代码可重用。
多态性 (Polymorphism)
It is a feature, which lets us create functions with same name but different arguments, which will perform different actions. That means, functions with same name, but functioning in different ways. Or, it also allows us to redefine a function to provide it with a completely new definition. You will learn how to do this in details soon in coming lessons.
它是一项功能,使我们可以创建具有相同名称但参数不同的函数,这些函数将执行不同的操作。 这意味着,具有相同名称的功能,但以不同的方式起作用。 或者,它还允许我们重新定义一个函数以为其提供全新的定义。 在接下来的课程中,您将很快详细了解如何执行此操作。
异常处理 (Exception Handling)
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at runtime.
异常处理是OOP的一项功能,用于处理运行时产生的未解决的异常或错误。
翻译自: /cpp/cpp-and-oops-concepts.php
c语言面向对象编程中的类