美文网首页
Cocos2dx之C++基础(一)

Cocos2dx之C++基础(一)

作者: 易水寒208 | 来源:发表于2017-10-17 14:58 被阅读0次

    输入输出

    std::cout << "Hello, World!" << endl;
    int x;
    std::cout << "输入x的值:";
    std::cin >> x;
    

    sizeof用法
    cout<<"int size of :"<<sizeof(int)<<endl;
    sizeof(int) 是计算int类型所占的内存空间
    结果:int size of :4

    typedef用法:

    typedef int fee;
    fee a = 10;    // 也就是 int a = 10;
    cout<<"a = "<<a<<endl; // a = 10
    cout<<sizeof(fee)<<endl; // 4
    

    typedef意思是给int从新取一个名字 fee 和 int 是一样的 也可以说fee是int的一个别名

    枚举:
    枚举的定义:

    // 第一种方法:推荐使用
    enum days{
        firstday,
        secondday,
        thirdday
    } day;
    // 第二种方法:
    enum{
        firstday,
        secondday,
        thirdday
    } day;
    // 第三种方法:
    typedef enum{
        firstday = 3,
        secondday,
        thirdday
    } Day;
    

    使用

    // 第一种
    days day = firstday;// 或 day = firstday
    if(day == firstday){
    cout<<"day == firstday"<<endl;
    }
    // 第二种
    day = firstday;
    // 第三种
    Day day = secondday;
    

    // 枚举类型不一定非要定义到函数中,
    // 枚举中参数是从0开始的,可以给指定数值,后面是从该数值上计算

        day = thirdday;
        
        switch (day) {
            case firstday:
                cout<<"firstday"<<endl;
                break;
            case secondday:
                cout<<"secondday"<<endl;
                break;
            case thirdday:{
                cout<<"thirday"<<endl;
            }
                break;
            default:
                break;
        }
    

    数据类型
    基本数据类型:整形short int, int, long int, 浮点型 float, double, char类型,bool类型。
    派生类型:枚举enum,指针*,数组[], 结构体struct,联合类型union,类class。
    空类型:void。

    字符串string

        string s1 = "Hello";
        string s2 = "World";
        string s3;
        string s4;
        size_t lens;
        
        s3 = s1;// 字符串复制到另外一个字符串上面 
        // strcpy(s3, s1); // C中使用函数strcpy
        cout<<"s3 = "<<s3<<endl;
        
        s4 = s1 + s2;// 字符串拼接 C中使用strcat(s1, s2);
        cout<<"s4 = "<<s4<<endl;
        
        lens = s1.size();// 字符串长度 C中使用的是strlen(s1);
        cout<<"lengs = "<<lens<<endl;
    
    //    判断 是不是 为空
        if (s1.empty()) {
            cout<<"s is empty"<<endl;
        }else{
            cout<<"s is not empty and s size is:"<<s1.size()<<endl;
        }
    

    结构体struct
    定义一个结构体:

    // 第一种 Person是结构体的名字 没有定义变量使用的时候定义变量
    // 推荐使用
    struct Person {
        int age;
        string name;
        bool sex;
    };
    // 结构体的使用
        Person p;
        p.age = 12;
        p.name = "zhangsan";
        p.sex = 1;
        cout<<"age = "<<p.age<<endl;
        cout<<"name = "<<p.name<<endl;
        cout<<"sex = "<<p.sex<<endl;
    // 结构体的生命也有三种形式
    // 第二种:
    struct {
        int age;
        string name;
        bool sex;
    }p;
    // p是结构体的变量可以直接使用
    p.age = 32;
    cout<<"age = "<<p.age<<endl;
    // 第三种:
    typedef struct Person {
        int age;
        string name;
        bool sex;
    }p;
    // p是结构体体Person的别名 定义变量的时候都可以使用
        Person p;
        p.age = 12;
        
        person p1;
        p1.age = 32;
    

    // 结构体可以作为函数的参数进行传递,

    联合内行union
    联合类型类似结构体,但是跟结构体不同的是:联合类型运行不同的数据类型访问相同的内存,因为这些数据变量在内存中的同一位置,联合类型的大小是这些数据变量中数据类型最大的那个。

    union Student {
        const char * name;
        int age;
        float score;
    }s;
    
        s.age = 10;
        cout<<"name = "<<s.age<<endl;
        cout<<"score = "<<s.score<<endl;
    

    宏定义
    宏定义一般变量都要大写
    宏定义的意义就是替换
    宏定义后面不能跟冒号

    #define PI 3.14  // 一般宏定义
    #define MIN(a,b) (a<b?a:b) // 带参数的宏定义
    // 判断宏定义 如果没有定义某个宏定义就定义某个宏定义
    #ifndef DEBUG
        #define DEBUG
    #endif
    // # 会把令牌转换为双引号引用的字符串
     #define MKSTR(x) #x
    //  等价于 #define MKSTR(x) "x"
    // ## 用于链接两个令牌
    #define CONCAT(a, b)  a##b
    // 等价于 // #define CONCAT(a,b) ab
    
        // 一些 预定义 宏
        cout << "Value of __LINE__ : " << __LINE__ << endl;// 该语句位于哪一行
        cout << "Value of __FILE__ : " << __FILE__ << endl;// 当前文件的路径位置
        cout << "Value of __DATE__ : " << __DATE__ << endl;// 当前日期
        cout << "Value of __TIME__ : " << __TIME__ << endl;// 当前时间
    

    相关文章

      网友评论

          本文标题:Cocos2dx之C++基础(一)

          本文链接:https://www.haomeiwen.com/subject/mdaruxtx.html