As the basis for object-oriented programming, it is important to master the basic structure and characteristics of Class.
1. How to implement a class
Class egclass {
PUBLIC:
// public provides a set of open operation functions and operators
// These disclosed operation functions and operators are called Member functions
// Declare or define the constructor and Destructor (destructor)
Private:
// Private implementation details
// Member Functions defined part
// Declare Data Members
// You can also initialize Data MEMBERS generally initialize the part of the Data MEMBER in the constructor.
}
All MEMBER FUNCTIONS must be declared in the Class, as for whether it is given to the definition, it can be determined by itself. If defined within the Class main body, this Member function will be treated as the inline function (Note: the inline function is quite with a macro). If you define a MEMBER FUNCTIONS in vitro to specify which class is specified using a special syntax. If you want this function as an inline function, you should specify the keyword inline at the forefront.
Inlne Bool Stack :: Empty ()
{
/ / Define MEMBER FUNCTIONS
}
The above example indicates that EMPTY () is a MEMBER FUNCTIONS for the Class Stack. Two colons behind the Class (Stack: :) are called the Class Scope Resolution Operator. Used to indicate the Member functions
Which Class is it.
2. Constructors and Destructors (destructor)
Constructors is used to initialize Data MEMBERS. The compiler will call CONSTRUCTORS to initialize Data Members each time the Class Object is defined.
The constructors' function name must be the same as the CLASS name. And the constructors should not specify the return value and do not need to return any values. It can overloaded. A Member Functions of the most Class It is that it can be defined in Class except.
Egclass :: egclass (); // This is the simplest constructor
Destructor, a MEMBER FUNCTIONS that is opposed to constructors. Its definition is determined by the user.
If you have a DESTRUCTORS in the Class. When Object ends life, the compiler will automatically call DESTRUCTORS. The DESRRUctor is primarily used to release resources configured in constructors or in object run cycles.
Destructors named is very strict, and its function name must be a Class name plus '~'. It does not bring any parameters, nor does it return any values. It is more impossible to overloaded. EG:
~ egclass ()
{
// Specific implementation
}
MEMBER INITIALIZATION LIST (member initial value form)
General constructors initialize Data Member has two methods, one is in the function body
EGCLASS :: Egclass (Const Egclass & RHS)
{
_DATA_MEMBER1 = rhs._data_member1;
_DATA_MEMBER2 = rhs._data_member2;
}
Another initialization syntax is the so-called Member Initialization List (member initial table): Egclass :: Egclass (const Egclass & rhs): _data_member1 (rhs._data_member1),
_DATA_MEMBER2 (rhs._data_member2)
{} // Note the function body is empty
3. Const and mutable (variable)
When a function calls a class, we can guarantee this Class not changed:
Int sum (const egclass & class1)
{
// Specific implementation
}
But if SUM calls a MEMBER FUNCTIONS of this class, the value of this Class may be modified by one of its Member functions. In order to make the value of this Class not be modified by its Member functions. We must labeze Const on the Member Functions to tell the compiler that this Member Functions will not change the Class Object.
Class egclass {
PUBLIC:
// These are const memory functions they will not change Class Object
INT length () const;
Int ended () const;
// These are Non-Const Member Functions
Int nex ();
Private:
// ...
}
After Const, after the parameter table of MEMBER FUNCTIONS, if Const Member Functions defined in the Class, you must specify const in the declaration and definitions.
Keyword mutable allows a Data Member with a nature: When its value is changed, it does not destroy the constantity of Class Object.
4. THIS pointer
Look at this example;
Stack & Stack ::
Transfer (Const Stack & RHS)
{
_ip = rhs_ip;
_size = rhs._size;
Return ??? // This is what we should return to each?
}
The THIS pointer is to solve this problem. The THIS pointer is used to address its caller in Member Functions. At this time, the return value can be written to Return * THIS;
The whole Member functions can be rewritten like this.
Stack & Stack ::
Transfer (Const Stack & RHS)
{
IF (this! = & r HS) {
_ip = rhs_ip;
_size = rhs._size;
}
Return * .this;
}