C + + Primer 3rd reading notes

Source: Internet
Author: User
Tags logical operators

 C + + Primer 3rd reading notesCategory: C/c++/vc2014-04-08 20:43 318 people read Comments (0) favorite reports

The first chapter begins

1. Standard header files in C + + are not with the. h suffix. The following code is worth noting:

#include <iostream.h>

void SayHello ()

{

       cout<< "Hello world!";

}

void Main ()

{

       SayHello ();

}

#include <iostream>

using namespace std;

void SayHello ()

{

       cout<< "Hello world!";

}

void Main ()

{

       SayHello ();

}

#include <iostream>

void SayHello ()

{

&NBSP;&N bsp;     std::cout<< "Hello world!";

}

void Main ()

{

       SayHello ();

}

When referencing header files with. h, you do not need to use namespace. When referencing a standard header file without a suffix, you need to use namespace. There are two ways to use namespace: 1. Called with the Using keyword. 2. Before the specific function pass:: Call. such as Std::cout.

2. #include称之为预处理器, there are two forms of:<> indicating the file when the standard header file, the lookup process checks the predefined directory, "" refers to the user-supplied header file that represents the file, the lookup file starts from the current directory, and the current directory does not find a predefined directory. Prevent duplicate processing with the following code:

#ifndef Bookstore_h

#define Bookstore_h

Content

#endif

Use the following code to determine whether the preprocessor constants have been defined:

int main ()

{

#indef DEBUG

cout<< "Beginning Execution of Main ()";

#endif

cout<< "Hello";

}

If the Debuge pre-compiler constant is defined, the cout<< "Beginning Execution of Main ()" is compiled, and if no debuge is defined, the sentence is not compiled.

3. The basic input and output streams are: cin, cout, and Cerr. The newline output is cout<<endl; or cout<< ' \ n ';

Chapter II Browse

1. In C + +, an array cannot be directly assigned a value. In Java, the array can be directly assigned values.

2. In C + +, it is divided into static (in the stack) and dynamic (in the heap) to allocate memory. Static allocation at compile time , high efficiency but lack of flexibility, dynamic allocation at execution time allocation, low efficiency but high flexibility. A static object is a variable with a name , which we manipulate directly; A dynamic object is an object with No name , and we manipulate it with pointers. The allocation and deallocation of static objects is handled automatically by the compiler, and the allocation and deallocation of dynamic objects is explicitly managed by the programmer, using new and delete.

int *pia = new int (1024x768);//pia points to the address of a single object

int *pint = new Int[4];//pint points to the address of the first element with an array of four elements

Delete pia;//Deleting a single object

Delete []pint;//Delete array

3. When invoking the method of an object, the point operator is used to call,-> for the object pointer using the class object .

4. The overhead of accessing the function is greater than the overhead of directly accessing the memory. When accessing the object's variables through the object's methods (accessing the object by accessing the function, accessing the object's variable direct access to the memory), C + + uses the inline mechanism to expand the calling function at the point of invocation, saving overhead.

5. A reference is a pointer with no pointer syntax.

6. The constructor of the class is used to initialize the data members of the object, and the destructor is responsible for releasing all resources requested in the object's life cycle.

7. In an inheritance operation, a function in the base class that needs to be implemented in a subclass should use the virtual decoration. C + + supports multiple inheritance. The inheritance relationship reflects the relationship of Is-a. The composition relationship reflects the relationship of Hava-a.

Chapter III C + + data types

1. Basic Data type:

char (1bit), short (2bit), int (4bit), Long (4bit), float (4bit), double (8bit)

where char, short, int, and long are integral types, signed and unsigned, and signed by default. Literal constants, such as the number 1, whose values cannot be changed and are not addressable. Variables are addressable compared to literal constants, and each variable has two values corresponding to the data value and the address value, respectively.

2. Class A; is a declaration, the declaration is not a definition and does not cause memory allocation.

A (100); static assignment

A *a = new A (100); dynamic assignment

3. Keywords in C + +:

Keyword explicit can prevent the " single-argument constructor " from being used for automatic type conversions

Class Stack

{

explicit Stack (int size);

};

Without explicit, stack s = 40; can be compiled by

And there is explicit can not, must stack s (40);

Keyword mutable meaning: variable. Used to modify some data members that are not related to the class state in a const function

Keyword volatile is designed to modify variables that are accessed and modified by different threads. The role of volatile: as an instruction keyword, ensure that this instruction is not omitted due to compiler optimizations and requires each direct read value.

4. Initialization of a basic data type variable: If the variable of the base type is defined in the global domain , the system initializes its value to 0, and if it is a local domain or is defined by new , the system does not initialize it. .

5. Initialization of objects is supported in two ways. 1.int A=100;2.inta (100);

6. A null pointer (void*) can accept any pointer-type address value assignment, which indicates that the value is an address value, but the object type of the address value is not known and cannot know the area of memory covered by the pointer.

7. C + + provides two styles of string types, one is an old-fashioned C-style string, and the other is the standard C + + introduced string. Using old-fashioned C-style strings requires #include<cstring>, which is often pointed with a char * type pointer. For example Char *s= "Hello", when Char *s=0, or char *s= "", and its length is 0. The old-fashioned string uses the length of the strlen tester, not includingthe. Using a string of standard C + + strings, you need to include the header file #include<string>, and use the size () method to get its length. Determines whether a string is empty by size () = =0 or the empty () method. String provides support for strings in older C languages, and can be used to convert old-fashioned C-style strings to string types, or vice versa, by Constchar *s = S1.c_str ().

8. assign a value to a string. S2=S3; The process is to first release the S2 associated character store, and then allocate enough storage to store the characters associated with S3, and finally copy the characters associated with the S3 to the store. Notice the difference here from Java.

9. Const defines a variable as a constant, so that the value of the variable cannot be modified . constants must be initialized . A pointer to a non-const object cannot point to a const object . A const pointer can point to a non-const variable, and its value cannot be modified by a pointer at this time.

A Const int*p=0;//(right-to-left) p is a pointer to type int that is defined as a const object. Call it: pointer to const . a pointer to a const can point to another, but cannot change the value pointed to. The const int *P is equivalent to the int const *p. (The const keyword is in the first, second position is a pointer to const)

Const double minwage = 0;

p=&minwage;

*p=100;//x Pointer to Const can point to other, but cannot change the value pointed to

Double dval=3.14;

p=&dval;

*p=100;//x pointing to the const pointer can point to the other, but cannot change the value pointed to

Pointers to const are commonly used in parameters of a function, such as int strcmp (const char *STR), to ensure that the actual object passed into the function is not modified in the function.

B Interr=0;int *const Cur=&err;//cur is a const pointer to type int. Call it:const pointer . A const pointer cannot point to another address value, but it can modify its value.

       c.const Double Pi=3.14;const double *constpi_ptr=&pi; a const pointer to the const type.

Const int AA = +;

       int bb = $;

       int cc =;

      //Pointer to const

       const int *p = &AA;

      //*p=100; Error

       p=&bb;

      //*p=100; Error

      //const Pointer

      //int *const pp = &aa; error, pointers of non-const types cannot point to const constants

       int *const pp = &bb;

      //pp=&cc; Error

       *pp =;

       int const * PPP = &AA;

       PPP = &CC;

10. A reference is a pointer with no pointer syntax, a formal parameter to a function that passes a class object to a function. The reference must be initialized. Once a reference is defined, it cannot point to another object. All operations on the pointer are applied to the object to which it is pointing.

int a = 100; Int&p = A; (reference to type int)

int *pi = &a;int *&ppi = pi, ( reference to pointer,& and * action offset )

A reference to a const can be initialized with literal constants, for example: const int &p = 3.14; Its internal implementation is implemented by defining a variable for literal constants and then referencing to that variable. int *pi=0; The interior is actually: int tmp=0; const int &ri=tmp; Instead of a reference to a const, it cannot be initialized (int &p=1;x) because literal constants cannot be addressed.

for const INTIVAL = 1024, define a reference to the Ival pointer as follows:

const int *const&p = &ival; (The first const note points to a constant, the second const note is a const pointer and can only point to a 1024 address, because the address of the literal constant is fixed)

The difference between a reference and a pointer;

1. A reference must always point to an object that must be initialized. The pointer can be empty.

2. When the references are assigned to each other, the referenced object is changed instead of the reference itself.

int &ri = ival, &ri2=ival2;ri=ri2; changes the ival, not the reference itself. After the assignment, two references still point to the original object.

11. Enumeration: Used to specify that an object contains a specified number of values. Enum forms{a=2,b,c}.c=4.

12. The array dimension value must be a constant expression, and it must be able to calculate its value during compilation. You can not write dimension values when explicitly initializing. The string is also an array, but it contains an extra "\". An array cannot be initialized by another array, nor can it be assigned to another array. C + + does not provide bounds checking of the compiler array.

13. Arrays and Pointers: The array identifier represents the address of the first element in the array, and its type is a pointer to the array element type.

Vectors can be initialized by another vector and can be assigned to another vector.

an easy-to-mistake problem of the TypeDef ;

Typedef Char *cstirng;

Extern const CString CStr;

Ask CStr what is it?

A pointer to a const char is not a const character *CSTR, but a const pointer to a char char * const CSTR.

The action of the volatile modifier is to tell the compiler that the value of the object may be changed without the compiler being monitored. Therefore, the compiler cannot arbitrarily optimize the code that references these objects.

The pair provides a storage structure in the form of key-value, with the need to include <utility>

Fourth Chapter expressions

1. The% modulo operation symbol can only be applied to integer operands, such as char, short, int, and long.

2. Relational and logical operators are automatically promoted to 1 or 0 if they are applied in the context of an integer value.

3. You cannot use a method such as a[index--]<a[index++], because C + + does not specify whether the program calculates the order from left to right or right to left. Should be: a[index--]<a[index+1].

4. Use i++ and ++i to implement the stack.

push:stack[top++] = value;

Pop:value = Stack[--top];

5. The sizeof operator is the function of returning the length of an object or type name. when you use sizeof to calculate an array, the entire type length of the array is returned, not the length of the first element. When sizeof computes a pointer, sizeof (p) =4;siziof (*p) =p points to the type size of the memory element. When sizeof computes a reference, it returns the type size of the reference. both sizeof A and sizeof (a) are correct in both ways.

       size_t St;

       st = sizeof St;

       std::cout<< "st=" <<ST;

       Double *sp = new double[3];

       int as[] = {1,2,3,4};

       std::cout<< "sizeof (SP) =" <<sizeof (sp) <<std::endl;

       std::cout<< "sizeof (*SP) =" <<sizeof (*sp) <<std::endl;

       std::cout<< "sizeof (AS) =" <<sizeof (as) <<std::endl;

       std::cout<< "sizeof (double *)" <<sizeof (double *) <<STD:: Endl;

       std::cout<< "sizeof (Double &)" <<sizeof (double &) < <std::endl;

The output is: 4, 8, 16, 4, 8

Also: sizeof (' C ') =1;sizeof ("c") = 2; "contains" sizeof (std::string) =16;string str = "s"; sizeof (str) = 16;

6. The area where the system allocates memory for the program is called an idle store or heap. Use new to perform an operation that allocates memory. all objects allocated from the heap are unnamed, and the new expression returns not the object itself, but the address of the object, all of which are performed indirectly through the address value. Use Delete to delete the object that we requested from the heap. Delete a single element: delete p; Remove array: delete[] p;

7. Inta = 3;int B = A + 1.24; The first is promoted to double, then the result of a double is a+1.24, then the result is converted to int (the compiler will error). The following are the cases where the stealth conversion occurs:

1. Mixed operation: int a= 3; Double d = 3.243; Double DD = a + D;

2. Assign a value to an object of another type with one type of expression. A = D; or int *p = 0;

3. Pass an expression to a function call, and the expression type differs from the parameter type. Extern double sqrt (double); COUT<<SQRT (2);

4. Returns an expression from a function that differs from the return type. Double dif (int a,int b) {return a–b;}

8. In arithmetic operations:

1. The type is always promoted to a wider type. Int->float->double->long Double

2. Arithmetic expressions that are less than an ordered type of shaping are converted to shaping. such as Char, signed Char Short, and so on. In addition to the values in the enum, the Boo value is also promoted to the shaping Int.

9. Explicit type conversions Common commands: static_cast (static transformations, general conversions), dynamic_cast (runtime recognition of class objects pointed at by pointers or references), const_cast (const constant conversions), and reinterpret_cast. The syntax for the display type conversion is:castname<type> (expression);

Legacy cast Syntax:

1. C + + cast symbol: type (expr)

2. C-language cast symbol: (type) expr;

any pointer to a non-const data type can be assigned a value of void*.

Fifth Chapter Statement

1. String*p, p1; defines p as a pointer, p1 as a string. string *p,*p1; Defines P,P1 as a pointer.

2. Switch is executed from the entry point if no break is added.

3. Break: Jump out of the loop body; continue: Jump out of this cycle and turn to the wash cycle.

C + + Primer 3rd reading notes

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.