The learning experience of C + +

Source: Internet
Author: User
Tags variable scope

An important rule in C + + object-oriented programming is that public inheritance means "is one." Be sure to keep this rule firmly in mind.
The non-virtual member function of a class is actually saying that this function represents a special invariance, and subclasses cannot change this "invariance".
1. Common base classes imply common characteristics. If class D1 and class D2 both declare Class B as the base class, D1 and D2 inherit common data members and/or common member functions from B.
2. Public succession means "is one". If Class D is publicly inherited from class B, each object of type D is also an object of type B, but not in turn.
3. Private inheritance means "with ... To achieve ". If Class D private inherits from Class B, the object of type D is only implemented with the object of type B, and there is no conceptual relationship between the objects of type B and type D.
4. Layering means "having a" or "using ... To achieve ". If Class A contains a data member of type B, the object of type a either has a part of type B, or an object of type B is used in the implementation.

There are two polymorphic polymorphic forms of C + +:
1, compile time polymorphic, compile time polymorphism relies on function overloading or template implementation
2. Multi-state of running time. Realization of running time polymorphism relying on virtual function virtual interface

Guidelines for use of references:
1. Do not use pointers when you can use a reference
2. References can be used as "left value"
3. References are not allowed to be empty, and pointers must be used when there is an empty object. A reference to a null value is very harmful!
4. Try to use "pass-through" instead of "pass the value"
5. Do not attempt to return a reference when an object must be returned
6. Never return a reference to a local object, or a reference to a pointer inside a function that is initialized with new

Class has const data, it must be initialized by a constructor.
However, static constants are initialized as follows:
static const Double A; static double constant declaration!
Const Double class name:: a=1.04; Static initialization!
The static constant integer of a class can be initialized directly in the class: static const int hashnum = 7;

t& operator[] (int index); Returns an element of an array, readable, writable
Const t& operator[] (int index) const; Returns an element of an array, readable, non-writable

overloaded functions, which distinguish whether the standard is overloaded:
(1) The function can only be distinguished by the parameters of the overloaded function (type, number, default parameters)
(2) You cannot differentiate overloaded functions by the return value of a function

Some differences between C and C + +

The difference between struct and struct in C:
C + + structs can be used as classes, except that members of class are private by default and members of a struct are public by default.
A struct in C can only be a collection of variables that encapsulate data but cannot hide data, and members cannot be functions.
The struct in C is a user-defined data type (UDT), and the struct in C + + is an abstract data type (ADT) that supports the definition of member functions.

The C + + language guarantees that if P equals null, then delete p does nothing.
Delete P is a two-step process: Call the destructor and then free up memory.
Delete p calls operator delete (void*), and delete[] p calls operator delete[] (void*).

The static keyword has at least the following effects:
(1) The function body static variable scope is the function body, different from the auto variable, the memory of the variable is allocated only once, so its value will remain the last value at the next call.
(2) The static global variable within the module can be accessed by functions used within the module, but not by other functions outside the module.
(3) The static function within the module can only be called by other functions within the module, and the use of the function is limited to the module in which it is declared.
(4) Static member variables in a class are owned by the entire class and have only one copy of all objects of the class.
(5) The static member function in a class is owned by the entire class, which does not receive the this pointer, and therefore can only access static member variables of the class.

The CONST keyword has at least the following effects:
(1) To prevent a variable from being changed, you can use the Const keyword. When defining the const variable, it is often necessary to initialize it, since there is no chance to change it again.
(2) For pointers, you can specify that the pointer itself is const, or you can specify that the pointer refers to a const, or both, to a const.
(3) In a function declaration, a const can modify a formal parameter to indicate that it is an input parameter and that its value cannot be changed inside the function.
(4) For a member function of a class, specifying it as a const type indicates that it is a constant function and cannot modify the member variables of the class.
(5) For a member function of a class, sometimes it is necessary to specify that its return value is a const type so that its return value is not "left value".

The learning experience of C + +

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.