1. Other functions of this object subclass object
Private property visible invisible
The protected attribute is visible and invisible.
Public property visible
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 is changed to "protected" without changing to "private". The subclass can be accessed and the subclass cannot be accessed.
The public attribute of the parent class is changed to protected and private, and the subclass can be accessed. The subclass cannot be accessed.
2. There are many constructor types. Because no constructor is specified, no constructor is used by default. If the parent class has no constructor without parameters, a compilation error occurs.
Teacher (char * Name, int age, double salary): Person (name, age ){......}, specify the constructors with parameters of the parent class.
3. 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.
4. Polymorphism: (1) pointer
(2) Reference
A parent class can reference a subclass object.
You can only call the parent class function through the parent class reference.
Calls a virtual function that overwrites a parent class and can call sub-class functions.
5. A subclass inherits a parent class-single inheritance
One subclass inherits multiple parent classes-multiple inheritance
ClassSpiderman:PublicSpider,PublicPerson {....}
6. Diamond inheritance solves conflicts between repeated Elements
Let two parent classes inherit a superclass at the same time, and put repeated elements in multiple inheritance in the superparent class.
When multiple sub-classes inherit one parent class at the same time, only one sub-class truly constructs the parent class.
ClassSpider: vertualPublicAnimal {.....};ClassPerson: vertualPublicAnimal {.....};ClassSpiderman:PublicPerson,PublicSpider {....};
Do not use more than three layers for inheritance
7. abstract class
Only function declaration, no function implementation
Pure virtual function: the unimplemented function virtual void writelog (char *) = 0;
If "= 0" is not written, the system considers it a function declaration and tries to find the function implementation in another ". cc" file.
Classes containing pure virtual functions are called abstract classes. They are abstract data types and cannot be used to create objects.
Abstract types are inherited by others. Subclass covers pure virtual functions and provides function implementation.
Use the parent class to standardize subclass usage
If the subclass does not fully implement all pure virtual functions of the abstract parent class, it is considered that the subclass is still an abstract data type.
Where virtual is used:
(1) Inheritance
(2) Multi-Inheritance
(3) pure virtual functions
The specification of abstract classes is very important. In engineering, it is very important for parallel development of projects.
The changes in the project can be easily handled
Point the pointer to the corresponding subclass object to conveniently call the subclass function.
8. Friends
For a class, its own private variables cannot be accessed by other classes. However, if you authorize a class as your friends, you can access its private attributes.
Something that can be used as a friend: another class, a global function.
Achieve membership authorization:
ClassGirl;ClassPerson {... friendClassGirl;//The Declaration of the Friends class --> authorize the girl class to become friends of the class, and you can access your own private variables.}
ClassGirl;ClassPerson {... friendVoidFN ();//Member function declaration --> grant permissions to the FN function to become a member of your own, and you can access your own private variables.}
Friends are not part of the class
If they are not mutual friends, they cannot access the private variables of the member class.
Use of friends:
Bus uses the conductor as its friends to access its own private variables, that is, to load the passenger's Array
Use of friends in projects
9. Static Data
Define a static data in the class (actually a global variable)
(1) does not depend on the object. It already exists before the object does not exist.
(2) share all objects
Differences from global variables:
(1) The static variables in the used class must be used by the class name
(2) restricted by access control symbols
(3) static variables cannot be assigned values when declared in the class. They must be initialized outside the class.
class A { Public : static int A ;}; int A: A = 100 ; /// only space is allocated int main () {cout
//
the usage of static variables is independent of objects, use directly }
Differences from member variables
(1) The member variables depend on the object. The class object does not exist, and the member variables do not exist.
Static variables do not depend on objects. As long as there is a class declaration, they exist.
(2) All objects share a static variable.
10. Static Functions
Add static before the Function
Does not depend on objects. No objects can still call static functions by class names.
A: FN ();
Static functions and static variables exist during class declaration.
Static functions can only use static variables, but cannot use member variables.
11. copy constructors
# Include <iostream> Using Namespace STD; Class Person { Public : Person () {cout <" Person () " < Endl ;} ~ Person () {cout < " ~ Person () " < Endl ;} Void Speak () {cout < " Hello " < Endl ;}}; Void FN (person p ){// The passing value here is passed, the form parameter is passed, and the creation of the form parameter here is the use of the copy constructor P. Speak ();} Int Main () {person P; FN (P ); Return 0 ;}
Output result:
Person () hello~Person ()~ Person ()//2 destructor, because the copy constructor provided by the system is called to construct a new object
Copy the constructor to create a new object with an existing object as the template.
Declaration method: Person (const person & P) {...} // saves space and ensures that the template will not be modified
The default copy constructor executes a simple copy of memory --- a shallow copy.
(1) shallow copy
Only copy the address, instead of copying the pointer to the space, will cause two pointers to the same space
(2) Deep copy
Create a new space for the pointer and copy the content of the pointer pointing to the space
When you call the copy constructor:
(1) When the value is passed
(2) A; // default constructor
A a1 = A; // when declaring, assign a value to an object and use the copy constructor.
(3) A A1;
A A2 (A1); // The displayed call copy constructor. The passed parameter is an object.
When do I need to write a copy constructor?
When dynamic memory is used, it will face the phenomenon of shallow copy. You need to write the copy constructor yourself.
12. Operator Overloading
A1 = a2;
The system calls the function operator =
This operation is equivalent to a1.operator = (A2 );
Student & operator = (const student & );
The function returns the left value. The return value is a reference.
The function returns the right value and the return value is itself.
View code
/* * student A1; * student A2; * a1 = a2; <=> a1.operator = (A2); */ Student & operator = ( const Student & A) {age = . age; id = . ID; strcpy (name,. name); // you do not need to create a new space, because when declaring A1 and A2, Both pointers point to one space. copy the variable pointed to by the pointer, that is, the value is assigned return * This ;}
when applying for space in the heap, it overwrites the value assignment operator ("=")
job: account class of the banking system
name, use Pointer to Save Password
(1) destructor
(2) copy constructor
(3) overload assignment operator