In object-oriented program design, there are frequent contact with classes, objects and other professional nouns; what is a class and what is an object? How is the program used? A class is the core of object-oriented programming, which is actually a new type of data and a tool for implementing abstract types, because a class is a data type that is implemented by means of an abstract data type. A class is an abstraction of a type of object, and an object is an instance of a class, so classes and objects are closely related. There are no classes that are detached from the object, and no objects that are not dependent on the class.
What is a class
A class is a complex data type that encapsulates a collection of different types of data and the operations associated with that data. This is a bit like the structure of C language, the only difference is that the structure does not define the "data-related operations", "data-related operations" is the "method" we often see, therefore, the class has a higher abstraction, the data in the class is hidden, the class also has encapsulation.
The structure of a class (also known as the composition of a class) is used to determine the behavior of a class of objects, which are determined by the internal data structures of the classes and related operations. These behaviors are described by an operator interface (also known as the member function of the class we see), and the user is only concerned with the function of the interface (that is, we only care about the function of each member function of the Class), and it is not interested in how it is implemented. The operation interface is also referred to as the service provided by such objects to other objects.
The definition format of the class
The definition format of a class is generally divided into a description section and an implementation part. The description section is used to describe the members in the class, including descriptions of data members and descriptions of member functions. member functions are used to manipulate data members, also known as "methods." The implementation section is used to define the member functions. In summary, the Description section tells the user what to do, and the implementation part tells the user how to do it.
The general definition format for a class is as follows:
Class
{
Public
Private
};
Here's a simple description of the above format: class is the keyword that defines the class, is an identifier, and usually begins with a T-letter string as the class name. A pair of curly braces is a description part of the class, including the preceding class header, that describes the members of the class. A member of a class contains two parts of a data member and a member function. From access rights, the members of the class are divided into: public, private, and protected (protected) three classes. Public members are described with public, which is often an operation (that is, member functions), which is the interface function provided to the user. Members of this section can be referenced in the program. Private members are described privately, and the private part is usually a data member that is used to describe the properties of the objects in the class that the user cannot access, and that only member functions or specially specified functions can refer to them, which are used to hide the part. The Protection class (protected) will be introduced later.
The keywords public,private and protected are referred to as access modifiers or access control modifiers. They are independent of the order in which they appear in the body of the class (that is, within a pair of curly braces), and are allowed to appear multiple times, using them to specify the access rights of the class members.
Where is the implementation part of the class definition, which contains all the definitions of functions that are described in the body of the class. If a member function is defined within the class, the implementation section will not appear. If all the member functions are defined within the class, the implementation part can be omitted.
An example of a date class definition is given below:
Class Tdate
{
Public
void SetDate (int y, int m, int d);
int Isleapyear ();
void Print ();
Private
int year, month, day;
}; Implementation part of the class
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::P rint ();
{
cout<}
The scope operator that appears here:: is used to identify which class A member function 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< Private:
int Yeay, month, day;
}
The implementation of the member function (that is, the definition of the function) is written in the class so that the implementation of the class is omitted. If the member function is defined outside the class body, precede the function header with the identity of the class to which the function belongs, using the scope operator::.
Things to consider when defining a class
1. Initialization of the defined data member is not allowed in the class body.
2. The type of a data member in a class can be arbitrary, including integer, float, character, array, pointer, and reference. It can also be an object. The object of another class can be a member of the class, but the object of its own class is not possible, and a pointer or reference to its own class is possible. When the object of a class is used as a member of this class, if the definition of another class is behind, it needs to be explained in advance. Job coordinates www.zhizuobiao.com
3, generally, in the class to first explain the public members, they are concerned about the user, after the description of private members, they are not interested in users. When you describe a data member, it is generally indicated by the type size of the data member, from small to large, which increases the space-time utilization.
4. It is customary to place the description part of the class definition or the entire definition part (including the implementation part) in a header file.
Definitions of classes and classes for C + +