C ++ object-oriented programming notes

Source: Internet
Author: User


1. The definition of the inline function must be in the same file as the declaration, whether in-class or out-of-class definition.

2. For member functions with default parameters, if the default parameter value is specified during function declaration, do not specify it when defining the function. Otherwise, the compilation error is returned: redefinition of the default parameter. 3. const: 1) initialization of regular data members of a class can only be completed through the parameter initialization table of the const function, and cannot be assigned a value in the constructor body; 2) all data members of the const object are const, but the member functions are not necessarily; the const object can only call the const member function, but cannot call non-const member functions; 3) const member functions cannot call non-const member functions. 4) define a constant pointer to an object in the form of [class name * const pointer variable name = object address, the pointer variable cannot point to another object, but can change the value of the data member pointing to the object. 5) if you define a pointer to a common object in the form of [const class name * pointer variable name = object address], the pointer variable cannot change the value of the data member pointing to the object, however, this pointer variable can point to another object. 4. When an object is assigned with a value of =, the data members of the class cannot have dynamically allocated data. Otherwise, serious consequences may occur during the assignment. 5. static data members are used for sharing between different objects of the class. Their initialization can only be performed outside the class. They cannot be initialized using the parameter initialization table. If they are not initialized, the default value is 0. 6. The static member function does not have the this pointer and cannot directly access non-static data members through the variable name. to reference a non-static member function, use the [Object Name. variable name. 7. If you want to define an operator overload function as a class member function, the first parameter of this operator must be an object of this class. We usually reload the single object operator as a member function of the class, and the binary operator as a friend function of the class. 8. Call Sequence of constructor: base class constructor> sub-object constructor> This class constructor. The Calling sequence of the Destructor is the opposite. 9. virtual base class: to ensure that only one indirect base class member is retained during multi-inheritance. For example, B and C all inherit from A, and D multiple inherit from B and C. If A is A virtual base class during inheritance, only one member in A is retained in D. The Declaration of the virtual base class is in the header of the derived class, in the form of class derived class name: virtual inheritance method base class name. In the preceding example, A must be declared as A virtual base class when declaring class B and class C. Otherwise, there will be multiple members of Class A in Class D. Indirect Derived classes are also responsible for the initialization of virtual base classes. In the preceding example, the constructor of C also calls the constructor of. 10. polymorphism can be divided into static polymorphism and dynamic polymorphism. Static polymorphism is implemented through function overloading. During program compilation, the system can determine which function is called. Therefore, it is also called the polymorphism during compilation. That is, static Association, early Association. Dynamic polymorphism is implemented through virtual functions, so that the function to be called can be dynamically identified during the program running. Therefore, it is also called Runtime polymorphism. That is, dynamic Association, lagging Association. If a class has a virtual function, the compilation system constructs a virtual function table for the class, which is a pointer array that stores the entry address of each virtual function. 11. The role of a virtual destructor is to ensure that when you delete a base class pointer to a derived class, the destructor of the derived class can also be called. When the destructor of the base class is a virtual function, no matter which class object the Pointer Points to in this class family, the system uses dynamic Association to call the destructor of this object. When the destructor of the base class is a virtual function, the destructor of all derived classes automatically become virtual functions. It is best to declare the destructor of the base class as a virtual function. The constructor cannot be a virtual function. The class object cannot be associated because it is not fully established when the constructor is executed. 12. The stream objects of C ++ include standard input and output streams, file streams, and string streams. The corresponding stream classes include istream, ostream, iostream, ifstream, ofstream, fstream, istrstream, ostrstream, and strstream. The corresponding header files are iostream, fstream, and strstream. No header files, such as ifstream, ofstream, istrstream, and ostrstream. 13. cout and cin are neither C ++ statements nor keywords. They are the objects of the ostream class and istream class respectively and are defined in the header file iostream. Scanf and printf are neither C-language statements nor keywords. They are C-defined functions. 14. standard output streams include cout, cerr, and clog. By default, cout is output to the screen, and can also be redirected to disk files. Cerr is a standard error stream and can only be output to the display. Clog is also a standard error stream, which has only one small difference with cerr: cerr directly outputs information to the display without passing through the buffer zone, and the information in clog is placed in the buffer zone, output to the Monitor (similar to cout) when the buffer is full or when an endl is encountered ). 15. You can use the <and> stream operators or put, get, getline, and other member functions to perform operations on ASCII files. The Open Method for ASCII files is either ios: in or ios: out, not ios: in | ios: out. (Different from binary files) binary file operations are performed using the member functions read and write. You can also use functions such as gcout, tellg, seekg, tellp, and seekp to obtain or change the file pointer position. The binary file can be opened in ios: in | ios: out | ios: binary. in this case, the stream object should be defined by the fstream class, And ifstream or ofstream cannot be used. 16. ASCII files cannot be opened in ios: in | ios: out, and cannot be read/written simultaneously. Open the ASCII file in this way. If you only read or write one operation, no problem occurs. If both the read and write operations are performed, a problem occurs. 17. The data member of a common object is a constant variable, but the member function of a common object is a constant function only when the const is explicitly modified. A common object can only call a common member function, but cannot call a non-member function. Data members of common objects cannot be changed. If you need to change a data member of a constant object, you can declare the data member as mutable. In this way, you can use the const member function of the constant object to modify its value. 18. A common member function can reference both a common data member and a special data member. However, a common member function cannot change the value of a data member. A common member function cannot call a non-member function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.