函数重载

#include<iostream> using namespace std; //名字相同,但是参数列表不能相同 //跟缺省值一起结合用,可能会出现报错/冲突 //返回值不能作为函数重载的条件 void fun(int a,double b) { cout <<"1: "<<a <<' ' <<b <<endl; } void fun(int a,float b) { cout <<"2: "<<a <<' ' <<b <<endl; } void fun(int a,char c=...
c++基础 | 2017-04-04 15:46 | 阅读 854 次 | 评论 0 条

函数缺省值与相对C语言增强版的for循环语句

#include<iostream> using namespace std; void fun(int a,char c,float d=1.233) //带缺省值的函数,从右到左,必须要连续赋予缺省值 { cout <<a <<' ' <<c <<' ' <<d <<endl; for(int i=0;i<10;i++) //可在循环内声明初始变量 { cout <<i <<' ' ; } //cout <<i; //这样是不对的,因为上面for循环里面的i只作用于循环内而非整个函数 cout <<endl...
c++基础 | 2017-04-04 14:26 | 阅读 1101 次 | 评论 0 条

引用的一个小应用

#include<iostream> using namespace std; void Swap(int &amp;Num1,int &amp;Num2) //引用做参数 { int Temp=Num1; Num1=Num2; Num2=Temp; cout <<Num1 <<' ' <<Num2 <<endl; } void Swap1(int a,int b) //普通参数, { int Temp=a; a=b; b=Temp; cout <<a <<' ' <<b <<endl; } void ...
c++基础 | 2017-04-04 13:29 | 阅读 837 次 | 评论 0 条

野指针的避免

初始化时置 NULL 指针变量一定要初始化为NULL,因为任何指针变量(除了static修饰的指针变量)刚被创建时不会自动成为NULL指针,它的缺省值是随机的。 释放时置 NULL 当指针p指向的内存空间释放时,没有设置指针p的值为NULL。delete和free只是把内存空间释放了,但是并没有将指针p的值赋为NULL。通常判断一个指针是否合法,都是使用if语句测试该指针是否为NULL。
C语言 | 2017-04-02 22:46 | 阅读 886 次 | 评论 0 条

C语言允许函数的返回值是一个指针(地址),这样的函数称为指针函数

#include <stdio.h> #include <string.h> char *strlong(char *str1, char *str2){ if(strlen(str1) >= strlen(str2)){ return str1; }else{ return str2; } } int main(){ char str1[30], str2[30], *s...
C语言 | 2017-04-02 11:34 | 阅读 1202 次 | 评论 0 条

命名空间

namespace name //不能重复 { void sort() { }; } using namespace name; //类似于头文件之类的吧,不过放在代码之中,必须放在namespace name之后 int main() { std::sort(); }
c++基础 | 2017-03-29 12:51 | 阅读 721 次 | 评论 0 条

我的第一个c++program

#include<iostream> int main() { using namespace std; cout << "let's coding !haha~" <<endl;//输出语句且换行 cout ; //这个语句可以跳过 cout << "you wont's be so worried!"; return 0; }
c++基础 | 2017-03-28 20:22 | 阅读 845 次 | 评论 0 条