1. Default parameters
To set a default value for a parameter, you must provide a default value for all parameters to the right of it
intHarpointNintm =0,intj =5)/*VALID*/intChicointNintm =6,intJ/*INVALID*/intGrouchointK =0,intm =2,intn =3)/*VALID*/
2. Function overloading
The parameter list of the function, also known as the feature label of the function.
Is the signature, not the function return type, which allows the function to be overloaded.
P.S. When the compiler checks a function feature, it sees the type reference and the type itself as the same feature label.
function overloading should only be used when the function basically performs the same task, but uses different forms of data.
3. Function templates:
Template <class any>/ * can also be written as template <typename any> */ void Swap (any &a, no &b) {any temp; = A; A = b; b = temp;}
Examples of programs:
#include <iostream>
Template<typename any>voidSwap (any &a, any &b);/*Statement*/intMain () {using namespacestd; inti =Ten; intj = -; Swap (i, j); cout<<"After swap, i ="<<i<<", j ="<<j<<Endl; return 0; } Template<typename any>voidSwap (any &a, any &b) {any temp; Temp= A; A = b; b =temp;}
4. Overloaded templates:
Template <classAny>/*can also be written as template <typename any>*/voidSwap (any &a, any &b) {any temp; Temp= A; A = b; b =temp;} Template<classAny>/*can also be written as template <typename any>*/voidSwap (Any a[], any b[], int n) {any temp; for(inti =0; I < n; i++) {Temp= A[i]; A[i] = B[i]; B[i] =temp; }}
Display materialization (Explicit specialization)
Start with template<> and indicate the type by name
e.g.
struct Job {
Char name[20];
Double salary;
int floor;
};
template<> void Swap<job> (job& A, job &b)
Or it can be written in a simpler form:template<> void Swap (Job &a, job &b)
If there are multiple prototypes, the compiler chooses the prototype, non-templated version > Display materialized > Template version
Non-template function prototype
void Swap (Job &, Job &)
Template prototype
Template<typename any>
void Swap (any &, any &)
Explicit specialization for the job type
template<> void Swap (Job &, Job &)
Display instantiation (Explicit instantiation)
template void swap<int> (int, int)(displays materialized declarations after template<>)
After the compiler sees the above declaration, it uses the swap () template to generate an instance of the type int.
5. Using declaration using Std::cout;
Using compiler command using namespace std;
namespaceJill {DoubleBucketDoubleN) {...} Doublefetch; structHill {...};}Charfetch;intMain () {using namespaceJill; Hill thrill; /*using Jill::hill*/ DoubleWater = Bucket (2);/*using Jill::bucket*/ DoubleFetch/*Hide Jill::fetch*/Cin>> fetch;/*Local Fetch*/Cin>>:: Fetch;/*Global Fetch*/Cin>> Jill::fetch;/*Jill::fetch*/
return 0;}
C + + Knowledge section