In object-oriented program design, there are often contact classes, objects and other professional nouns; what is the class, what is the object? What is the application of the program? Class is the core of object-oriented programming, which is actually a new type of data and a tool for implementing abstract types, because classes are data types that are implemented by means of abstract data types. A class is an abstraction to a class of objects, and an object is an instance of a class, so classes and objects are closely related. There is no class that is detached from the object, and there are 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 operations associated with that data. It's kind of like the structure in C language, the only difference is that the structure does not define "data-related operations", "data-related operations" is what we often see as "methods," so the class has a higher abstraction, the data in the class is hidden, and the class also has encapsulation.
The structure of a class, or the composition of a class, is used to determine the behavior of a class of objects that are determined by the internal data structures of the classes and the associated operations. These behaviors are described by an operating interface (also known as a member function of the class we see), and the user is concerned only with the function of the interface (that is, we only care about the function of the individual member functions of the Class) and is not interested in how it is implemented. The operating interface is also called the service provided by such objects to other objects.
The definition format of a class
The definition format of a class is generally divided into the description part and the implementation part. The description section is a description of the members in the class that contains the description and member functions of the data member. member functions are used to manipulate data members, also known as "methods." The implementation section is used to define member functions. In a nutshell, the description will tell the user "What to do" and the implementation part tells the user "How to do it."
The general definition format for the class is as follows:
Class <class name>
{
Public: <description of member function or data member>
Private: <Description of data member or member function>
}; <implementation of each member function>
Here's a simple description of the format above: class is the keyword that defines the class,< class name > is an identifier, usually a string starting with the T letter 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 the access permissions, the members of the class are divided into: public, private (private), and protected (protected) three classes. Public members are illustrated with public, which is often an operation (that is, a member function), which is a function of the interface provided to the user. This part of the member can be referenced in the program. Private members use private to illustrate that private parts are usually data members that describe the properties of objects in the class that are inaccessible to users, and that only member functions or specially described functions can refer to them, which are the parts that are used to hide them. The Protection class (protected) will be introduced later.
Keywords Public,private and protected are called access rights modifiers or access control modifiers. They are independent of the order in which they appear in the class body (that is, within a pair of curly braces), and allow multiple occurrences, using them to specify the access rights of class members.
The implementation of each member function in,< > is the implementation part of the class definition, which contains all the definitions of the functions described in the class body. If the class body of a member function is defined, the implementation part will not appear. If all member functions are defined in the class body, 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;
}; //The 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::Print();
{
cout< }
The scope operator that appears here:: is used to identify which class A member function belongs to.
The definition of the 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;
}
This implementation of the member function (that is, the definition of the function) is written in the class body, so 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, use the scope operator::.
Things to consider when defining a class
1. Initialization of defined data members is not allowed in the class body.
2. The types of data members in a class can be arbitrary, including integers, floating-point types, character types, arrays, pointers, and references. 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, and the pointer or reference to its own class is OK. When an object of a class is used as a member of this class, if the definition of another class is later, it needs to be explained in advance.
3, generally, in the class body to explain the public members, they are concerned about the user, after the description of private members, they are not interested in users. When describing data members, generally by the type size of the data member, from small to large description, this can improve the space-time utilization.
4. Often habitually place the description part of the class definition or the entire definition part (including the implementation part) in a header file.