C++概述

C++概述

 
C++ 是 C 的一个超集, C 属于C++,C++比C多类等概念 

最开始就是C语言,写着写着就诞生了很多更方便的写法,
同时也融入一些看待世界的思想,比如一切事物皆对象,
收集,整体,封装,改进,优化...打造出来就叫C++了...

如果你会C,那么看C++的时候,就会自然产生下面的一些想法:
1. 这个在C中也有...但形式上有些不一样
2. 这个是对C中的**做了改进,嗯用起来方便多了...
3. 有些功能被禁用了... 

2020发布C++20

代码示例

 
#include <iostream>  //头文件 
using namespace std; //std命名空间,加上命名空间就省去了 std.变量1 的std.,直接使用变量1就可以了 


int main()
{
    int a = 1;
    #if a==1
        cout << "is 1\n" << endl;
    #else
        cout << "条件编译:int型变量在编辑时的默认初始化值为0 \n" << endl;
    #endif
    cout << "C++!\n" << endl;
    return 0;
}

编译与执行

 
(base) xt@VM-8-13-ubuntu:/opt/tpf/cwks/C++$ g++ c01.cpp 
(base) xt@VM-8-13-ubuntu:/opt/tpf/cwks/C++$ ./a.out 
条件编译:int型变量在编辑时的默认初始化值为0 

C++!

C++数据类型

整数,浮点,布尔,字符

 
C/C++ 通用6种类型:int,char,float,double,bool,void 

类型修饰符有四个: 长短正负,long,short,unsigned,signed

signed英/saɪnd/美/saɪnd/
adj.有符号的;已签字的;有正负之分的

C++比C多一个wchar_t,用于表示超过1个字节的字符

01就不说了,计算机底层是01,任一语言底层都是01组合,
就 数据类型来说,实际上只有两种,数字与字符 
数字分了整数与浮点,字符组合出来字符串
再派生/衍生的话,就是指针,枚举,结构体等更复杂的类型了 

整数类型

 
short        2字节
int          4字节
long         4字节
long long    8字节

 
#include <iostream> 
using namespace std;

int main ()
{
    int a=1;
    short b1 =2;
    short int b2 =2;
    long c =3;
    long long d =4;
    cout << a << b1 << b2 << c <<d << endl;
    return 0;
}

$ g++ c02.cpp 
$ ./a.out 
12234

浮点类型

 
float         4字节
double        8字节
long double  16字节

字符类型

 
字符,单个的符就是字符,一个英文字母,一个汉字,一个&,都是一个字符

char a ='A';  

 
中文在操作系统中有乱码现象,所以操作中文时,要使用特定的编码 

查看linux系统已有中文字符集 
(base) [xt@ai1 web73]$ locale -a|grep zh_CN
zh_CN
zh_CN.gb18030
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8

utf8用三个字节表示一个汉字 

 
#include <iostream> 
using namespace std;

int main ()
{
    char a ='A';
    char b[4] = "我"; // 3+\0
    std::locale::global(std::locale("zh_CN.utf8"));//要选系统上有的中文字符集 

    wchar_t c=L'我'; // 用4字节存一个3字节的汉字 
    // cout<<"size:"<<sizeof(c)<<endl; //size:4

    wchar_t d[3]=L"我们"; //2+\0 
    wcout<<"c="<<c<<",d="<<d<<endl;
    return 0;
}

$ g++ c02.cpp 
$ ./a.out 
c=我,d=我们

注意:
使用wcout输出时,不要使用cout,二者不要同时使用
L是与wchar_t配合使用的 

C++数组

 
#include <iostream> 
using namespace std;

int main ()
{
    int a[]={1};
    char b[3] ="ab";

    cout << a[0] << b[1] << endl;
    return 0;
}
        
$ g++ c02.cpp 
$ ./a.out 
1b

字符串默认最后一个字符是\0 

bool 布尔,true为1,false为0

 
#include <iostream> 
using namespace std;

int main ()
{
    bool a= true;
    bool b =false;

    cout << a << b << endl;
    return 0;
}

$ g++ c02.cpp 
$ ./a.out 
10

指针类型 type*

 
#include <iostream> 
using namespace std;

int main ()
{
    bool a= true;
    bool* b = &a;

    cout << a << *b << endl;
    return 0;
}
 
$ g++ c02.cpp 
$ ./a.out 
11
        

复合类型

 
枚举,
结构体,
共用体,
类 

C++变量

变量作用范围:局部变量与全局变量

 
位于{}内的,比如方法/类的{},作用范围被限定于该{},就是局部变量

{}外的就是全局变量,作用于整个文件

当二者重名时,{}内的覆盖{}外的 

方法中的静态变量:方法调用结束,静态变量值不消失

 
#include <cstdio> 
#include <iostream> 
using namespace std;

void bb(int& b){
    static int count = 1;
    count = count + b;
    printf("count =%d\n",count);
}

int main(){
    int b=2;
    bb(b); //count =3
    bb(b); //count =5

    return 0;
}

结构体中的静态变量:所有实例共享静态变量

 
#include <cstdio> 
#include <iostream> 
using namespace std;

//声明
struct Students
{
    static int sid;
    float score;
};

//定义
int Students::sid = 2;

int main(){
    //使用
    Students::sid = 3;
    printf("sid=%d\n",Students::sid); //sid=3
    Students st1;
    printf("st1.sid=%d\n",st1.sid); //st1.sid=3
    st1.sid = 4;
    Students st2;
    printf("st2.sid=%d\n",st2.sid);//st2.sid=4

    return 0;
}

C++命名空间

 
#include <cstdio> 
#include <iostream> 
using namespace std;
namespace aa{
    //声明
    struct Students
    {
        static int sid;
        float score;
    };
}

//定义
int aa::Students::sid = 2;
int main(){
    //使用
    aa::Students::sid = 3;
    printf("sid=%d\n",aa::Students::sid); //sid=3
    return 0;
}

C++格式化输出

cstdio:使用C的printf

 
#include <cstdio>
#include <iostream> 
using namespace std;
int main(){

    printf("%d:%s\n",1,"abc");
    return 0;
}

[root@kl C++]# g++ g.cpp  
[root@kl C++]# ./a.out 
1:abc

C++引用

C++引用 &: 原变量所指内容的一个别名

 
& 在C++中表示引用,
int a =1; 会在编辑时为a在栈中分配一个4字节的内存空间p1; 
字符a是一个变量,它的值是内存地址p1指向的4字节空间中01组合出来的一个数; 

int& b =a; b引用a,并不会为b分配内存空间,
字符b也是一个变量,它直接指向变量a值的地址p1 
也就是说,一个地址对应了多个变量符号

"引用"这个词也形象,是说b本身没啥内容,它只是a的一个引用罢了 

也可以说b是a的别名,就像古人有两个名字一样,
诸葛亮,字孔明,号卧龙,不管你说孔明,还是说卧龙,大家都知道你说的是诸葛亮

引用的本质是传址,但在形式上做了简化

 
#include <cstdio>
#include <iostream> 
using namespace std;
void aa(int* a){
    *a =1;
}
void bb(int& b){
    b =1;
}
int main(){
    int a =11,b=22;
    aa(&a);
    bb(b);
    printf("a=%d,b=%d\n",a,b);
    return 0;
}

(base) [xt@kl C++]$ g++ g.cpp 
(base) [xt@kl C++]$ ./a.out 
a=1,b=1

C++引用代码示例

 
#include <iostream> 
using namespace std;

void swap(int& a, int& b)
{
    int tmp;
    tmp = a; 
    a = b;    
    b = tmp; 
    return;
}

int main ()
{
    int a = 1, b = 2;
    swap(a, b);
    cout <<"a="<< a <<",b="<< b <<  endl;
    return 0;
}
         
(base) [xt@ai1 C++]$ g++ c02.cpp 
(base) [xt@ai1 C++]$ ./a.out 
a=2,b=1

C++函数重载

 
#include <iostream> 
using namespace std;

void print(int i) {
    cout << i << endl;
}

void print(double  f) {
    cout << f << endl;
}

void print(char c[]) {
    cout << c << endl;
}

int main (int argc, char** argv)
{
    int a=1;
    double b = 2.1;
    char ss[] = "wa ka ka";
    print(a);
    print(b);
    print(ss);
    return 0;
}
        
C++模板方法

 
#include <iostream> 

template <typename T> void swap(T &a, T &b) {
    T tmp{a}; 
    a = b;
    b = tmp;
}

template<typename T, typename L> 
void print(T &a, L &l) {
    std::cout << a << "," << l << std::endl;
}

template<typename T>
inline T & get(T &a) {
    return a; 
}

template<typename T> 
inline T const& put(T const& a) {
    return a; 
}


int main(int argc, char* argv[])
{
    int a = 1; 
    int b = 2;
    swap(a, b); // 使用函数模板
    std::cout << "a=" << a << ", b=" << b << std::endl;

    double c = 2.1;
    print(a,c);

    double d = get(c);
    std::cout << d << std::endl;

    float f = 3.1;
    float f2 = put(f);
    std::cout << f2 << std::endl;

    return 0;
}
        

 
$ g++ d.cpp -std=c++11 
$ ./a.out 
a=2, b=1
2,2.1
2.1
3.1

参考文章