I've been a sophomore and had my own major in CES from now on!
The next thing I'm going to do i s to write down and suumarize what I've learned in class about CS once a week . Sometimes I may report some new knowledge learned by myself or problems I have.
Doing this just for supervising myself to keep studying CS regularly.If you skim over my passage unwittingly,you can comment or criticse any unproper detail you find which will help me a lot to make progress.Thanks!
$1function
.......
1.1recursive function(refer to my last passage)
Here's a Hanoi problem,it's classic.
#include<iostream>
using namespace std;
void move(char x,char y)
{
cout<<x<<" --> "<<y<<endl;
}
void Hanoi(int n,char x,char y,char z) //move x to z by y
{
if(n==1)
move(x,z);
else
{
Hanoi(n-1,x,z,y);
move(x,z);
Hanoi(n-1,y,x,z);
}
}
int main()
{
int n;
cin>>n;
Hanoi(n,'A','B','C');
return 0;
}
1.2function overload:the functions have same name but the number of parameter or when the number is the same the type or showing order must be different.
1.3default parameter:must start from the right side.
1.4inline function
1.5function template:for all the same steps but the different type of parameters.
Format:template<typename+ name of type> return type+ name of function template(list of parameter)
Example:
#include<iostream>
using namespace std;
template <typename T> T Max(T a,T b) //declaration
{
return (a>b)?a:b;
}
char *Max(char *a,char *b) //overload the function template to compare the content of string rather than the first address.
{
return strcmp(a,b)>=0?a:b;
}
int main()
{
int a=3,b=5;
char c='3',d='5';
double x=3.5,y=5.5;
char s[10]="abcd",t[10]="ABCD";
cout<<Max(a,b)<<endl; //automaticly define
cout<<Max(c,d)<<endl;
cout<<Max(x,y)<<endl;
cout<<Max(s,t)<<endl;
return 0;
}