I. Procedural and Object-Oriented Programming
1) To adopt procedural programming methods, first consider the steps to be followed, and then consider how to express the data.
2) using object-oriented programming, we first consider objects from the user's perspective, the data required to describe objects, and the operations required to describe the interaction between users and data. After describing the interface, you need to determine how to implement the interface and data storage, and finally use the experience design solution to create a program.
Ii. abstraction and class
1) Class: converts abstraction into user-defined C ++ tools, and combines data representation and data manipulation methods into a neat package.
2) classes include: class declaration: describes the data part in the form of data members, and describes the public interface in the form of member functions.
Class method definition: describes how to implement class member functions
3) Data Hiding: place data in the private part of the class.
Encapsulation: hides the attributes and implementation details of an object and only develops public interfaces.
4) member functions: used for definition: To identify the class to which the function belongs. Private components of the member functions can be used.
Void stock: Buy (double price) // The buy function has a class scope. Other member functions can be accessed:
5) Public: the defined function or member variable can be accessed by any method.
Protect: can only be accessed by methods in this class or methods in the derived class
PRIVATE: can only be accessed by methods in this class
6) Example:
# Include <iostream> # include <cstring> class student {PRIVATE: char * Name; int num; double average_score; int count; public: Student (char * m_name, int m_num, double m_average_score, int m_count); student ();~ Student (); void Update (char * m_name, int m_num, double m_average_score, int m_count); void show () ;}; Student: Student (char * m_name, int m_num, double m_average_score, int m_count) {name = m_name; num = m_num; average_score = m_average_score; Count = m_count;} Student ::~ Student () {} Student: Student () {} void Student: Update (char * m_name, int m_num, double m_average_score, int m_count) {name = m_name; num = m_num; average_score = m_average_score; Count = m_count;} void Student: Show () {Using STD: cout; Using STD: Endl; cout <"Name: "<name <Endl; cout <" Num: "<num <Endl; cout <" average_score: "<average_score <Endl; cout <"count:" <count <Endl;} int main () {Using STD: cout; char * m_name = "tianshuai "; student * s = new student (m_name, 01, 98.8, 5); // the pointer S-> show (); s-> Update ("Xiaoming", 02, 99.6, 10); s-> show (); Return 0 ;}
7) usage notes:
1. constructor and destructor do not have return types
2. Generally, a program cannot directly access data members. It can only access data members through member functions.
3. When constructing a function, do not use the class member name as the constructor parameter (m_value)
4. You cannot use an object to call the constructor because the object does not exist before the constructor constructs the object.
5. Generally, you should not explicitly call the destructor in the Code. After an object is deleted, the Destructor is called.
3. This pointer
This pointer is used to point to the object that calls this method. The address of the class instance to which the called function is located.
Each member function has a this pointer pointing to the called object.
* This indicates the object itself.
4. Object Array
Default constructor is the constructor called when Initialization is not explicitly provided. It is defined by a constructor without parameters or a constructor that provides default real parameters for all parameters. If Initialization is not provided when a class variable is defined, the default constructor is used.
When a program creates a class object that is not explicitly initialized, it always calls the default constructor.
Interface: function declaration
Class scope: member variables cannot be initialized in the Declaration.