Define a class
struct sales_data{
std::string ISBN () const;
sales_data& Combine (const sales_data&);
Std::string Bookno;
unsigned units_sold=0;
Double revenue=0.0;
}
1. member functions
The declaration of a member function must be within the class, and its definition can be defined both inside the class and externally, in which the inner function is implicitly the inline function, and the external definition requires the use of the scope operator::.
1.1. Const member function
String fun () const{};
The function of the const keyword after the argument list is to modify the type of the implicit this pointer, in the above example, the type of this is sales_data * const, meaning that we cannot bind this to a constant object, so We cannot call the member function ISBN () on a constant object. If you use the const member function, this becomes a const sales_data * Const Type, this can point to a constant object, and the Isbin function body cannot change the object that this refers to. The Combine function does not require a const, because a const object cannot use this function, and the object of the function changes its contents.
Defining the CONGST member function outside of the class must also explicitly specify the Const property after the argument list, specifying the class name to which it belongs:
std::string Sales_data::isbin () const{
return bookno;
}
1.2. Constructor function
The task of the constructor is to initialize the data members of the class object and execute the constructor whenever the object of the class is created.
- The constructor name is the same as the class name
- No return type
- can be overloaded
- cannot be declared as const
(1) The default constructor of the composition
If we do not explicitly define constructors for the class, the compiler implicitly defines a default constructor (also called the composition's default constructor), which initializes the data members of the class according to the following rules:
1. If there is an initial value in the class, use it to initialize the member (for example, units_sold=0,revenue=0.0 above).
2. Otherwise, the default initialization (such as Bookno is initialized to an empty string).
2. This pointer
The member function invokes the object with an implicit parameter called this, and when we call a member function, initializes the this pointer with the address of the object that requested the function. Therefore, any custom parameter or variable named this is illegal, and we can also use this within the function.
String Fun () {return this->no;}
This is a constant pointer and does not allow changing the address saved in this.
2.1. Return this object
Use *this in the function to dereference the object that executes the function:
return *this;
3. Scope of the class
The class itself is a scope, and the definition of the member function of the class is nested within the scope of the class, so that member variables of the class can be used even if the member function is defined externally.
The compiler processes the class in two steps: the declaration of the member function is compiled first and then the member function body. Once, the member function body is free to use other members of the Le species without caring about the order in which the members appear.
C + + Class