c++--Study notes 012

Source: Internet
Author: User

20150516

A header file, with a suffix of H, a declaration that contains functions, data (including data type definitions), classes, and so on.

The "iostream" header file is referenced more than once in the example in order to share the input-output stream object in which it is declared

The declaration of the handle module is placed in the header file, and the specific implementation is placed in the corresponding source file

In all source code of a program, source files and header files work together to complete a complete program

The source files and header files are not only different in content, but also very different in use.

Header files are not allowed to reference source files. Although it's syntactically possible.

Header files can refer to other header files to form a new header file

Header file StdAfx.h is a collection of multi-head files

To avoid a program in which a header file may be referenced by multiple source files, in order to prevent the header file from being compiled multiple times, the variable or function declared in it is repeated multiple times.

Workaround: Use a few precompiled directives to prevent a header file from being compiled multiple times.

You can include #pragma once in the header file, which prevents a header file from being compiled multiple times.

Name space:

Use namespaces (namespace) to plan and manage program structures

Place the same name in a different namespace, so that it doesn't affect each other.

namespace syntax format:

Namespace namespace name

{

declarations and definitions within namespaces

}

What is declared and defined in the namespace belongs to this namespace

Namespaces have packaging capabilities

Name Space Zhangsan

Namespace Zhangsan

{

Student data structures in namespaces Zhangshan

struct student

{

int nindex;

int nage;//Age

};

};

Name Space Lisi

Namespace Lisi

{

Student data structures in namespaces Lisi

struct student

{

int nindex;

String strname;//Name

};

};

Global namespaces

If no specific namespace is specified, the default is in the global namespace

struct Student//student data structure in global namespace

{

int nindex;

BOOL bmale;//Sex

};

When the namespace Zhangsan and the global namespace have a student definition, using the default namespace cannot differentiate the struct, the use of the domain operator "::" must be displayed to indicate the namespace in which it resides.

Scope

Scope is divided into local scope and global scope

A scope defines an area in which an identifier is valid in a program. If the identifier is valid within a scope, it can be referenced to say that the identifier is visible within that scope.

Local scope

The range of code enclosed in {} braces. If the local scope contains a smaller child scope, the child scope has a higher priority.

A common local scope has a function body, such as If,for,while,switch

It is best not to define two variables of the same name in a function

It's best not to appear in this situation

void foo (void)

{

int nnum=0;

{

int nnum;

Nnum=1;

cout<< "Output in local scope:" <<nnum<<endl;

}

cout<< "Output in function body scope:" <<nnum<<endl;

}

If a variable or function is not in any local scope, it is said that the variable or function is in the global scope, becoming all variables or global functions.

20150517

The global scope is the entire source file scope

Define a global variable

int gn;

Define a global function

void Globalfunc (int a, int b)

{

for (GN=0;GN<0;++GN)//Access global variables

{

//...

}

}

int _tmain (int argc,_tchar* argv[])

{

gn=3; Accessing global variables

Globalfunc (2,4); Calling global functions

//...

return 0;

}

Global variables can be called anywhere in the source file

Global functions can be called anywhere in the source file

You can also define global variables in multiple source files, primarily when a program consists of multiple source files and header files.

Use the "extern" keyword to find global variables that are defined in other source files

If you want to use a variable or function, you must declare it beforehand.

Global.cpp: Defining global variables and global functions

#include "stdafx.h"

Global variables

int gtotal=0;

Global functions

int add (int a, int b)//Here is a function to take a approximate name, below will tell us what the function will do, like this is, return a+b and, if the function is referenced in other places, the direct reference to add (int a,int b) will be reached to return a + B's and the effect.

{

return a+b;

}

Use the "extern" keyword

Helloworld.cpp using global variables and global functions

Re-declare global variables by adding the "extern" keyword before the variable declaration

extern int gtotal;

Re-declare the global function by adding the "extern" keyword before the function declaration

extern int Add (int a,int b);

int _tmain (int argc,_tchar* argv[])

{

Using global variables and global functions

Gtotal=add (2,3);

return 0;

}

Compilation preprocessing

In the source code to add a special pre-compilation instructions to change the compilation process, change the program source code structure and content, to achieve flexible processing source file purposes

The most common precompiled directives:

1. #include指令

This directive is more used to embed a header file, implementing a reference to the variable or function in which it is declared

The syntax format is:

#include < file name >

#include "file name"

The difference between the two formats:

<> represents a standard way to search for this file in a compiler-developed directory, typically to introduce system-supplied header files.

"" means to search in the current directory first, if the file is not present in the current directory, and then search in the standard way. General rush to introduce the header files that you created, placed under the But Money project folder.

Introduction of standard header files

#include "stdafx.h"

Introduction of header files in standard libraries

#include <iostream>

Introduce the header file you created

#include "global.h"

2. #define #undef

One is definition, one is delete.

#define用来定义一个符号常量或者宏, generally the symbolic constants of this definition are often used in the conditional compilation directives described below.

#undef作用是删除一个由 the symbol constants or macros defined by the # define.

Defines a symbolic constant _DEBUG

#define _DEBUG

3. #if等条件编译指令

If the _DEBUG identifier is defined, the intermediate program code is compiled and two additional functions are added to the program for secondary debugging. However, as with the release version being compiled, the _DEBUG identifiers are not defined and the two functions are not compiled, thus reducing the program volume.

#ifdef _DEBUG

virtual void asservalid () const;

virtual void dump (cdumpcontext& DC) const;

#endif

4, #pragma once

Used to make a duplicate definition of a number type or function, which is only included in the current file at compile time, to avoid multiple introductions of the same file.

How the Master is tempered

Define a macro with # define

The syntax format is:

#define Identifier string

Identifiers are symbolic constants, also known as "macro names"

When you have defined a macro, you can use it in your program code instead of the string in the definition.

Define a macro

#define PI 3.14159

Calculate the area of a circle with a defined macro

Double fr=5.0f;

Double FAREA=PI*FR*FR;

Defining macros can be a simple and complex effect

Syntax format for macro definitions with parameters:

#define Identifier (argument list) string

There is no concept of type for macro parameters

When macro expansion is performed using a macro with parameters, you must not only replace the macro identifier with a string, but also replace the parameter.

Define a macro with parameters, get two more large numbers

#define MAX (a) > (b)? (a):(B))

Use macros to get 2 and 4 of a larger number

int Nmax=max (2,4);

When the macro is expanded, the macro life is replaced with a string, and the line of code becomes:

int nmax=a>b? A:B;

Then, replace the parameters in the macro definition with the actual parameters of the macro, and this line of code eventually becomes:

int nmax=2>4? 2:4;

Defining aliases for types with typedef

The syntax format for typed definition type aliases is:

typedef data type Data type Alias

Defines an alias for pointer type int* pint

typedef int* PINT;

Defines a variable of type pint, which is actually a variable of type iint*

PINT Pint=null;

1. Define a type of Alias

Effect:

Defines an alias for pointer type int* pint

Typed int* PINT

Define multiple pointer type variables at the same time

PINT Pint1,pint2

2. Define platform-independent types

typedef long double REAL;

When porting code to a platform that does not support long double, you can modify this definition as follows:

typedef double REAL;

3. Define simple aliases for complex life

Const

1. Use const instead of # define constants

Defining a Macro Pi

#define PI 3.1415926

Defining a constant Pi

Const DOUBLE PI =3.14159

The second is better than the first kind.

2. Use const to represent constants

int const * Pnumber

int * Const PNUMBER=&NUMBER

Two different kinds of differences,

A const on the left of *, one on the right. On the left, the value of the pointer itself is variable; on the right, the int variable value is variable.

3, the const is used to represent the input of the function, output parameters

BOOL Autoresizerect (crect& destrect,//output parameter can be modified

Const crect& srcrect)//incoming parameter, non-modifiable

{

//...

}

member functions that do not have a const modifier are also known as change functions

When declaring a member function of a class, if you add a const modifier at the end, you must not change any of the object's data within this member function.

Class Constdemo

{

Public

Getting data from a class

Add a const modifier after the function to indicate that this is a view function

Char getvalueat (int nindex) const

{

Accidental modification of data in a class can result in a compilation error

m_data[3]= ' A ';

return M_data[nindex];

}


c++--Study notes 012

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.