Chapter I classes and objects
Classes and objects are the two most basic and important concepts of object-oriented programming. The object is the abstract description of the objective transaction in the computer, while the so-called class is a uniform description of a group of objects with similar properties and behaviors. From a programming language perspective, a class is a data type, and an object is a variable of this type.
1. Definition of class
The general form of a class definition is:
Class name
{
Private
data member or member function
Protected
data member or member function
Public
data member or member function
};
< implementation code for each member function >
Where class is the keyword that defines the classes. The class name is a valid identifier and is typically capitalized. The braces are part of the class Description section, which declares all members of the class, including data members and function members, that are divided into three classes from the access rights, private, public, and protected (protected), where the default permissions are private.
A member of the private part of a class that is not accessible outside of a class, only a member function in a class can access private data members and member functions. A member of the public part of a class that can be accessed by any function or statement in the program, and the public member is a member function that provides an interface to the outside world, through which the access to private members can be realized. The members of the protected section of the class are not accessible outside of the class, and only the member functions of the class and their subclasses (derived classes) can access the members of the protected.
2. Definition of member function
A member function in a class can be defined in two places: one is to write the definition of a member function directly in the class, it is generally suitable for the small size of the member function, and the second is to write only the prototype of the member function in the definition body of the class, while the definition of the member function is written outside the definition of the class, which is more suitable for the larger case of the member function body. The format is defined as:
Return value type class Name:: member function name (parameter description)
{
function body;
}
The "::" symbol here is called the scope operation (namespace delimiter), which is used to indicate which function belongs to which class or which data belongs to which class.
It should be explained that a member function can be either a parameter function or, like a normal function, specify a default value for the parameter.
3. Objects and pointers to objects
An object is an instance of a class, and you must describe the object's class before you define it. The general format for defining objects is:
Class name Object name table;
Where the object name table can have one or more object names, and multiple object names are separated by commas.
Alternatively, you can define a pointer to a class type, which is defined in the following format:
Class name * pointer variable name;