C ++ from scratch (1)-Basic Knowledge 1

Source: Internet
Author: User

I recently found something interesting. I 'd like to answer this question-http://wenda60.com /.

At this level, I dare to rush to C ++. Below are some records.

Default this pointer

This pointer is a special pointer. When a non-static member function of the class is executed, this pointer exists. It points to an object of the class, and a member function of this object is being called.
The name of this pointer is always this and is always passed to every declared member function as an implicit parameter.

In actual programming, the function declaration does not need to include this parameter.

When a program calls a member function of an object, the compiler adds the object address to the parameter list.

The static member function does not have the this pointer.

When calling a member function of an object, the compiler passes the object address to the this pointer and then calls the function. Therefore, you use the this pointer implicitly to call a member function.

Class inheritance rules

There are three types of C ++ class inheritance for a derived class: public, private, and protected)

Public)

The public and protected members of the base class can be the public members and protected Members of Their Derived classes.

The member functions of the derived class can access the public and protected members in the base class, and cannot access the Private Members in the base class.

The object of the derived class can access the public members of the base class.

Private)

The public and protected members of the base class are both private Members of Their Derived classes.

In private inheritance, the members of the base class can only be accessed by the direct derived class, but cannot be inherited further.

Protects C ++ class inheritance (protected)

All public and protected members of the base class are protected members of the derived class.

The public and protected members of the base class can only be accessed by their directly derived class member functions or friends.

Constructor and destructor cannot be inherited

Therefore, when constructing a derived class object, you must initialize the data members of the base class. New data members and data members of the member object

The parameter table of the derived class constructor must contain both the initial values of some data members of the subclass and the initial values of the data members of the base class.

If the base class does not have a default constructor, the derived class must have a constructor that provides parameters for the base class constructor.

Array pointer and pointer Array

-------------- Pointer ----------------
Int A = 10;
Int * P = &;

------------- Pointer -----------
Int B = 20;
Int * P = & B;
Int ** P2P = & P;

------------- Simple array -----------------
Int C [10]; // an integer array containing 10 integer Elements
That is to say, each element is an integer.

-------------- Pointer array --------------------
Int * P [10]; // pointer array, containing 10 pointer Elements
That is to say, every element is a pointer.

-------------- Array pointer --------------------
INT (* P) [10]; // array pointer, which can be used to point
An integer array containing 10 elements

Virtual base class

The role of the virtual base class is described above: If a derived class has multiple direct base classes, and these direct base classes have a common base class, in the final derived class, multiple members with the same name of the indirect common base class data member are retained.

When referencing these members with the same name, you must add a direct base class name after the Object Name of the derived class to avoid ambiguity, so that it uniquely identifies a member, such
C1.a: Display ().
In a class, multiple members with the same name are retained to indirectly share the base class. This phenomenon is not expected. C ++ provides the virtual base class (virtual base class) method, so that only one member is retained when the indirect common base class is inherited.

The virtual base class is not declared when the base class is declared, but declared when the derived class is declared and the Inheritance Method is specified. A base class can be used as a virtual base class when a derived class is generated, but not a virtual base class when another derived class is generated.

The general form of declaring a virtual base class is
Class derived class name: base class name in virtual inheritance mode
After such declaration, when the base class is inherited by a derived class through multiple derived paths, the derived class only inherits the base class once.

Note: To ensure that the virtual base class inherits only once in the derived class, it should be declared as a virtual base class in all direct Derived classes of the base class. Otherwise, the base class will be inherited multiple times.

If Class A is declared as a virtual base class in the derived class B and C, and Class A is not declared as a virtual base class in the derived class D, in the derived class E, although only one basic class member is retained for the part derived from Class B and C paths, a base class member is retained for the part derived from Class D paths.

When using multi-inheritance, you must be very careful and often encounter ambiguity issues. Many professionals believe that multi-inheritance should not be promoted in programs, but should be used only when it is relatively simple and not prone to ambiguity or when necessary, do not use multiple inheritance for issues solved with a single inheritance. For this reason, some object-oriented programming languages (such as Java and smalltalk) do not support multiple inheritance.

Pure virtual functions

Defining a function as a virtual function does not mean that the function is not implemented. It is defined as a virtual function to allow the base class pointer to call this function of the subclass.
Defining a function as a pure virtual function means that the function is not implemented. It is defined to implement an interface and act as a specification, which inherits this specification. Class programmers must implement this function.

Classes that contain pure virtual functions are called abstract classes and cannot be initially called objects.

Break, continue, and return

Break

Location: it can only be used in switch and loop statements.

Purpose: jump out of the switch statement or terminate the loop in advance, and switch to the statement after the switch statement or loop statement is executed.

Application: it is mostly used to end a loop (including an omitted for loop) in advance to avoid an endless loop.

Continue

Position: it can only be used in loop statements.

Purpose: Terminate this loop, that is, skip the loop body statement that has not been executed and start the next loop.

Difference: the difference with the break statement is that the continue statement ends the current loop, while the break statement ends the entire loop.

Return

Location: Inside the Function

The Return Statement has two forms:

Return;

Return expression;

A return statement without a return value can only be used for a function with a return type of void. A return statement is used to force the function to end. This usage is similar to a break statement in a loop structure.

A function with the return type void usually does not use the second form of return statement, that is, it can return the call of another function with the same return type as void.

Any function whose return type is not void must return a value, and the return type must be the same as the return type of the function, or be implicitly converted to the return type of the function.

Although C ++ cannot ensure the correctness of the result, it can ensure that the function returns an appropriate type of result every return.

Strlen and sizeof

Char ss [100] = "0123456789 ";
The result of sizeof (SS) is 100 ===" SS indicates the size in the memory is 100 × 1
The strlen (SS) result is 10 ===" strlen is a function internally implemented by a loop before \ 0.

Char Q [] = "ABC ";
Char P [] = "A \ n ";
Sizeof (Q), sizeof (P), strlen (Q), strlen (P );
The result is 4 3 3 2.

Class templates and function templates

A template is a tool for code reuse. It can implement type parameterization, define a type as a parameter, and truly reuse the code.

There are two types of templates: function templates and class templates. You can use them to construct template functions or template classes. After the template is instantiated, the template function or template class is obtained. After the template function or template class is instantiated, the object is obtained.

Function templates can be used to create a common function to support a variety of different form parameters, avoiding repeated design of function bodies for heavy-duty functions. The data type used by the function is used as a parameter.

The data type parameter identifier of the function template is actually a type parameter. When using the function template, You need to instantiate this parameter as a determined data type. Parameters instantiated by type parameters are called template parameters, and functions instantiated by template parameters are called template functions. The template function is generated to instantiate the type parameters of the function template.

A class template, also known as a class or a class generation class, is a mode defined for the class, it allows the parameters or return values of some data members and member functions in the class to take any data type. A class template is not a specific class. It represents a family of classes and is the unified mode of these classes. To use a class template, You need to instantiate it as a specific class.

The specific class generated after the template parameters of the class template are instantiated is the template class.

Ofstream, ifstream, and fstream

Ofstream: File class for write operations (output) (extended by ostream)
Ifstream: File class for read Operations (input) (extended by istream)
Fstream: A file class that supports simultaneous read/write operations (extended by iostream)

The member functions of ofstream, ifstream, and fstream of all these classes open a file by default. The default methods of these three classes are different:

Default method of class Parameters
Ofstream IOs: Out | IOs: trunc
Ifstream IOs: In
Fstream IOs: In | IOs: Out
The default value is used only when the function is called and the method parameter is not declared. If any parameter is declared when a function is called, the default value is completely rewritten without combining with the called parameter.

Regular data member

Class, you can only initialize the list of members.

The definition object modified with const is called a constant object;
Declaring a member function modified by const is called a member function;
The declared data member modified by const is a common data member.
After a variable or object is modified by const, its value cannot be updated. Therefore, the variable or object modified by const must be initialized.

Declaration method:

Const int cctwl;
Int const cctwl;
Int cctwl const; // the preceding two statements are incorrect. The data type cannot be omitted. You can add access controllers such as public private.

1. No function can assign values to common data members.
2. the constructor can only initialize a regular data member through the initialization list.
3. Regular data members must be assigned a value during initialization, or they must be initialized.
4. If the class has multiple Default constructors, you must initialize the common data member.

Write down these points first. Today we have to score 300!

Related Article

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.