Step-by-step learning C + +(chapter two summary)

来源:互联网 发布:图片在数据库中的类型 编辑:程序博客网 时间:2024/04/27 20:23

Chapter Two  Summary

1. C plus plus is a statically typed language.

     Type-checking is done at compile time.

     In contrast,  notably Smalltalk and Python,  type-checking is done at the run time.

2.  float & double(32 bit PC)

  •     float: single-precision floating-point , has 6 significant digits
  •     double: double-precision floating-point, has 10 significant digits                   

3. Direct-Initialization & Copy-Initialization

  • Direct initialization : int val(1024)
  • Copy initialization : int val = 1024
  • Initialization:  Created  a variable & initial value
  • Assignment: Obliterating an object's current value & value with a new one
  • Suggestion: Every object of built-in type be initialized. (Best practices!)

4. Declare & Define

  • Definition : Allocates storage for the variable and may also specify an initial value for the variable.
  • Declaration: Makes known the type and name of the variable to the program.

             such as : extern int i ; // declares but does not define i

                               int i; // declares and defines

5. Scope of a Name

  • A scope is a region of the program.

  • A name can refer to different entities in different scopes.

  • Global Scope & Local Scope

               Global Scope : Names defined outside any function .

               Local Scope: Defined within the scope of the some function.

  • Define variables where they are used .(Best practices.)
  • Scope in c++ Nest

            The local define nameValue variable is hides the global the same name of define variable.

  • Class Scope & Namespace Scope (Letter Declare)

6 . Magic number

  • 512 , Is known as a magic number , lets program to unreadability.

                       slove method: define macro & const qualifer

7. Reference 

     NOTE

  •    Alias of variable , can't need allocate storage .
  •   A reference must be initialized using an object of the same type as the reference.
  •  When a reference is  initialized , it remain bound to that object as long as the reference exists.
  • There is no way to rebind a reference to a different object
  • Terminology : const reference  is a reference to const

8. const Qualifer

  • Recommended:

          Non const variable are extern by default. 

          To make a const variable accessible to other files we must explicitly specify that it is extern.

9. Class Type 

  • Class Type = interface + implementation
  • Class Type = member function + data member
  • Access Labels: public, protected, private
  • Struct Vs Class

    The only different between a class defined with the class keyword or  the struct keyword is the default access level

    By default , members in a struct are public, those in a class are private.

10. Separate Compilation

          To allow programs to be broken up into logical parts, C++ supports what is commonly known as Separate compilation. Separate Compilation lets us compose a program from several files.

      Goal: To reduce the compile time.

11. Preprocessor

Avoiding Multiple inclusions 

              We can use these facilities to guard against including a header more than one:

                    #ifndef HeaderFileName_H

                    #define HeaderFileName_H

                       //Difinition of ClassName and related function goes here

                    #endif

原创粉丝点击