Cplusplus 简明教程
C++ Object Oriented
C++ 编程的主要目的是向 C 编程语言添加面向对象功能,而 C 编程语言本身就是最强大的编程语言之一。
The prime purpose of C++ programming was to add object orientation to the C programming language, which is in itself one of the most powerful programming languages.
纯面向对象编程的核心是创建代码中具有一定属性和方法的对象。在设计 C++ 模块时,我们会尝试将整个世界视为由对象组成的。例如,汽车是一个对象,它具有一些属性,例如颜色、车门数量等。它还具有一些方法,例如加速、刹车等。
The core of the pure object-oriented programming is to create an object, in code, that has certain properties and methods. While designing C++ modules, we try to see whole world in the form of objects. For example a car is an object which has certain properties such as color, number of doors, and the like. It also has certain methods such as accelerate, brake, and so on.
形成面向对象编程基础的一些基本概念 -
There are a few principle concepts that form the foundation of object-oriented programming −
Object
这是面向对象编程的基本单元。作为对象操作的数据和函数都被捆绑在一个称为对象的基本单元中。
This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.
Class
定义类时,您正在为对象定义一个蓝图。这实际上不会定义任何数据,但它确实定义了类名的含义,也就是说类对象由什么组成以及可以在这样的对象上执行什么操作。
When you define a class, you define a blueprint for an object. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
Abstraction
数据抽象是指只向外界提供必要的信息并隐藏其背景详细信息,即在程序中表示所需信息而不提供详细信息。
Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.
例如,数据库系统隐藏了有关如何存储、创建和维护数据的一些详细信息。类似地,C++ 类向外界提供各种方法,而不会提供有关这些方法和数据的内部细节。
For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.
Encapsulation
封装是在同一位置放置数据及其处理该数据的功能。使用过程语言时,哪些函数处理哪些变量并不总是明确的,但面向对象编程为您提供了一个框架,可以将数据和相关函数放在同一对象中。
Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.
Inheritance
面向对象编程最有用的一方面是代码可重用性。顾名思义,继承是从现有类(即称为基类的现有类)形成新类,新类被称为派生类。
One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.
这是面向对象编程的一个非常重要的概念,因为此功能有助于减少代码大小。
This is a very important concept of object-oriented programming since this feature helps to reduce the code size.
Polymorphism
以不同的方式使用运算符或函数,换句话说,赋予运算符或函数不同的含义或功能被称为多态性。多重是指许多。即单一函数或运算符根据用法以多种不同方式作用称为多态性。
The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.