C + + Primer learning notes _5_ variables and basic Types (cont. 2)

Source: Internet
Author: User

 variables and basic types



Vii. Enumeration

Enumerations define not only the set of integer constants, but also the aggregation of them into groups. Enumerations are better than simple const constants by following a piece of code. At a glance, we know:

[CPP]View Plaincopyprint?

    1. enum  {input, output, append};
    2. Const   int input = 0;
    3. Const   int output = 1;
    4. Const   int append = 2;
enum {input, output, append};const int input = 0;const int output = 1;const int append = 2;

1 , defining and initializing enumerations

[CPP]View Plaincopyprint

"Class=" about "href=" http://blog.csdn.net/zjf280441589/article/details/22932157# ";?"

    1. ///By default, the first enumeration member is assigned a value of 0, and each subsequent enumeration member assigns a value that is 1 larger than the previous one.   
    2. enum  Open_modes{input,output,append};
By default, the first enumeration member is assigned a value of 0, and each subsequent enumeration member assigns a value greater than the previous 1.enum open_modes{input,output,append};

2 , enumeration members are constants

The value used to initialize the enumeration member must be a constant expression, which is an integer expression in which the compiler evaluates the result at compile-time.

Enumeration member values can be non-unique:

[CPP]View Plaincopyprint?

    1. enum  points {point2d  = 2,point2w,  
    2.                point3d = 3,point3w  
    3.              };   
    4.   Points a = point3w;        //correct   
    5.    Points a = 4;     //error   
Enum Points {point2d = 2,point2w,                Point3D = 3,point3w               };    Points a = point3w;//correct    Points a = 4;//error

viii. Types of classes

C + + , you define the data type by using the class.

The class defines the data that this type of object includes and the operations that the object of that type can run.

1 , from the beginning of the operation of the design class

When defining a class, you typically define the interface of the class first. That is, the operations provided by the class, through these operations. The ability to determine what data is required for the class to complete its function, and whether some functions need to be defined to support the implementation of the class.

define sales_item class:

[CPP]View Plaincopyprint?
    1. class  Sales_item  
    2. {  
    3. public :   
    4.     //...   
    5.   
    6. private :   
    7.      std::string isbn;  
    8.     unsigned units_sold;   
    9.     double  revenue;  
    10. }; //do not forget the semicolon   
Class sales_item{public://...private:std::string isbn;unsigned units_sold;double revenue;};/ /Don't forget the semicolon

2 , Public with private Access to the members of the label control class can be visited outside the class.

3 , the data member of the class defines the contents of the object of that class type.

4 , in general, the initialization of a class member cannot be used as part of its definition!.

5 , a member function of a class can use whatever member of the class, regardless of its access rights. Instead of the part of the class code, you cannot access the private member!

6 , using class and struct the only difference between keyword definition classes is the default access level!


[CPP]View Plaincopyprint?
    1. //p57 Exercise 2.28, compile the following program, note the error message   
    2. class Foo
    3. {
    4. //empty   
    5. }
    6. int Main ()
    7. {
    8. return  0;
    9. }
P57 exercise 2.28, compile the following program. Note the error message Class Foo{//empty}int main () {    return 0;}

Nine, write your own header file

in order to implement the program can be composed of multiple files. C + + support is compiled separately!

1 , header files typically include: Class definitions,extern variable declarations, and function declarations

2 , the correct use of the header file can bring advantages:

1 ) ensures that all files use the same declaration for a given entity.

2 ) When the statement needs to be changed. Only the header file needs to be changed.

P58 compiling and linking multiple source files is good and worth reading.


3 , the header file is used to declare instead of define

Exceptions:

1 ) header files can define classes

2 ) value is known at compile time. Const Object

3 ) inline function

"These entities can be defined in multiple files, with the same definition in each source file."


4 , some Const object definition in header file

1 ) Const A variable defaults to a local variable that defines the file in the variable, so it agrees Const the variable is defined in the header file.

Also, a constant expression is an expression that the compiler calculates the result at compile time.

2 In practice, most compilers replace these with the corresponding constant expressions at compile-time . Const The use of variables, so, in practice, there will be no storage space for storage that is initialized with constant expressions. Const object.

3 ) assume that Const The object is not initialized with a constant expression. Then he should not be defined in the header file. Instead, his variables should be the same as the other variables. Defined and initialized in the source file. Instead, add an extern life to the header file . So that it can be shared by multiple files.

[CPP]View Plaincopyprint

"Class=" about "href=" http://blog.csdn.net/zjf280441589/article/details/22932157# ";?"

    1. //p60 exercise 2.32  which of the following statements should be placed in the source file, which should be placed in the header file?   
    2.     int  var;  
    3.     const   double  PI =  3.1415926;  
    4.     extern   int  total = 255;  
    5.     const   double  sq = squt (2.0);   
P60 exercise 2.32 Which of the following statements should be placed in the source file, which should be placed in the header file? int Var;const Double PI = 3.1415926;extern int total = 255;const double sq = squt (2.0);

5 , avoiding multiple including

1 ), when designing a header file, it should be possible to include it in the same source file multiple times. We have to make sure that multiple classes and objects that include the same header file will not be defined for its header file are defined multiple times. Usually we need to use the header file Protector (header Guard)

2 ), #define accept a name and define a rename as a preprocessor variable

3 ), in order to ensure that the header file is only processed once in the doubtless source file, we first detect #ifndef , assuming that the preprocessor is not defined. Then all instructions followed will be processed. Until #endif, suppose #ifndef find that salesitem_h has been defined, Then the program ignores the rest of the header file.

4 ), the header file should contain a protector.

5 , we are able to name processor variables with entities defined in the header file (such as classes) to avoid the duplicate processor variables!

[CPP]View Plaincopyprint

"Class=" about "href=" http://blog.csdn.net/zjf280441589/article/details/22932157# ";?"

    1. #ifndef Salesitem_h   
    2. #define SALESITEM_H   
    3. //...   
    4. #endif//Salesitem_h   
#ifndef salesitem_h#define salesitem_h//... #endif//Salesitem_h

6 , use your own header file

[CPP]View Plaincopyprint?
    1. /*
    2. * Standard header file. The compiler will look for the header file in a pre-defined location,
    3. * Of course the location can be changed by setting the settings to find the path variable or by command-line options
    4. */   
    5. #include <standard_header>   
    6. /*
    7. * Assume that the file name is included in "". Table name is a non-system header file,
    8. * Lookup of non-system header files usually starts at the path where the source files are located
    9. */   
    10. #include "my_file.h"   

C + + Primer learning notes _5_ variables and basic Types (cont. 2)

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.