目录
1.c++是c语言的拓展
1.1 布尔类型
1.2输入与输出的使用
1.2.1cin&&cout
1.3函数重载
1.3.1 C++通过重载来简化编程
1.3.2 重载的规则
1.3.3 匹配原则
1.3.4 重载的底层原理
1.4 默认参数(默认参数顺序(从右到左)和调用顺序(从左到右))
1.c++是c语言的拓展
1.1 布尔类型
c语言中,逻辑的真假使用0和1表示,而c++中有了具体的数据类型,bool类型
//bool类型,sizeof(bool)=1 #include "stdafx.h" #includeusing namespace std; int _tmain(int argc, _TCHAR* argv[]) { bool flag=true; if(flag!=false) { printf("i know bool type nown"); } printf("bool size =%dn",sizeof(bool)); system("pause"); return 0; }
1.2输入与输出的使用
1.2.1cin&&cout
// 第一节.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char name[30];
int age;
cout<<"pls input name and age:"<>name;
cin>>age;
//cin>>name>>age
cout<<"Your name is:"<
1.3函数重载
1.3.1 C++通过重载来简化编程
// 第一节.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
using namespace std;
int ab(int a)
{
return a>0?a:-a;
}
double ab(double a)
{
return a>0?a:-a;
}
int main()
{
cout<
函数重载: 函数重载是一种特殊情况,C++允许在同一作用域中声明几个类似的同名函数,这些同名函数的形参列表(参数个数,类型,顺序)必须不同,常用来处理实现功能类似数据类型不同的问题。
1.3.2 重载的规则
1.函数名相同
2.参数的个数不同,参数的类型不同,参数的顺序不同皆可构成重载
3.返回值类型不同则不可以构成重载
//函数重载规则
//
#include "stdafx.h"
#include
#include
using namespace std;
void func(int a);
void func(char a);
void func(char a,int b);
void func(int a,char b);
//char func(int a);错误,与第一个函数有冲突
int main()
{
system("pause");
return 0;
}
1.3.3 匹配原则
1.严格匹配
2.通过隐式转换寻找一个匹配,找到就调用
#include "stdafx.h"
#include
#include
using namespace std;
void print(double a)
{
cout<
1.3.4 重载的底层原理
C++利用name mangling(倾轧)技术,来改变函数名,区分参数不同的同名函数
1. Name Mangling 简介
Name Mangling 是一种在编译过程中,将函数、变量的名称重新改编的机制。在 C++重载、namespace等操作符下,函数可以有同样的名字,编译器为了区分各个不同地方的函数,将各个函数通过编译器内定的算法,将函数改成唯一的名称。
用v c i f l d表示void char int float long double 及其引用。
如: void func(char a); //func_c(char a)
void func(char a,int b,double c); //func_cid(char a,int b,double c)
除此之外,这种编码必须简单,而且要可逆,就是能由全局名称很快的知道局部的名称。
1.4 默认参数(默认参数顺序(从右到左)和调用顺序(从左到右))
规则:
1.默认的顺序从右到左,不能跳跃
2.默认参数调用顺序从左到右
// 默认参数
//
#include "stdafx.h"
#include
#include
using namespace std;
//默认参数顺序从右到左
float volumn(float length,float weight =4,float high =5)
{
return length*weight*high;
}
int main()
{
//调用顺序从左到右
float v0=volumn(10);
float v1=volumn(10,20);
float v2=volumn(10,20,30);
cout<
3.一个函数,不能既做重载,又做默认参数的函数。当你少写一个参数时,系统无法确认是重载还是默认参数。
// 默认参数
//
#include "stdafx.h"
#include
#include
using namespace std;
void print(int a)
{
}
void print(int a,int b=10)
{
}
int main()
{
print(10);
system("pause");
return 0;
}
显示错误
1.5 引用
// 第一节.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #includeusing namespace std; int _tmain(int argc, _TCHAR* argv[]) { char name[30]; int age; cout<<"pls input name and age:"< >name; cin>>age; //cin>>name>>age cout<<"Your name is:"< 1.3函数重载
1.3.1 C++通过重载来简化编程
// 第一节.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include#include using namespace std; int ab(int a) { return a>0?a:-a; } double ab(double a) { return a>0?a:-a; } int main() { cout< 函数重载: 函数重载是一种特殊情况,C++允许在同一作用域中声明几个类似的同名函数,这些同名函数的形参列表(参数个数,类型,顺序)必须不同,常用来处理实现功能类似数据类型不同的问题。
1.3.2 重载的规则
1.函数名相同
2.参数的个数不同,参数的类型不同,参数的顺序不同皆可构成重载
3.返回值类型不同则不可以构成重载
//函数重载规则 // #include "stdafx.h" #include#include using namespace std; void func(int a); void func(char a); void func(char a,int b); void func(int a,char b); //char func(int a);错误,与第一个函数有冲突 int main() { system("pause"); return 0; } 1.3.3 匹配原则
1.严格匹配
2.通过隐式转换寻找一个匹配,找到就调用
#include "stdafx.h" #include#include using namespace std; void print(double a) { cout< 1.3.4 重载的底层原理
C++利用name mangling(倾轧)技术,来改变函数名,区分参数不同的同名函数
1. Name Mangling 简介Name Mangling 是一种在编译过程中,将函数、变量的名称重新改编的机制。在 C++重载、namespace等操作符下,函数可以有同样的名字,编译器为了区分各个不同地方的函数,将各个函数通过编译器内定的算法,将函数改成唯一的名称。
用v c i f l d表示void char int float long double 及其引用。
如: void func(char a); //func_c(char a)
void func(char a,int b,double c); //func_cid(char a,int b,double c)
除此之外,这种编码必须简单,而且要可逆,就是能由全局名称很快的知道局部的名称。
1.4 默认参数(默认参数顺序(从右到左)和调用顺序(从左到右))
规则:
1.默认的顺序从右到左,不能跳跃
2.默认参数调用顺序从左到右
// 默认参数 // #include "stdafx.h" #include#include using namespace std; //默认参数顺序从右到左 float volumn(float length,float weight =4,float high =5) { return length*weight*high; } int main() { //调用顺序从左到右 float v0=volumn(10); float v1=volumn(10,20); float v2=volumn(10,20,30); cout< 3.一个函数,不能既做重载,又做默认参数的函数。当你少写一个参数时,系统无法确认是重载还是默认参数。
// 默认参数 // #include "stdafx.h" #include#include using namespace std; void print(int a) { } void print(int a,int b=10) { } int main() { print(10); system("pause"); return 0; } 显示错误
1.5 引用



