C++基础
一、Hello World!
#include <iostream>
using namespace std;
int main ( )
{
cout<< "Hello World!" << endl;
return 0 ;
}
#include< iostream > 这不是C++语句,是一个预处理语句,编译器的预处理器把输出流的标准头文件包括在本程序中,所以不需要在句末加分号。 #include< > 与 #include" “区别 < >常用来包含系统提供的头文件,编译器会到保存系统标准头文件的位置查找头文件,” "常用于包括程序员自己编号的头文件,用这种格式时,编译器先查找当前目录是否有指定名称的头文件,然后从标准头目录中进行查找。 iostream与iostream.h区别 后缀为.h的头文件在C++标准已经明确提出不再支持了,.h后缀是为了区分C与C++。 using namespace std; 命名空间是为了让大量类名共存而不至于引起冲突而设计的。
二、函数
C语言是一种面向过程的编程语言,而C++却是一种面向对象的编程语言。在C++程序里,数据和对数据的处理都被封装在了一个对象里。 形参与实参的区别 形参只有被调用时才分配内存单元,在调用结束时,立即释放所分配的内存单元。形参与实参的类型应相同或赋值兼容。
int min ( int a, int b)
{
……
}
int main ( )
{
int a, b;
min ( a, b) ;
}
函数重载 C++允许函数同名,但要求不同数量参数或不同类型参数。
三、数组
定义数组 一维数组:int a[10] = {1,2,3,4,5,6,7,8,9,10} 1 2 3 4 5 6 7 8 9 10 二维数组:int b[2][2] = {1,2,3,4} 1 2 3 4 字符数组 char str[10] = “Book” strlen()与sizeof()区别: (1)strlen()是函数,在运行时才能计算。参数必须是字符型指针(char *),且必须是以’\0’结尾的。当数组名作为参数传入时,实际上数组已经退化为指针。功能:返回字符串的长度。 (2)sizeof()是运算符,在编译时就计算好了,**功能:用于计算数据空间的字节数。**sizeof不能用来返回动态分配的内存的空间大小。
四、指针
概念 指针也是一种变量,普通变量存放的是实际的数据,指针变量包含的是内存中的一块地址,这种地址指向某个变量或函数。指针的内容包括:指针的类型、指针所指向的类型、指针的值以及指针本身所占的内存区。
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
int a = 1 ;
int * pa = & a;
cout<< a<< "地址:" << pa << " 值:" << * pa<< endl;
a = 2 ;
cout<< a<< "地址:" << pa << " 值:" << * pa<< endl;
a = 3 ;
cout<< a<< "地址:" << pa << " 值:" << * pa<< endl;
}
数组与指针 (1)一维数组 *(p+i) 与 *(a+i) 等价于 a[i] (2)二维数组 输出数组第i行第j列一个元素(a[i][j]),方式: *( p[i]+j )、 *( *(p+i)+j )、( *( p+i ) )[j]、p[i][j] (3)代码
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
int a[ 5 ] = { 1 , 2 , 3 , 4 , 5 } ;
int * p1;
p1 = a;
int * p2;
p2 = & a[ 0 ] ;
cout<< "*p1 = " << * p1<< " And *p2 = " << * p2<< " So p1 = a 等价于 p2 = &a[0]" ;
}
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
int a[ 3 ] [ 4 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 } ;
int ( * p) [ 4 ] ;
p = a;
cout<< ( * p) [ 0 ] << endl;
cout<< ( * ( p+ 1 ) ) [ 0 ] << endl;
cout<< ( * ( p+ 2 ) ) [ 0 ] << endl;
cout<< ( * ( p) ) [ 5 ] << endl;
}
字符串与指针
#include <bits/stdc++.h>
using namespace std;
int main ( )
{
char str[ ] = "I am a programmer ." ;
char * str1 = "abc" ;
char * str2[ ] = { "hello world" , "good bye" } ;
string str3 = "I am a programmer too." ;
cout<< "str: " << str<< endl;
cout<< "str1: " << * ( str1+ 1 ) << endl;
cout<< "str2: " << * ( * ( str2+ 1 ) + 1 ) << endl;
cout<< "str3: " << str3<< endl;
}
函数与指针 函数指针首先是个指针变量,而且这个变量指向一个函数。 声明函数指针:返回值类型 (*指针名)(参数1,参数2·······)
#include <bits/stdc++.h>
using namespace std;
int Mmin ( int x, int y)
{
return x< y ? x: y;
}
int Mmax ( int x, int y)
{
return x> y? x: y;
}
int main ( )
{
int ( * f) ( int x, int y) ;
int a = 10 , b = 20 ;
f = Mmin;
cout<< ( * f) ( a, b) << endl;
f = Mmax;
cout<< ( * f) ( a, b) << endl;
}
五、引用
引用是什么 符号:&,在C语言中代表取地址符,在C++中代表着引用。 引用声明方法:类型 &引用名 = 目标变量名; 例子: int a; int &r = a;//定义引用r,即a的别名,a、r占用内存的同一存储单元。 特性:a变r就变,r变a就变,因此将引用作为参数,可以使形参与实参为同一存储单元,因此形参在函数中的改变会导致实参的改变。 常引用声明方法:const 类型 &引用名 = 目标变量 注:常引用不能通过引用对目标变量的值进行修改。
#include <bits/stdc++.h>
using namespace std;
#define MAXNUMS 999999
void returnfive ( int a, int & b)
{
a = 5 ;
b = 5 ;
}
int main ( )
{
int a = 1 ;
int & r = a;
cout<< a<< " " << r<< endl;
r = 2 ;
cout<< a<< " " << r<< endl;
a = 3 ;
cout<< a<< " " << r<< endl;
const int & cr = a;
cout<< a<< " " << cr<< endl;
a = 4 ;
cout<< a<< " " << r<< endl;
int x = 1 , y = 2 ;
cout<< x<< " " << y<< endl;
returnfive ( x, y) ;
cout<< x<< " " << y<< endl;
return 0 ;
}
五、引用
数据类型字节
类型/编译器 16位编译器 32位编译器 64位编译器 void 0 0 0 char 1 1 1 short int 2 2 2 int 2 4 4 float 4 4 4 double 8 8 8 long 4 4 8 long double 8 12 16 long long 8 8 8
结构体、共用体、枚举 ①结构体 struct 结构名 { 数据类型 成员名; 数据类型 成员名; ······· }; ②共用体 struct 共用体名 { 数据类型 成员名; 数据类型 成员名; ······· }; ③枚举 enum 枚举类型名 {枚举常量表列} 结构体、共用体在内存单元占用字节数计算 ①struct字节计算 全部相加,在看是否是结构体中最大类型字节数的倍数,不是的话要补充字节。 ②union字节计算 union中变量共用内存,应以最长的为准,默认内存对齐方式,必须根据最长的类型对齐。
#include <bits/stdc++.h>
using namespace std;
#define MAXNUMS 999999
struct A
{
int a;
char c;
double b;
} ;
union B
{
int a;
char c;
double b;
} ;
int main ( )
{
A a;
B b;
cout<< sizeof ( a) << " " << sizeof ( b) << endl;
}