Latest web development tutorials

C ++ Class & Objects

C ++ language based on C adds object-oriented programming, C ++ supports object-oriented programming. C ++ class is a core feature, often referred to as user-defined types.

Class is used to form the specified object, which contains data representation and method for processing data. Class data and methods referred to as members of the class. Function in a class are called members of the class.

C ++ class definition

The definition of a class is essentially a blueprint to define a data type. This does not actually define any data, but it defines the name of the class what it means, that is, it defines a class of objects, including what and what operations can be performed on the object.

The class definition is based on keywords toclass at the beginning, followed by the class.Body of the class is included in a pair of curly brackets. It must be followed by a semicolon or a statement list after the class definition. For example, we use theclass keyword to define Box data type, as follows:

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

Keywordpublic determines the access attribute class members.In the class object scope, public members outside the class is accessible. You can also specify the class members isprivate or protected,that we will later explain.

C ++ object definitions

Class provides the blueprint for an object, so basically, is based on the object class to create. Declared object of the class, like the variable declaration of the same basic type. The following statement declares the class Box two objects:

Box Box1;          // 声明 Box1,类型为 Box
Box Box2;          // 声明 Box2,类型为 Box

Box1 and Box2 objects have their own data members.

Access data members

Public data members of a class of objects can directly use the member access operator (.) To access. To better understand these concepts, let's try the following examples:

#include <iostream>

using namespace std;

class Box
{
   public:
      double length;   // 长度
      double breadth;  // 宽度
      double height;   // 高度
};

int main( )
{
   Box Box1;        // 声明 Box1,类型为 Box
   Box Box2;        // 声明 Box2,类型为 Box
   double volume = 0.0;     // 用于存储体积
 
   // box 1 详述
   Box1.height = 5.0; 
   Box1.length = 6.0; 
   Box1.breadth = 7.0;

   // box 2 详述
   Box2.height = 10.0;
   Box2.length = 12.0;
   Box2.breadth = 13.0;

   // box 1 的体积
   volume = Box1.height * Box1.length * Box1.breadth;
   cout << "Box1 的体积:" << volume <<endl;

   // box 2 的体积
   volume = Box2.height * Box2.length * Box2.breadth;
   cout << "Box2 的体积:" << volume <<endl;
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Box1 的体积:210
Box2 的体积:1560

It should be noted that the members of the private and protected members can not use direct member access operator (.) To directly access. We will learn in subsequent tutorials on how to access private members and protected members.

Detailed Class & Objects

So far, we have a basic understanding of C ++ classes and objects. The following list also lists some other C ++ classes and objects related concepts, you can click the appropriate link to learn.

概念描述
类成员函数 类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。
类访问修饰符 类成员可以被定义为 public、private 或 protected。默认情况下是定义为 private。
构造函数 & 析构函数 类的构造函数是一种特殊的函数,在创建一个新的对象时调用。类的析构函数也是一种特殊的函数,在删除所创建的对象时调用。
C++ 拷贝构造函数 拷贝构造函数,是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。
C++ 友元函数 友元函数可以访问类的 private 和 protected 成员。
C++ 内联函数 通过内联函数,编译器试图在调用函数的地方扩展函数体中的代码。
C++ 中的 this 指针 每个对象都有一个特殊的指针 this,它指向对象本身。
C++ 中指向类的指针 指向类的指针方式如同指向结构的指针。实际上,类可以看成是一个带有函数的结构。
C++ 类的静态成员 类的数据成员和函数成员都可以被声明为静态的。