1. Classes and objects
Class is the description of the object, mainly from the attribute and behavior aspects.
The attribute is generally set to private, and the behavior is set to public.
Function (1) constructor, initializes all member variables, automatically called by the system, and can be reloaded
(2) destructor, which is automatically called at the end of the object lifecycle.
The constructor and destructor are automatically called by the system. You can call the constructor through an object.
A;
A. A (); // The error constructor cannot be manually called.
A .~ A (); // right when you manually call the destructor, it is called as a common member function.Code Will be executed, and the object will not be destroyed
(3) The get and set methods are used to access private member variables. The only channel for external access variables
(4) Class behaviors are part of our effort in coding.
2. Composition of Classes
(1) Data Section
(2) constructor and destructor
(3) Get & Set Method
(4) Business Methods
3. stack implementation (store int-type stacks)
To achieve this, an array is needed as a container to save the data inserted by the user, which is implemented using a pointer; int * P;
You also need a variable to save the int top position on the top of the stack; // the top position starts from 0.
There is no limit to the storage of data on the stack, so an int type variable is required to record the maximum length of the array int Max;
(1) constructor
When no parameter is provided, the stack length is the default value stack (). You can also specify the stack length (INT)
(2) destructor
Yes, because you need to apply for space in the heap.
(3) to protect stack data security, the get and set methods cannot be provided for P.
The value of top and Max depends on the inserted data. Only the get method is available and cannot be set. The set method is not required.
(4) int push (INT); insert. When the stack is full,-1 is returned, indicating insertion failed.
Int POP (); Delete the element at the top of the stack. If a data item is deleted correctly, the value of the data is returned. If the value is not deleted correctly,-1 is returned, indicating that the stack is empty.
Void disp (); real data
(5) Notes:
Add "Stack:" Before the function name ::"
(6) Insert data in the stack, that is, insert data at the top position.
Deleting data means moving the stack to the bottom
4. Inheritance
(1) Inheritance represents a "is a" relationship. Teacher extend person
Here, teacher is a subclass and person is a parent class.
Subclass inherits all attributes and actions of the parent class
(2) class teacher: public person {};
Indicates that the teacher Class inherits the person class.
In extends_sample2, subclasses can directly use the teach function of the parent class, but cannot use the name attribute of the parent class private. The name attribute actually exists in the subclass, but cannot be directly used by the quilt class because it is a private attribute. Private attributes can only be accessed by functions of this object. Public attributes can be directly used by quilt classes, but external functions can also access attributes with public permissions. What should I do if I want to use quilt classes and do not want external functions to access them? Use protected to modify the attribute.
The following is a class attribute with different access control modifiers in different object visibility I (visibility is accessible ):
Other functions of this object subclass object
Private property visible invisible
The protected attribute is visible and invisible.
Public property visible
There are also three different access control modifiers before the inheritance keyword extends. After being inherited by different inheritance methods, the access control permission will change. The inheritance method can be understood as Chengmen. No matter how fat people are outside, they can only lose weight to the corresponding width through the different widths of the city gate.
Public extends protected extends private extends
the private attribute of the parent class cannot be accessed, but cannot be accessed.
the protected attribute of the parent class changes to protected and does not change to private, sub-classes can be accessed. sub-classes cannot be accessed.
the public attribute of the parent class is changed to protected and private. sub-classes can be accessed. sub-classes cannot be accessed.
Constructors cannot be inherited. Because the constructor is only suitable for the generation of this class.
For example, extends_sample4, you must first create a parent class when creating a subclass. How to Understand? Consider the composition of sub-classes, just as 46 used by gangsters in CS is composed of AK47 added with sight.
When creating a subclass, the constructor of the parent class is called first, because the constructor of the parent class is not specified during inheritance. There are many types of constructor. Because no constructor is specified, no constructor is used by default. If the parent class has no constructor without parameters, a compilation error occurs.
This is the problem. How can this problem be solved? You can add a non-argument constructor to the parent class. If we are not the designer of the parent class, we should specify the constructor that uses the parent class during subclass inheritance. For example, when writing a subclass constructor, teacher (char * Name, int age, double salary): Person (name, age ){......}, you can specify the constructors with parameters of the parent class.
It is a child class after the parent class. Where are the Destructor? The Destructor cannot be inherited. When a subclass is released, it first calls its own destructor, and then calls the destructor of the parent class. This is opposite to the calling sequence of the constructor.
5. You can modify the behavior of the parent class in the subclass, that is, overwrite the method.
(1) The function name in the subclass must be the same as that in the parent class.
(2) hidden, unable to produce Polymorphism
class base { Public : void FN ( int A) {cout " base A = " Endl ;};
ClassSub:PublicBase {Public:VoidFN () {cout<"Sub B = 20"<Endl ;}};
(3) Call the method of the parent class
A. Base: FN (10);//You can call functions of the parent class in a targeted manner.
6. High Cohesion and low coupling of functions
High Cohesion: The function functions should be as simple as possible, so the Code has a high maintainability.
Low coupling: Avoid chained reflection when modifying functions.
7. Polymorphism
(1) What is polymorphism?
A pointer of the int type can only point to a variable of the int type.
For objects, the person type pointer can point to objects of the person type.
The person type pointer can point to a teacher (extend person) type object.
(2) polymorphism features:
The pointer of the parent class can point to the object of the subclass.
The parent class pointer can only call the methods declared in the parent class.
When a function is called through a pointer, if the function is a virtual function, the behavior displayed is the behavior of the object itself.
Person * P = new teacher;
Compile-time type runtime type
(3) The subclass pointer cannot point to the parent class object.
This is because the subclass has a function that is unique to the subclass. during runtime, you cannot use a pointer to call the function that is unique to the subclass.
(4) If this keyword is added before the return value of the "virtual" parent function, it is a virtual function.
(5) Prerequisites for producing polymorphism:
Inheritance, method override, and virtual functions
8. The implementation principle of virtual functions each object has a pointer to the virtual function list, which is a stack. When constructing an object, according to the principle of first constructing the parent class and then constructing the subclass, the function of the parent class is first written into the stack, and the function covered in the subclass is placed above. When the object is constructed, the subclass function is at the top. According to the principle of post-stack first-in-first-out, the subclass function is called first.
9. When resources are released, delete p; only the destructor of the person class will be called. Because the pointer type is person, the subclass space will not be released in time, this will cause memory leakage and write the Destructor as a virtual function. In this way, the sub-class destructor will be called first, and then the parent class's destructor will be in the inheritance relationship, the destructor of the parent class must be a virtual function !!!
10. Use of Polymorphism
# Include <iostream> Using Namespace STD; Class Person { Public : Virtual Double Buy (){ Return 2 ;}}; Class TEACHER: Public Person { Public : Virtual Double Buy (){ Return 1 ;}}; Class Student:Public Person { Public : Virtual Double Buy (){ Return 0.5 ;}}; Class CEO: Public Person { Public : Virtual Double Buy (){ Return 1000 ;}}; Void Shoufei (person * P) {cout <P-> buy () < Endl ;} Int Main () {person P; teacher t; student s; ceo c; shoufei ( & P ); // Call the corresponding function by passing in the address of different objects Shoufei (& T ); // Comparison with if... else Shoufei (& S ); // WriteProgramWe should try to minimize the changes to the written source code, so as to implement the Code's common and low coupling. Shoufei (& C ); Return 0 ;}
Homework: Write a school bus class and assemble the passenger bus with a number
Bus: Get on, get off, sell tickets, etc.
Teacher and Student are available on the vehicle to calculate the total fare.