Function call, enumeration, and function call Enumeration

Source: Internet
Author: User

Function call, enumeration, and function call Enumeration

Function:

[A function is a method, and a method is a function] [the function must be written in the class]

Function Structure:

Static void function name (data type variable name) // Static is Static, private can only be used in the current class; public is public, can be used throughout the namespace

{

Function body;

}

Function usage: Find the class that contains the function, define a variable to initialize the class, and call the function and instantiate it.

For example:

Write a function (in the program class ):

Public void hanshu (int I) // multiple variables can be written in parentheses, separated by commas (,).
{
Int he = 1;
For (int j = 1; j <= I; j ++)
{
He * = j;
}
Console. WriteLine ("Result:" + he );
}

Call the function:

Program p = new Program ();
P. hanshu (3 );

 

Add a comment before the function: to avoid forgetting the function when using this function in the future, you need to add some comments so that you can place the cursor over it to display the comments that have been written, when using this function, input the following slash (/) on the function to be annotated, that is, the following is displayed (enter the function in the second line)

/// <Summary>
///
/// </Summary>
/// <Param name = "I"> </param>

  

 

Enumeration

 

Enum Enumeration

 

 

 

// Constants in the enumeration

 

Enum meiju: uint // The Index type specified after the colon. It can only be a signed and unsigned integer.

 

{

 

One = 3, // when it is equal to a number, it indicates that the specified index

 

Two = 6,

 

Three,

 

Hello word,

 

Four = three, // when it is equal to the previous constant, it is equal to the constant

 

}

 

(Enumeration can directly define the index: Define the index of one as 3, the index of two as 6, the index of three as 7, and so on)

 

(Numbers cannot be placed in enumerations. enumerations are strings that place constants)

 

(The following enumerated types are allowed: byte, sbyte, short, ushort, int, uint, long, or ulong)

 

(The constant is defined in enumeration and ended with a comma)

 

 

 

For example

 

Enum houxuanren

 

{

 

Zhangsan = 1,

 

Lisi = 2,

 

}

 

 

 

Static void Main (string [] args)

 

{

 

Int zg = 0, ls = 0;

 

For (int I = 1; I <= 2; I ++)

 

{

 

Console. Write ("please vote :");

 

Int tou = int. Parse (Console. ReadLine ());

 

Switch (tou)

 

{

 

Case (int) houxuanren. zhangsan: zg ++; break;

 

Case (int) houxuanren. lisi: ls ++; break;

 

}

 

}

 

Console. WriteLine ("the result of James's vote is:" + zg + "and the result of Li Si's vote is:" + ls );

 

Console. ReadKey ();

 


Why do I use static member functions to call enumeration types in C ++ classes?

Enumerative is approximately equal to const static int XXX;

How to Use Enumeration type in C ++ function declaration

CTime is the return type, and PASCAL is the call Convention. This method is the same as the parameter passing method in the classical language pascal (the parameter is input to the stack from right to left ), therefore, some implementations are called PASCAL call conventions. In vc, they are actually _ stdcall macros. In addition, if the function does not specify the call method, the default method is _ cdecl:
Void a () = void _ cdecl ();
At the C/C ++ code level, this does not have a big impact on the use of functions.

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.