When you see the title and are interested in opening this article, then I presume that you should be ready to test "level two C + + grade". If I'm right, I hope it will help you in the following exam, if I guess wrong, you might as well take a short time to see, this in the programming practice also need to pay attention to, you should review, deepen, find resonance.
(i) constructors--from the most easily encountered problems
(1) initialization list of constructors
1) Constructor: Point(float a,float b) {x=a;y=b;} ①
2) Overwrite with initialization list: Point(float a,float b): Y (b), X (a) {} ②
1.1 above are two ways to write the same constructor. where xandy are variables defined in advance,aandb are parameters of the constructor.
1.2 Promoted by ①② Point (float a,float b): Y (b) {x=a;} established ③
1.3 {} is the constructor body.
: The following y (b), X (a) is the so-called initialization list. Here is a test center, in the constructor body, the initialized member is not repeated initialization in the initialization list, conversely, in the constructor body is not initialized members, must be written in the initialization list, and pay special attention to the initialization of the list before the ":" The colon is delimited and marked.
Special Note: When x or y is defined as constant data, the constant data can only be initialized with the initialization list (② or ③ form)
Class Point
{
Private
float x, y
Public
Point (float a,float b): Y (b), X (a) {} ' or point (float a,float b): Y (b) {x=a;}
' cannot be wrongly written as Point (float a,float b) {x=a;y=b}
}
(2) How to construct the constructor function under the inheritance relation
Class Person
{
Private
Char Name
int Age
Public
Person (char A,int b): Name (a), age (b) {}
}
Class Student:public Person
{
FLOAT score// default to (private) Private member
Public
Student (char x,int y,float z):P erson (x, y), score (z) {}
}
The comparison is not difficult to find, here is characterized by:
(1) derived class (subclass)Student in the initialization list [Person (x, y), score (z)], according to the constructor of the base class, the corresponding parameters directly to the base class.
(2) Initializes the newly derived members of the derived class, of course, the initialization of the newly derived member can be re-constructed in the function body "{}", or the list (":") can be initialized again.
(ii) Inheritance--Easy to remember (difficult to clarify)
(1) Definition of inheritance
Definition: Subclasses inherit useful data members and member functions from the parent class.
(2) Classification of inheritance
Single inheritance and multiple inheritance
Single inheritance: There is only one direct parent class, class A:public b, read as " a public inheritance from Class A to b(a is a subclass, B as the parent class) ".
Multiple inheritance: There are two and more direct parents, example class a:public B, publicC, read "class A ," The Common inheritance and class B at the same time the public inherits from class C"(The second publicly can be omitted from writing).
(3) class member access rights
Class members have three access rights:Public (publicly),private(private),protected(protected)
Class Example
{
Private
? Members here can only be used for this class, cannot be accessed by a derived class, and objects of this class cannot be accessed directly
Protected
? Members here can be accessed directly by this class and derived classes, but objects of this class cannot be accessed directly
Public
? The Members here can be accessed directly by both the class and the derived class, or directly by the objects of this class.
};
The effect of table derivation mode on inherited member access rights
Inherited Way |
Base class Public |
Base class Private |
Base class Protected |
Description |
Public |
Public |
Private |
Protected |
Access rights are not changed |
Private |
Private |
Private |
Private |
All become private. |
Protected |
Protected |
Private |
Protected |
The public becomes protected and the rest is unchanged. |
Note: The access to members of a class is used, in order to avoid conflicting members, you can use the scope restriction "::".
Here's a word to remember: objects of any class can only access the public members of that class.
The dialectical understanding of this sentence is that the object of class instantiation (new) does not have direct access to privateand protected members (protected) , and the object's use of these members is done through the public member function in the class body. That is, if a non-public member inside a class cannot be used through an existing public member function, then it can only be used by modifying the contents of the class and re-establishing the invocation mechanism. A member consists of a data member (a variable or constant variable that we often define in a class) and a member function, and a public member of the parent class is also a public member of the subclass if the child class is a public-inherited relationship with the parent class. "
To sort out the "access" issue:
Member access 1) adopts: Object name . member name or reference name . member name or pointer- > The member name.
2) class Access member: called directly using the data member variable name, member function name.
(iii) Recommendation and sharing
A good book, can get the proper instruction, can simplify the simple things (relative to some books is, simple things complicate, make the reader confused, the final conclusion is this knowledge is really difficult), to lead the reader to understand the natural and smooth access to knowledge. Thirdly, we recommend a book, "Practical tutorial ofC + + Programming" Wang Dalen, Tsinghua University Press 2006.
C + + is An extension of C, which can be considered an enhanced version. This book is more about C + +, because, said C + + includes C , as for the different places, there are simple contrast hints, and different points are actually very few. This book whether for examination or in the presence of the two languages in the foundation of deepening, are very good, because, the knowledge of the point is structured, clear, examples are very capable, quite representative. The examples in this article come from this book.
C + + Important knowledge point finishing-thinking