一、C语言概述
1、TIOBE语言排行榜:java、C、C++、
2、C和C++都是强类型语言,但是C++更强
3、C++去除了C的不好特性,增加了很好的特性,包括支持面向对象,操作符重载,异常处理,泛型编程……
4、C++比C更适合大型软件的开发
二、第一个C++程序
1、编译方式:
链接时 +l 链接库的名字
g++ 1.cpp
gcc –x c++ 1.c –lstdc++
2、文件扩展名
.cpp / .cc / .C .cxx
3、#include<iostream> 标准输入输出库函数
#include<cstdio>//等价stdio.h
4、输入和输出
用cin对象表示标准输入,用cout对象表示标准输出
cout << a <<endl ; // 把数据a插入到标准输出设备
等价于 pring(“a\n”,a);
<< :插入运算符
例:cout << a << “hell”<<’A’<<endl;
等价于:printf(“%d%s%c\n”,a,”hello” ,’A’) ;
>>:提取运算符
cin>>a ;//从标准输入设备提取一个整形数放到a中
等价于 scanf(“%d”,&a);
5、std标准名字空间
1 #include<iostream>
2 #include<cstdio>//==等价stdio.h
3 //编译方式g++ xx.cpp
4 int main(void){
5 std::cout<<"helloworld!"<<std::endl;
6 printf("hello\n");
7 int a=10;
8 double d=3.14;
9 std::cout << a << ',' <<d <<std::endl;
10 printf("%d,%g\n",a,d);
11 std::cout<< "please input ainteger:";
12 std::cin>>a;
13 std::cout<<a<<std::endl;
14 return 0;
15 }
hello world!
hello
10,3.14
10,3.14
please input a integer:1
1
三、名字空间
1、定义名字空间
namespace 名字空间名{
名字空间成员1;
名字空间成员1;
}
名字空间成员可以是:全局变量、函数、类型、名字空间
Namespace ns1{
int a;
void foo(void ){ }
struct student { }; //结构体
namespace ns2{ }
}
2、名字空间使用
1)通过作用域限定运算符“::”
名字空间名::要访问的成员
例:
1 #include<iostream>
2 namespace ns1{
3 void func(void){
4 std::cout<<"ns1::func"<<std::endl;
5 }
6 }
7 namespace ns2{
8 void func(void){
9 std::cout<<"ns2::func"<<std::endl;
10 }
11 }
12 int main(void){
13 //func();名字空间成员不能直接使用
14 ns1::func();//调用ns1名字空间中的func
15 ns2::func();//调用ns2名字空间中的func
16 return 0;
17 }
tarena@tarena-virtual-machine:~/day36$ g++ 2.cpp
tarena@tarena-virtual-machine:~/day36$ ./a.out
ns1::func
ns2::func
tarena@tarena-virtual-machin
2)名字空间指令
Using namespace 名字空间名:
该条指令以后的代码,指明名字空间中的成员可见,访问其中的成员可以省略作用域限定,直接访问。
例:int main(void ){
a=100;//error
Using namespace ns 1;
a=100;
foo( );
}
1 #include<iostream>
2 using namespace std;//代码中可以省略std::
3 namespace ns1{
4 void func(void){
5 cout<<"ns1::func"<<endl;
6 }
7 }
8 namespace ns2{
9 void func(void){
10 cout<<"ns2::func"<<endl;
11 }
12 }
13 int main(void){
14 using namespace ns1;//名字空间指令
15 func();//调用ns1名字空间中的func
16 using namespace ns2;//名字空间指令
17 func();//引起歧义
18 return 0;
19 }
3)名字空间声明
using 名字空间名::名字空间成员;
将名字空间特定成员引入当前作用域,在该作用域中访问这个成员同访问自己的成员一样,可以省略作用域限定,直接访问
int main(void ){
using ns 1::a;
a=100;
foo( );
}
1 #include<iostream>
2 using namespace std;//代码中可以省略std::
3 namespace ns1{
4 void func(void){
5 cout<<"ns1::func"<<endl;
6 } 4
7 }
8 namespace ns2{
9 void func(void){
10 cout<<"ns2::func"<<endl;
11 }
12 }
13 int main(void){
14 using ns1::func;//名字空间声明
15 func();
16 using namespace ns2;//名字空间指令
17 func();//局部优先,仍然访问ns1中的func()
18 ns2::func();
19 return 0;
20 }
ns1::func
ns1::func
ns2::func
无名名字空间
不属于任何名字空间的标示符,将被编译器自动放到无名名字空间中
1 #include<iostream>
2 using namespace std;//代码中可以省略std::
3 namespace ns1{
4 void func(void){
5 int num=10;
6 cout<<"ns1::func"<<endl;
7 }
8 }
9 namespace ns2{
10 void func(void){
11 cout<<"ns2::func"<<endl;
12 }
13 }
14 int num=20;//无名名字空间成员
15 int main(void){
16 using ns1::func;//名字空间声明
17 func();
18 using namespace ns2;//名字空间指令
19 func();//局部优先,仍然访问ns1中的func()
20 ns2::fuinc();
21 using na1::num;
22 cout<<num<<endl; //10
23 cout<<::num<<endl;//访问无名名字空间成员
24 return 0;
25 }
4)名字空间嵌套
1 #include<iostream>
2 using namespace std;//代码中可以省略std::
3 namespace ns1{
4 void func(void){
5 int num=10;
6 cout<<"ns1::func"<<endl;
7 }
8 }
9 namespace ns2{
10 void func(void){
15 int main(void){
16 using ns1::func;//名字空间声明
17 func();
18 using namespace ns2;//名字空间指令
例
namespace ns2{
int x=20;
namespace ns3{
int x=30;
}
}
};
int main(void){
cout<<ns1::x<<endl;//10
cout<<ns1::ns2::x<<endl;//20
cout <<ns1::ns2::ns3::x<<endl;//30
}
四、C++的结构体、联合体、枚举
1、c++结构体
1)定义结构体变量时可以省略struct关键字
2)C++结构体里面可以直接定义函数,称为成员函数,在成员函数可以直接访问成员变量
Struct student{//声明
Char name[20];
Int age;
}
Int main(void){
/*Struct*/ student s={“tangzihao”,24};
}
例:
1 #include<iostream>
2 using namespace std;
3 struct student{
4 //成员变量
5 char name[100];
6 int age;
7 //成员函数
8 void who(void){
9 cout<<"我叫"<<name<<"今年"<<
10 age<<"岁"<<endl;
11 }
12 };
13 int main(void){
14 /*struct*/student s={"张飞",28};
15 s.who();
16 return 0;
17 }
2、联合体(了解)
1)定义联合体变量可以省略union
2)支持匿名联合
1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4 int main(void){
5 union{//匿名联合
6 unsigned int un;
7 unsigned char uc[4];
8 };
9 int i;
10 un=0x12345678;//12是高地址,78是低地址
11 for(i=0;i<4;i++){
12 printf("%#x",uc[i]);//uc[0]是低位
13 }
14 cout<<endl;
15 return 0;
16 }
tarena@tarena-virtual-machine:~/day36$ ./a.out
0x780x560x340x12 //小端
3)枚举
定义枚举变量可以省略enum关键字
C++的枚举是一个独立的数据类型,C语言中枚举是整型变量
enum COLOR{RED,GEEEN,BLUE};
enum COLOR c;
c=100; // C中可以,C++错误
例:
1 #include<iostream>
2 using namespace std;
3 int main(){
4 enum color{
5 RED,//0
6 GREEN,//1
7 BLUE=100,//100
8 YELLOW,//101
9 };
10 cout<<RED<<''<<GREEN<<' '
11 <<BLUE<<''<<YELLOW<<endl;
12 /*enum*/color c;//定义枚举变量
13 //c=100;//C :ok C++:error
14 c=RED;
15 cout<<c<<endl;//0
16 return 0;
17 }
六、C++字符串(重点)
1、C中字符串
1)双引号字面值“helloworld” 代码段
2)字符指针 char *
const char *p=”hello world”;
char *p; p=”tangzihao”; //OK
char * const p=”hello world”;
char *p; p=”tangzihao”;//error;
3) 字符数组
char arr[12 ]=“hello world”; //等价于char *const arr
易错点:strcpy(arr,”tangzihao”); //不安全,数组越界有风险
char *p; p=”tangzihao”; //OK
strcpy (p,arr); //段错误
arr=p; //编译报错,arr指向不能改
2、C++字符串兼容C字符串,增加了string类型,专门表示字符串
string s=”hello”; //定义字符串
s+=”world”; //字符串连接 不会溢出,不会段错误,不会越界
cout<< s <<endl; // “hello world”
s=”tangzihao”; //字符串拷贝
if(s= =”tangzihao”){
cout <<”ture”<<endl;
}
else{
Cout<<”false”<<endl;
}
例:
1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 int main(void){
5 string s="hello";
6 cout<<s<<endl;
7 //字符串连接
8 s+=" world!";
9 cout<<s<<endl;
10
11 string s2;//定义一个空字符串""
12 s2=s;//字符串的拷贝
13 cout<<s2<<endl;
14
15 string s3="tangzihao";
16 swap(s2,s3);//字符串的交换
17 cout<<"s2="<<s2<<endl;//tangzihao
18 cout<<"s3="<<s3<<endl;//hello world!
19
20 //取出字符串中某个字符
21 cout<<s2[2]<<''<<s2[3]<<endl;//n g
22
23 cout<<(s==s2)<<endl;//0假
24 cout<<(s==s3)<<endl;//1真
25 cout<<(s>s2)<<endl;//0假
26
27 cout<<s.length()<<endl;//12
28
29 //sting-->char*,转化成C风格的字符串
30 cout<<strlen(s.c_str())<<endl;
31 return 0;
32 }
hello
hello world!
hello world!
s2=tangzihao
s3=hello world!
n g
0
1
0
12
12
七、C++的bool类型
1、bool类型是C++中的基本数据类型,专门表示逻辑值
真(true)、假(false)
2、bool在内存占一个字节:1表示ture,0表示false
3、bool类型变量可以接收任何类型的变量或表达式,其值非零则为ture,值为0(NULL)则为false
NULL--->(void *)0;
例:
1 #include<iostream>
2 using namespace std;
3 int main(void){
4 bool b=false;//0
5 cout<<sizeof(b)<<endl;//1 大小是1个字节
6 //插入流ZZ控制符,用字符串形式打印bool变量值
7 cout<<boolalpha<<b<<endl; //false
8 b=100;
9 cout<<b<<endl;//ture
10 b=3.14;
11 cout<<b<<endl;//true
12 b="hello world";
13 cout<<b<<endl;//true
14 char *p=NULL;
15 b=p;
16 cout<<b<<endl;//false
17 return 0;
18 }
八、C++字符串的运算符(了解)
&& ==== and
|| ==== or
^ ==== xor
{ ==== <%
} ===== %> ……..