Object-oriented programming - a job two

About functions

1. Why function)

The program is written in front of the relatively small size of the program, the practical application, typical commercial software is usually hundreds of thousands, millions, tens of millions of lines of code even more. In order to reduce the complexity of developing large-scale software, the programmer must be a big problem into a number of small problems, small problems into smaller problems. Routine high-level language is used to achieve this module division. Subroutine C and C ++ language reflected a function. Typically relatively independent, functional abstraction frequently used as a function. After you've written a function that can be reused, can only concern specific functions have to realize functions and use functions without having to be concerned about when using functions. This facilitates code reuse, can improve development efficiency, and enhance the reliability of the program, but also easy to maintain and modify the division of labor.

Overall: easy to use as a function of writing, a clear idea of ​​the program is very practical
For example the following: writing a program calculates the integer n factorial n! .
If you do not function to complete, this will happen:
#include<iostream>
using namespace std;
int main()  
{
    int i,m;  
    long ret=1;  
    cout<<"Input m:"<<endl;  
    cin>>m;  
    for(i=2;i<=n;i++)  
    {  
        ret*=i;  
    }  
    cout<<m<<"!="<<ret<<endl;  
    return 0;  
}  
If the function of words:
#include<iostream>
using namespace std;
int main()  
{  
    int m;  
    long ret;  
    cout<<"Input m:"<<endl;  
    cin>>m;  
    ret=Fact(m);  
    cout<<m<<"!="<<ret<<endl;  
    return 0;
}  
long Fact(int n)  
{
    int i;  
    long result;  
    for(i=2;i<=n;i++)  
    {  
        result*=i;
    }  
    return result;  
 
}  

In comparison, the use of the function of the program is more able to understand the intent of the program, divided into clear

2. Why use function overloading

When using the C programming language, sometimes you find there are several functions with different names to achieve the same kind of operation. For example, to identify in claim 3 wherein from the greatest number in which the type and number of prior uncertainty 3, may be integer, long integer or real. In the preparation of C language program, you need to design a 3 respectively function, its prototype is:

int max1(int a,int b,int c);  
float max2(float a,float b,float c);  
double max3(double a,double b,double c); 

C language provision can not function has the same name in the same scope, hence the name three different functions. C ++ allows the same function name in the same scope defined by the plurality of functions, the number of these parameters and parameter types are not the same functions, these functions with the same name is used for different functions. In this case, the function for the same type of operation that we do not define the name of a completely different function, but also easy to use

For example: find the maximum number of the number 3 (considered separately integers, real numbers, the case of long integers).
#include<iostream>
using namespace std;
int max(int a,int b,int c)
{   if(b>a)a=b;
    if(c>a)a=c;
    return a;
}
float max(float a,float b,float c)
{
    if(b>a)a=b;
    if(c>a)a=c;
    return a;
}
long max(long a,long b,long c)
{
    if(b>a)a=b;
    if(c>a)a=c;
    return a;
}
int main()
{
    int a,b,c;
    float d,e,f;
    long g,h,i;
    cin>>a>>b>>c;
    cin>>d>>e>>f;
    cin>>g>>h>>i;
    int m;
    m=max(a,b,c);
    cout<<"max_i="<<m<<endl;
    float n;
    n=max(d,e,f);
    cout<<"max_f="<<n<<endl;
    long int p;
    p=max(g,h,i);
    cout<<"max_l="<<p<<endl;
}

operation result:


Thus facilitate the calling function, without defining several different function names

3. What is the value passed

Into function parameters and parameter arguments, the argument variables are present in the main function, when the calling function, the arguments passed to the function body information. In the value transfer, just pass arguments to a function argument values ​​body, regardless of the address of the argument variables.

For example: In achieving the exchange of two integers
#include<iostream>
using namespace std;
void swap(int a,int b){
    int t=a;
    a=b;
    b=t;
}
int main(){
    int x=5,y=10;
    cout<<"x="<<x<<"  y="<<y<<endl;
    swap(x,y);
    cout<<"x="<<x<<"  y="<<y<<endl;
    return 0;
}

operation result:


The results can be seen from the Run value of the parameter has not changed, it is found and is not changed by value method

4. What is the address delivered

As the name suggests, is to address the variable passed to the function body, the address will be able to obtain variable variables change

If the above code to read:
#include<iostream>
using namespace std;
void swap(int &a,int &b){
    int t=a;
    a=b;
    b=t;
}
int main(){
    int x=5,y=10;
    cout<<"x="<<x<<"  y="<<y<<endl;
    swap(x,y);
    cout<<"x="<<x<<"  y="<<y<<endl;
    return 0;
}

operation result:


Pass through changes to address to achieve the changes to the parameter, which is passed as an address

5 how to design experiments and lesson plans, writing recursive function analysis

Recursive call function refers to the function can call itself directly or indirectly, in order to reach solutions to complex problems, a recursive function should have the right recursive equation and end conditions

For example: find n! .

Analysis of the problem can be obtained the formula:
! N-. 1 = (n-= 0) or n! = n (n-1) ( n> 0)
can be written as codes based on the formula:

#include<iostream>
using namespace std;
//计算n的阶乘
unsigned fac(unsigned n){
    unsigned f;
    if(n==0)
        f=1;
    else
        f=fac(n-1)*n;
    return f;
}
int main()
{
    unsigned n;
    cout<<"Enter a positive integer:";
    cin>>n;
    unsigned y=fac(n);
    cout<<n<<"!="<<y<<endl;
    return 0;
}

operation result:


fac function, if (n == 0) f = 1 condition is the end of this part; else part is recursive equation, thus completing the operation of computing the factorial of n.

Guess you like

Origin www.cnblogs.com/YuShiqicodelife/p/11521457.html