Cplusplus 简明教程
C++ Classes and Objects
C 编程的主要目的是为 C 编程语言添加面向对象,而类是支持面向对象编程的 C 语言的中央特征,通常称为用户自定义类型。
The main purpose of C programming is to add object orientation to the C programming language and classes are the central feature of C that supports object-oriented programming and are often called user-defined types.
类用于指定对象的格式,它将数据表示和用于处理数据的函数合并到一个简洁的包中。类中的数据和函数称为该类的成员。
A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
C++ Class Definitions
当你定义一个类时,你就定义了一个数据类型的蓝图。这实际上并不定义任何数据,但它确实定义了类名意味着什么,即类的对象将由什么组成,以及可以在这样的对象上执行什么操作。
When you define a class, you define a blueprint for a data type. 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.
类定义以关键字 class 开始,后跟类名;类正文用一对花括号括起来。类定义后面必须是分号或声明列表。例如,我们使用关键字 class 定义了 Box 数据类型,如下所示 −
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows −
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
关键字 public 决定了跟随它的类成员的访问属性。可以在类的作用域内的任何地方从类外部访问公共成员。你还可以将类的成员指定为 private 或 protected ,我们将在小节中讨论它们。
The keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section.
Define C++ Objects
类为对象提供了蓝图,因此本质上对象是从类创建的。我们声明一个类的对象,其声明类型与声明基本类型的变量完全相同。下面的语句声明了 Box 类的两个对象 −
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box −
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
这两个对象 Box1 和 Box2 都将有自己数据成员的副本。
Both of the objects Box1 and Box2 will have their own copy of data members.
Accessing the Data Members
可以使用直接成员访问运算符 (.) 访问类对象的公共数据成员。让我们尝试以下示例以阐明问题 −
The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear −
#include <iostream>
using namespace std;
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Volume of Box1 : 210
Volume of Box2 : 1560
需要注意的是,不能直接使用直接成员访问运算符 (.) 访问私有和受保护成员。我们将学习如何访问私有和受保护成员。
It is important to note that private and protected members can not be accessed directly using direct member access operator (.). We will learn how private and protected members can be accessed.
Classes and Objects in Detail
到目前为止,你已经对 C 类和对象有了非常基本的了解。与 C 类和对象相关的还有更有趣的概念,我们将在下面列出的各个小节中讨论它们 −
So far, you have got very basic idea about C Classes and Objects. There are further interesting concepts related to C Classes and Objects which we will discuss in various sub-sections listed below −
Sr.No |
Concept & Description |
1 |
Class Member FunctionsA member function of a class is a function that has its definition or its prototype within the class definition like any other variable. |
2 |
Class Access ModifiersA class member can be defined as public, private or protected. By default members would be assumed as private. |
3 |
Constructor & DestructorA class constructor is a special function in a class that is called when a new object of the class is created. A destructor is also a special function which is called when created object is deleted. |
4 |
Copy ConstructorThe copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. |
5 |
Friend FunctionsA friend function is permitted full access to private and protected members of a class. |
6 |
Inline FunctionsWith an inline function, the compiler tries to expand the code in the body of the function in place of a call to the function. |
7 |
this PointerEvery object has a special pointer this which points to the object itself. |
8 |
Pointer to C++ ClassesA pointer to a class is done exactly the same way a pointer to a structure is. In fact a class is really just a structure with functions in it. |
9 |
Static Members of a ClassBoth data members and function members of a class can be declared as static. |