C ++ definition of class and class

zhaozj2021-02-11  202

In object-oriented programming, there are often professional nouns that often contact, objects; what is the class, what is an object? What is the application of the program? Class is the core of object-oriented programming, it is actually a new data type, which is also a tool for realizing an abstract type, because class is a data type implemented by an abstract data type. Class is an abstraction of a certain type of object; and the object is an instance of a certain type, so the class and objects are closely related. There is no object that is out of the object, and there is no object that is not dependent on class.

What is class

Category is a complex data type that is a set of different types of data and an operation associated with these data. This is a bit like the structure in the C language. The only difference is that the structure does not define the "data-related operation", "data-related operation" is what we usually see, so the class has higher Abstract, data in the class is hidden, and the class also has encapsulation.

The structure of the class (i.e., the composition) is used to determine the behavior of a class of objects, and these behavior is determined by the internal data structure and related operations. These behaviors are described by an operational interface (i.e. us usually we see the membership function), the user only cares about the function of the interface (that is, we only care about the functions of each member function of the class), How it is not interested in it. The operation interface is also called the service provided by such objects to other objects.

Class definition format

The definition format of the class is generally divided into the description and the implementation part. The description is used to explain the instructions for the members in this class, including the description of the data member and the member function. The member function is used to operate the data member, also known as "method." The implementation part is used to define the member function. In summary, the explanation will tell the user "what", and the implementation is to tell the user "how to do".

The general definition format of the class is as follows:

Class

{

PUBLIC:

Private:

}

The above format will be described below: Class is the keyword defined by the definition class, is an identifier, usually using a string beginning with T letters as class name. A pair of curly brackets is a class of instructions (including the foregoing header) indicating the members of the class. A member of the class contains two parts of the data member and the member function. From access to access, members of the class are divided into three categories: public (private), private (private) and protected. Public members are illustrated by public, and the public part is often some operations (ie, member functions), which is the interface function provided to the user. This part of the member can be referenced in the program. Private members use private to explain that private parts are usually some data members, which are used to describe the properties of the objects in this class, and the user is unable to access them, only member functions or special instructions can be referenced. They are parts that are used to hide. Protected (protected) will be described later.

Keyword public, private, and protected are called access control or access control modifiers. They have nothing to do in the order of the class (ie, a pair of curly brackets), and allow multiple appearances, with them to illustrate access to the class members.

Among them, the implementation of each member function> is the implementation portion in the class definition, which contains all the definitions of the functions illustrated within the specifier. If a member of a member function is defined, the implementation portion will not appear. If all member functions are defined within the class, the implementation portion can be omitted.

Here is an example of a date class definition:

Class TDATE

{

PUBLIC:

Void setDate (int y, int m, int d);

Int isleapyear ();

Void print ();

Private:

Int year, month, day;

// Class implementation

Void Tdate :: SetDate (int y, int m, int d)

{

Year = Y;

Month = m;

Day = d;

}

Int tdate :: isleApyear ()

{

Return (Year% 4 == 0 && Year% 100! = 0) || (Year% 400 == 0);

}

Void tdate :: print ();

{

COUT << Year << "." << Month << "." << day << endl;

}

The scope operator here :: is used to identify which class belongs to which class belongs to.

The definition of this class can also be as follows:

Class TDATE

{

PUBLIC:

Void SetDate (int y, int m, int d)

{year = y; month = m; day = d;}

Int isleapyear ()

{RETURN (Year% 4 == 0 && Year% 100! = 0) || (Year% 400 == 0);}

Void print ()

{COUT << Year << "." << Month << "." << day << endl;}

Private:

Int Yeay, Month, DAY;

}

This is written in the class for the implementation of the member function (ie, the definition of the function), so the implementation part of the class is omitted. If the member function is defined outside the class, you should add the identity of the function belonging in front of the function header, which uses the scope operator ::.

Messages that should be noted when defined

1. Do not initialize the defined data members in the class.

2, the type of data in the class can be arbitrary, including integer, floating point, character type, array, pointer, and reference. It can also be an object. Another object of the class can be a member of this class, but the objects of their own class are not possible, and their own pointers or references are possible. When a class object is used for this class, if another class is defined, it is necessary to explain in advance.

3. Generally, in the category, the public member will explain that they are concerned, and they will explain that private members are not interested in users. When describing data members, generally according to the size of the data member, by small to large, this can improve the time and space utilization.

4. Place the description of the class or the entire definition portion (including the implementation portion) in a header file is often habitually accustomed to.

2001-5-29 18:02 Shantou

转载请注明原文地址:https://www.9cbs.com/read-3812.html

New Post(0)