Today's small knitting and everyone together through several examples to learn the basic knowledge of C + +, the following example resolution:
"1-1" to write a program to achieve an integer, long integer, floating-point number and double the Count divided by 2 of the calculation.
Profiling This is a typical program for function overloading. The function div () is the division function, and the function of each function is basically consistent, except that the type of the formal parameter is different. The program code is as follows:
#include <iostream> using namespace std; int division (int x) {return X/2;} long di
Vision (long x) {return X/2.} Float Division (float x) {return X/2;} Double division (double x) {return X/2;} int main () { int A; Long B; float C;
Double D; cout<< "input a,b,c,d:" <<endl;
cin>>a>>b>>c>>d;
if (a!=0) cout<<a<< "/2=" <<division (a) <<endl; else cout<< "Input error!"
<<endl;
if (b!=0) cout<<b<< "/2=" <<division (b) <<endl; else cout<< "Input error!"
<<endl;
if (c!=0) cout<<c<< "/2=" <<division (c) <<endl; else cout<< "Input error!"
<<endl;
if (d!=0) cout<<d<< "/2=" <<division (d) <<endl; else cout<< "Input error!"
<<endl;
return 0; }
"Summary" This is one of the most original procedures, and very mechanized. Interested readers will find that the above program has a lot of repetition, which is what we will cover later in the template.
"1-2" write a function that finds the maximum and minimum values from three integers and returns.
the "Analysis" program requires one call to get two values from the calling function, which is not a problem that value transfer can solve, and is bound to consider using address delivery. There are two ways to implement address delivery, one is to implement the pointer, the other is to use a reference. The reference of variables is both convenient and intuitive, which is very helpful to understand. The program code is as follows:
#include <iostream>
using namespace std;
void swap (int &a,int &b)
{int t; t=a; a=b; b=t;}
void Max (int a,int b,int c,int &maxnum,int &minnum)
{
if (a<=b) swap (a,b);
if (a<=c) swap (a,c);
if (b<=c) swap (b,c);
Maxnum=a; minnum=c;
}
int main ()
{
int a,b,c,maxnum,minnum;
cout<< "input a,b,c:"; cin>>a>>b>>c;
Max (a,b,c,maxnum,minnum);
cout<< "maxnum=" <<maxnum<<endl;
cout<< "minnum=" <<minnum<<endl;
return 0;
}
The "Summary" of this program and two of the functions, are used to reference. Use a reference to return two parameter values at once, convenient and concise.
"1-3" Write a function, the cube volume, the default edge length of 10.
the "Analysis" program requires functions with default parameter values. When the function is declared, the parameter is given a default value. When a function is called, the default function value does not work if the argument value is entered. In the absence of an argument value input, the default parameter value is used for the calculation. The program code is as follows:
#include <iostream>
using namespace std;
float volumn (float a=10,float b=10,float c=10);
int main ()
{
float a,b,c;
cout<< "No actual parameter value, volumn=" <<volumn () <<endl;
cout<< "input a,b,c:"; cin>>a>>b>>c;
cout<< "with actual parameter values, volumn=" <<volumn (a,b,c) <<endl;
return 0;
}
float volumn (float a,float b,float c)
{return a*b*c; }
When summary uses functions with default parameter values, the default parameter values can only be annotated when the function is declared, but not when the function is defined. Of course, function declarations and function definitions are not subject to this restriction when they are labeled together.
"1-4" Write a program that calculates the average score of students who take 2, 3 and 4 courses respectively.
"Analysis" is also a typical problem with function overloading. The difference between these functions is that the number of parameters is different. The program code is as follows:
#include <iostream>
using namespace std;
float avg (float,float);
float avg (float,float,float);
float avg (float,float,float,float);
int main ()
{
cout<< "the" the "the" "Student": <<avg (80.0f,70.2f) <<endl;
cout<< "The second student:" <<avg (80.1f,78.1f,12.3f) <<endl;
cout<< "The third student:" <<avg (45.6f,90.1f,78.6f) <<endl;
return 0;
}
float avg (float x,float y) {return (x+y)/2.0 }
float avg (float a,float b,float c) {return (A+B+C)/3;}
float avg (float a,float b,float c,float d) {return (A+B+C+D)/4;}
The starting point of the AVG function overload of "analysis" is that the number of formal parameters is different. And because the function body has only one word, and has not the choice and the circulation and so on complex structure, therefore also may define it as the inline function, enhances the program operation effect.
The above is today's small compiled with you share of C + + classic examples, we need everyone hands-on to understand the C + + language to bring us fun.