"" Data structure "Experiment 1: flexible use of VC programming tools" as well as summary

One. .Purpose

     Review and consolidate VC programming environment used, and the C ++ template design.

1. Review and master VC single file structure of the program design process.

2. Recalls VC multiple files and master the engineering design process

3. Master VC program debugging process.

4. Review procedures C ++ templates and template design.

two. Experiment time

   The second week of the second class. 2 hours.

three. Content Experiments

1. Design a single complete file structure of the program from the keyboard input "and" and "product" is the result of two numbers, both of the output. Requirements are as follows:

1) Design function to calculate the "and" and "product", the main function call, and can be considered overloaded functions , fractional and integer can make calculations.

2), respectively, using the single-step debugging and breakpoint debugging by the debugger . And repeatedly seek skilled run debugging methods.

2. Use the template function to achieve the above functions.

3. Use a class to implement the above functions. Claim:

  1) Use a template class

  2) using the multi-file: class declaration in the header files; function definition in a class source file, the main function of the design program in the main program file, in the example of the output result.

Code One:

#include<iostream>  
using namespace std;  
void result(int ,int );  
void result(double ,double );  
  
void result_1(int c,int d)  
{  
    cout<<"the result of multiplication is: "<<c*d<<endl;  
    cout<<endl;  
    cout<<"the result of the conbimed is: "<<c+d<<endl;  
}  
  
void result_2(int c,double f)  
{  
    cout<<"the result of multiplication is: "<<c*f<<endl;  
    cout<<endl;  
    cout<<"the result of the conbimed is: "<<c+f<<endl;  
}  
  
void result_1(double j,int d)  
{  
    cout<<"the result of multiplication is: "<<j*d<<endl;  
    cout<<endl;  
    cout<<"the result of the conbimed is: "<<j+d<<endl;  
}  
  
void result_2(double j,double k)  
{  
    cout<<"the result of multiplication is: "<<j*k<<endl;  
    cout<<endl;  
    cout<<"the result of the conbimed is: "<<j+k<<endl;  
}  
int main()  
{  
    int a,b;  
    double x,y;  
    cout<<"input two numble: ";  
    cin>>x>>y;  
    a=x,b=y;  
    if(a==x)  
    {  
        if(b==y)  
            result_1( a, b);  
        else  
            result_2( a, y);  
    }  
    else   
        if(b==y)  
            result_1( x, b);  
        else  
            result_2( x, y);  
    return 0;  
}  



Code II:

#include<iostream>  
using namespace std;  
  
template<class T>  
T result(T a,T b)  
{  
    cout<<"the result of multiplication is: "<<a*b<<endl;  
    cout<<endl;  
    cout<<"the result of the conbimed is: "<<a+b<<endl;  
          
    return a*b,a+b;  
}  
  
int main()  
{  
    double x,y;  
    cout<<"input two numble: ";  
    cin>>x>>y;  
    result(x,y);  
    return 0;  
}  




to sum up:

The experiment failed miserably, before the whole thing forgotten, and read the book can only get the first two, class templates that are not familiar with it, how also do not add up, only temporarily empty, evil seems to need a good up.

Function overloading features: 1, two or more of the same function name, but the number or parameter should not be the same type parameter, rather than return a different value types; 2, if the parameters are a function the default value, must ensure that the call is not to be confused with other forms of function after its default parameters; 3, the algorithm functions should not be different.

函数模板的特征:1、必须先说明函数模板,然后实例化成相应的模板函数进行调用;2、template<模板参数表>尖括号不能为空,参数可以有多个;3、如果类型多于一个,则每个类型形参都要使用class或typename。<模板函数参数表>中的参数必须是唯一的,而且在<函数定义体>中至少出现一次。



Guess you like

Origin blog.csdn.net/z1094219402/article/details/39611183