Example analysis of C++ basic knowledge (I)

  • 2020-04-02 03:14:47
  • OfStack

Today this site and you through a few examples to learn the basics of C++, the following example analysis:
[1-1] write a program to realize the calculation of an integer, long integer, floating point number and double precision number divided by 2.
This is a typical function overload program. Declare the function div() as a division function, each function is basically the same function, the difference is just the type of formal parameters are different. The program code is as follows:


#include <iostream>
using namespace std;
int division(int x){ return x/2; }
long division(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 a very primitive process, and very mechanical. Interested readers will find that the above program has a lot of overlap, which is exactly what we will cover in the template later.
[1-2] write a function that finds the maximum and minimum values of three integers and returns them.
This program requires one call to get two values from the main call function. This is not a problem that value passing can solve. There are two ways to implement address passing, one is a pointer implementation, the other is to use a reference. References to variables are easy and intuitive 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;
}

[summary] this procedure and two called functions, are used by reference. Use a reference to return two parameter values at a time for convenience and brevity.
[1-3] write a function to find the volume of the cube. The default side length is 10.
This program needs to use a function with a default parameter value. When a function is declared, the parameter is given a default value. When a function is called, the default function value does not work if an argument value is entered. If no argument value is entered, the default parameter value is used. 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<<" When there is 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;  }

[conclusion] when using a function with default parameter value, it should be noted that the default parameter value can only be annotated when the function is declared, and cannot be annotated when the function is defined. Of course, function declarations and function definitions are not subject to this restriction when annotated together.
[1-4] write a program to calculate the average score of students taking 2, 3, and 4 courses.
This is also a typical function overload problem. The difference between these functions is the number of arguments. 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 first 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; }

[analysis] the starting point of the overload of these avg functions is due to the different number of parameters. And because the function body only has one sentence, and there is no complex structure such as selection and loop, so it can be defined as inline function, in order to improve the program running effect.

Above is today this site to share with you the classic examples of C++, you need to do it yourself to understand the C++ language to bring us fun.


Related articles: