C + + class, function explanation

Source: Internet
Author: User

A class can be a user-defined class, or it can be a class that is already defined by a system, a custom class, for example, we define a person, and a person is a class, and both men and women are the subclasses of this person. There is also a class that the system has given us a defined class, we can directly use, such as Iostream class, String class, System ... Wait a minute.

What is a function? A simple understanding is the module that creates C + + programs. There are 2 types of functions, one is a return value and the other is no return value. Functions, like classes, can also be divided into custom functions and system-defined functions. We define a function ourselves, which is explained in the simplest example,

We define an addition function such as:

#include <iostream>

using namespace Std;

int ADD (int a,int b)

{

int C;

c = a+b;

return C;

}

int main ()

{

Add (+);

Cout<<add (<<endl;)

return 0;

}

You can also use system-defined functions

int x;

x = sqrt (4); This is directly using a system-defined function: A function that opens the square root.

In the above example, we can summarize the following points:

Any executable program must have a main function,

First the program enters the main function mian () First executes, the function has the return value, the function is composed by the function's head and the function body. The head of the function refers to the function's return value, and the function body is the statement in {}

Statements of the C + + type.

Declaring a statement----the name and type of the variable used in the definition function

Assignment statement----Assign a value to a variable using the assignment symbol "="

The return statement----Returns the result of a function to the caller.

Function call----as the main function calls the Add function, the return result of add is passed to the statement inside the main function.

The function prototype----declares the function's return type, and the function accepts the number and type of arguments.

The parameters of a function are divided into arguments and formal parameters, what is an argument what is a formal parameter, is very simple, the call is an argument, the call is a formal parameter.

As shown above, add (1) Here, 2 represents the actual argument, and ing add (int a,int b) Here the 2 parameters are the formal parameter.

The above is the basic implementation of the function. The next lesson is about C + + data types. Continuous update ...

C + + class, function explanation

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.