Androider学C/C++—(2)进入c++的世界,hello,数据类型,变量,常量

还是helloworld

#include<iostream> //c++的头文件引入
using namespace std;    //命名空间 cout函数依赖于这个命名空间
int main()
{
    cout << "    WTF      " << endl;  //在此填写我们的开篇Hello 
    system("pause");//系统函数为了不让黑窗口一闪而过
    return 0;
}
  • include iostream
    C++ 语言定义了一些头文件,这些头文件包含了程序中必需的或有用的信息。上面这段程序中,包含了头文件 。
  • using namespace std //命名空间
    告诉编译器使用 std 命名空间。命名空间是 C++ 中一个相对新的概念。
  • main()
    int main() 是主函数,程序从这里开始执行
  • cout << ” WTF ” << endl; //输出WTF 可以用 “\n” 代替以上代码里的 endl。
    会在屏幕上显示消息 “Hello World”。
    cout是c++的printf函数,” WTF “是字符串,<< 流操作符,endl:”\n” 表示内容为一个回车符的字符串。std::endl 是流操作子,输出的作用和输出 “\n” 类似,但可能略有区别。std::endl 输出一个换行符,并立即刷新缓冲区。

C++数据类型

  • 布尔型 bool
  • 字符型 char
  • 整型 int
  • 浮点型 float
  • 双浮点型 double
  • 无类型 void
  • 宽字符型 wchar_t

具体字节和修饰符的参考:http://www.runoob.com/cplusplus/cpp-data-types.html

利用sizeof函数打印字节数
本实例使用了 endl,这将在每一行后插入一个换行符,<< 运算符用于向屏幕传多个值。


    #include<iostream>  
    #include<string>  
    #include <limits>  
    using namespace std;  

    int main()  
    {  
        cout << "type: \t\t" << "************size**************"<< endl;  
        cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);  
        cout << "\t最大值:" << (numeric_limits<bool>::max)();  
        cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;  
        cout << "char: \t\t" << "所占字节数:" << sizeof(char);  
        cout << "\t最大值:" << (numeric_limits<char>::max)();  
        cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;  
        cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);  
        cout << "\t最大值:" << (numeric_limits<signed char>::max)();  
        cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;  
        cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);  
        cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();  
        cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;  
        cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);  
        cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();  
        cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;  
        cout << "short: \t\t" << "所占字节数:" << sizeof(short);  
        cout << "\t最大值:" << (numeric_limits<short>::max)();  
        cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;  
        cout << "int: \t\t" << "所占字节数:" << sizeof(int);  
        cout << "\t最大值:" << (numeric_limits<int>::max)();  
        cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;  
        cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);  
        cout << "\t最大值:" << (numeric_limits<unsigned>::max)();  
        cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;  
        cout << "long: \t\t" << "所占字节数:" << sizeof(long);  
        cout << "\t最大值:" << (numeric_limits<long>::max)();  
        cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;  
        cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);  
        cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();  
        cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;  
        cout << "double: \t" << "所占字节数:" << sizeof(double);  
        cout << "\t最大值:" << (numeric_limits<double>::max)();  
        cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;  
        cout << "long double: \t" << "所占字节数:" << sizeof(long double);  
        cout << "\t最大值:" << (numeric_limits<long double>::max)();  
        cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;  
        cout << "float: \t\t" << "所占字节数:" << sizeof(float);  
        cout << "\t最大值:" << (numeric_limits<float>::max)();  
        cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;  
        cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);  
        cout << "\t最大值:" << (numeric_limits<size_t>::max)();  
        cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;  
        cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;  
        // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
        cout << "type: \t\t" << "************size**************"<< endl;  
        getchar();
        return 0;  
    }

输出:

    type:         ************size**************
    bool:         所占字节数:1    最大值:1        最小值:0
    char:         所占字节数:1    最大值:        最小值:?
    signed char:     所占字节数:1    最大值:        最小值:?
    unsigned char:     所占字节数:1    最大值:?        最小值:
    wchar_t:     所占字节数:4    最大值:2147483647        最小值:-2147483648
    short:         所占字节数:2    最大值:32767        最小值:-32768
    int:         所占字节数:4    最大值:2147483647    最小值:-2147483648
    unsigned:     所占字节数:4    最大值:4294967295    最小值:0
    long:         所占字节数:8    最大值:9223372036854775807    最小值:-9223372036854775808
    unsigned long:     所占字节数:8    最大值:18446744073709551615    最小值:0
    double:     所占字节数:8    最大值:1.79769e+308    最小值:2.22507e-308
    long double:     所占字节数:16    最大值:1.18973e+4932    最小值:3.3621e-4932
    float:         所占字节数:4    最大值:3.40282e+38    最小值:1.17549e-38
    size_t:     所占字节数:8    最大值:18446744073709551615    最小值:0
    string:     所占字节数:24
    type:         ************size**************

typedef声明

typedef int feet;

下面的语句会告诉编译器,feet 是 int 的另一个名称:下面的声明是完全合法的,它创建了一个整型变量 distance:

#include <iostream>
using namespace std;//cout函数依赖于这个std命名空间

int main() {
    typedef int feet;
    feet distance = 666;
    cout << "distance == " << distance;
    getchar();
    return 0;
}

输出

distance == 666

枚举类型

  • 枚举类型(enumeration)
  • 它是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。
  • 如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。
  • 所谓”枚举”是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。【参照JAVA枚举】
  • 创建枚举,需要使用关键字 enum。
    #include <iostream>
    using namespace std;

    void main() {
        //写法跟java差不多
        //这个枚举自带int指,你不设它默认从0开始,后面递增1
        //如果你给他设了,它则意这个值为其实,后面元素也递增1

        enum color { red, green, blue } ;

        color c;

        c = green;

        cout << "WTF : c = " << c << "bule = " << blue << endl;

        enum color2 { white, yellow = 5, black } test;
        test = yellow;

        cout << "WTF : test yellow = " << test << "black = " << black << endl;

        system("pause");

    }

输出

    WTF : c = 1bule = 2
    WTF : test yellow = 5black = 6
    请按任意键继续. . .
  • 写法跟java差不多
  • 这个枚举自带int指,你不设它默认从0开始,后面递增1
  • 如果你给他设了,它则意这个值为其实,后面元素也递增1
  • 定义的时候枚举名不能一样,这个要注意

C++变量常量(和Java一样)

局部变量

    #include <iostream>
    using namespace std;

    int main ()
    {
      // 局部变量声明
      int a, b;
      int c;

      // 实际初始化
      a = 10;
      b = 20;
      c = a + b;

      cout << c;

      return 0;
    }

全局变量

g是全局变量。局部变量可以和全局变量名字一样,但是这样一来,全局变量的值就被覆盖了。这点和java一样。


    #include <iostream>
    using namespace std;

    // 全局变量声明
    int g;

    int main ()
    {
      // 局部变量声明
      int a, b;

      // 实际初始化
      a = 10;
      b = 20;
      g = a + b;

      cout << g;

      return 0;
    }

变量初始值

int => 0
char => ‘\0’
float => 0
double => 0
pointer => NULL

常量

使用#define


    #include <iostream>
    using namespace std;

    // 在C++ 中,有两种简单的定义常量的方式
    // 使用 #define 预处理器。
    // 使用 const 关键字。

    #define LENGTH 10   
    #define WIDTH  5
    #define NEWLINE '\n'
    #define HELLO "HELLO"

    void main() {

        int area;

        area = LENGTH * WIDTH;
        cout << area;
        cout << NEWLINE;
        cout << HELLO;

        getchar();
    }

输出结果


    为何输出是这样?换行符没起作用呢
    50
    HELLO

    我期望是这样
    50

    HELLO

    但是本文重点不在这里,先存疑。
    群里问了问,貌似是因为:其实已经换行了,但是我这个输出的字符串本来该在第一行。
    先TODO,如果知道原理的朋友请不吝赐教。

使用Const关键字


    #include <iostream>
    using namespace std;

    int main()
    {
        const int  LENGTH = 10;
        const int  WIDTH = 5;
        const char NEWLINE = '\n';
        const char* Hello = "Hehe";
        int area;

        area = LENGTH * WIDTH;
        cout << area;
        cout << NEWLINE;
        cout << Hello;


        getchar();
        return 0;
    }

Demo

Demo:https://github.com/zj614android/c-c-/tree/master/Lis2
推荐:c++入门视频:https://www.imooc.com/u/1349694/courses?sort=publish
推荐:菜鸟教程:http://www.runoob.com/cplusplus/cpp-tutorial.html

猜你喜欢

转载自blog.csdn.net/user11223344abc/article/details/80510794