📜  C++类和对象

📅  最后修改于: 2020-12-17 05:10:20             🧑  作者: Mango


C++编程的主要目的是向C编程语言添加面向对象,而类是C++的主要功能,它支持面向对象的编程,通常称为用户定义类型。

一个类用于指定对象的形式,它结合了数据表示形式和用于将该数据处理到一个整齐的包中的方法。一个类中的数据和函数称为该类的成员。

C++类定义

定义类时,将为数据类型定义一个蓝图。这实际上并没有定义任何数据,但是确实定义了类名的含义,即,类的对象将由什么组成,以及可以对该对象执行什么操作。

类定义以关键字class开头,后跟类名。和班级身体,用大括号括起来。类定义之后必须是分号或声明列表。例如,我们使用关键字定义Box数据类型,如下所示-

class Box {
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};

关键字public确定了紧随其后的类的成员的访问属性。可以从类外部在类对象范围内的任何位置访问公共成员。您还可以将类的成员指定为私有受保护的成员,我们将在小节中讨论。

定义C++对象

类提供了对象的设计图,因此基本上是从类创建对象的。我们使用与声明基本类型的变量完全相同的声明类型来声明类的对象。以下语句声明Box类的两个对象-

Box Box1;          // Declare Box1 of type Box
Box Box2;          // Declare Box2 of type Box

对象Box1和Box2都将拥有自己的数据成员副本。

访问数据成员

可以使用直接成员访问运算符(。)访问类对象的公共数据成员。让我们尝试以下示例以使事情变得清晰起来-

#include 

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 <

编译并执行上述代码后,将产生以下结果-

Volume of Box1 : 210
Volume of Box2 : 1560

重要的是要注意,不能使用直接成员访问运算符(。)直接访问私有成员和受保护成员。我们将学习如何访问私有成员和受保护成员。

类和对象的详细信息

到目前为止,您已经对C++类和对象有了一个非常基本的想法。还有一些与C++类和对象有关的有趣概念,我们将在下面列出的各个小节中进行讨论-

Sr.No Concept & Description
1 Class Member Functions

A 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 Modifiers

A class member can be defined as public, private or protected. By default members would be assumed as private.

3 Constructor & Destructor

A 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 Constructor

The 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 Functions

A friend function is permitted full access to private and protected members of a class.

6 Inline Functions

With 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 Pointer

Every object has a special pointer this which points to the object itself.

8 Pointer to C++ Classes

A 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 Class

Both data members and function members of a class can be declared as static.