1. Type Encapsulation
Class --> Object
Description: (1) attributes
(2) behavior-attributes and behaviors belong to the class
Create an object.
2. constructor-Initialize an object
(1) The constructor name must be the same as the class name.
(2) The constructor cannot write the return type.
The system automatically calls the constructor when creating an object.
Constructors can be overloaded to meet user requirements.
If no constructor exists in the class, the system provides the default constructor without parameters.
However, if a constructor is defined in the class, the system will not provide the default one. Therefore, we recommend that you write a constructor without parameters when writing the constructor.
3. Definition of class security
Variables> private variables are protected to prevent external modifications. They can only be used within the class (only used by their own member functions)
Function> Public
To ensure that private variables can also be accessed, the set and get methods are provided inside the class.
The set method does not return values, but requires parameters.
The get method must have a return value without parameters.
4. Define a class
(1) write an attribute
member variable function
(2) member variable --> private
member function --> Public
(3) special function
constructor. We recommend that you write two. A
Get, set function with parameters and a member variable with no parameters corresponds to a pair of get and set functions, it is the only way for external access to this variable
for the setxxx function, there is no return value, there are parameters, the parameter type is consistent with the property type assigned.
for the getxxx function, there is no parameter and a return value. The type of the returned value is the same as that of the Output property.
class behavior
5. encapsulation
the process of defining a class is encapsulation
exercise: encapsulate an account type
attribute: ID, password, name, balance
action: Save, withdraw, query
6. Split the account class into multiple file structures.
(1) convenient and quick browsing of the entire class definition
(2) easy to use, including header files
long account: GETID () {...........}
when implementing a function, add the class name before the function to determine the ownership of the function. This prevents compilation errors because the two classes have functions of the same name.
":" is called a domain access controller.
7. person per; creates an object and the system calls the constructor.
person * P = NULL; the system does not call the constructor to declare a class pointer.
person * P = NULL;
P = new person; Apply for a class space in the heap
Delete P;
the data in the heap has no name and can only be indicated by the pointer to the class object, member variables of the category class:
(* P ). sayhello ();
P-> sayhello ();
person PS [5]; declares an array of the person type, 5 constructor calls
when the array is declared, the system allocates space for it
and there is no chance to specify the constructor when declaring the array, only constructors without parameters are called.
when a class definition has no constructor, but an array of classes must be declared, you can declare a pointer array
person * PS [5]; --- when the pointer is declared, the class object is not created. Each element in the array is a person pointer, reaching:
(1) No constructor
(2) Implement object usage
For(IntI =0; I <5; I ++) {PS [I]=NewPerson (I,20+ I );//Initialize each pointer in a loop}For(IntI =0; I <5; I ++) {Delete PS [I];//Release the space pointed to by the pointerPS [I] =NULL ;}
8. classroom exercises:
Requirement: The main () function cannot be written.Code , In the running Program Print "Hello World"
Answer: The initialization of global variables has been initialized before the main function is executed. Therefore, you can write a global variable class, and its non-argument constructor writes the output statement.
Then declare the global object of a class.
9. constructor:
Each object has a pointer named this pointing to itself and using its own member variable this-> name
10. destructor
When the lifecycle of an object ends, the system automatically calls the object to reclaim space.
All resources requested (New char [20]), released in the destructor
If the object is a variable in the main function, the Destructor is called only after the main function is completed.
If the object is a local variable in a function, the Destructor is called when the function returns.
Destructor call: After the object lifecycle ends, the system automatically calls the destructor.
You can write the resource release code in the destructor.
Statement of the Destructor: Add "~" before the constructor.
Cannot be overloaded or have Parameters
If no destructor is written, the system provides the default destructor.
When the class uses the system space: new memory, open the file
Write destructor to release resources
11. Job: (1) Implement the stack structure using the object-oriented idea and write the main function for testing.
(2) Change the original banking system to an object-oriented form.