variables and basic types
Vii. Enumeration
Enumerations define not only the set of integer constants, but also the aggregation of them into groups; The enumeration is inferior to a simple const constant, which is known by the following piece of code:
[CPP]View Plaincopyprint?
- enum {input, output, append};
- Const int input = 0;
- Const int output = 1;
- 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?
- ///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.
- 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 that the compiler can calculate the result at compile time.
Enumeration member values can be non-unique:
[CPP]View Plaincopyprint?
- enum points {point2d = 2,point2w,
- point3d = 3,point3w
- };
- Points a = point3w; //correct
- 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 + + , the data type is defined by the class. A class defines the data that an object of that type contains and the actions that an object of that type can perform.
1 , starting from the operation of the design class
When you define a class, you typically define the interface of the class, that is, the operations provided by that class, which determine the data that the class needs to complete its functionality, and whether you need to define functions to support the implementation of the class.
define sales_item class:
[CPP]View Plaincopyprint?
- class Sales_item
- {
- public :
- //...
-
- private :
- std::string isbn;
- unsigned units_sold;
- double revenue;
- }; //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 Label controls whether members of the class can be accessed outside the class.
3 , the data member of the class defines the contents of the object of that class type.
4 , generally, the initialization of a class member cannot be used as part of its definition!!!
5 , a member function of a class can use any member of the class, regardless of its access rights. Code that is not part of a class cannot access private members!
6 , using class and struct the only difference between a keyword definition class is the default access level!
[CPP]View Plaincopyprint?
- //p57 Exercise 2.28, compile the following program, note the error message
- class Foo
- {
- //empty
- }
- int Main ()
- {
- return 0;
- }
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 a program that can consist of multiple files, C + + support to compile separately!
1 , header files typically contain: Class definitions,extern variable declarations, and function declarations
2 and the benefits of proper use of the header file:
1 ) ensures that all files use the same declaration of a given entity.
2 When the declaration needs to be modified, only the header file needs to be modified.
P58 compiling and linking multiple source files is very 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, as long as the definitions in each source file are the same"
4 , some Const object definition in header file
1 ) Const a variable defaults to a local variable that defines the file in the variable, and therefore allows Const the variable is defined in the header file.
Also, a constant expression is an expression that the compiler can calculate 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 storing initialized with constant expressions Const object.
3 ) If Const object is not initialized with a constant expression, then he should not be defined in the header file. Instead, his variable should be defined and initialized in the source file, just like any other variable. Instead, add an extern life to the header file so that it can be shared by multiple files.
[CPP]View Plaincopyprint?
- //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);
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 , avoid multiple inclusions
1 ), when designing a header file, you should make it available for inclusion in the same source file multiple times. We have to make sure that the class and objects that contain the same header file more than once will not be defined by 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 , if the preprocessor is undefined, then all instructions followed will be processed until #endif , if #ifndef found Salesitem_h is already defined, the program ignores the rest of the header file.
4 ), the header file should contain a protector!
5 , we can name the processor variable with the entity (such as Class) defined in the header file to avoid the problem of duplicate processor variables!
[CPP]View Plaincopyprint?
- #ifndef Salesitem_h
- #define SALESITEM_H
- //...
- #endif//Salesitem_h
#ifndef salesitem_h#define salesitem_h//... #endif//Salesitem_h
6 , use your own header file
[CPP]View Plaincopyprint?
- /*
- * Standard header file, the compiler will find the header file in a predefined location,
- * Of course, the location can be set to find the path variable or through the command line options to modify
- */
- #include <standard_header>
- /*
- * If the file name is contained in "", the table name is a non-system header file,
- * Lookup of non-system header files usually starts at the path where the source files are located
- */
- #include "my_file.h"
C + + Primer learning notes _5_ variables and basic Types (cont. 2)