zoukankan      html  css  js  c++  java
  • C++基础

    一、C++简介

    C++ 完全支持面向对象的程序设计,包括面向对象开发的四大特性:封装、抽象、继承、多态

    二、C++基本语法

    基本名词有对象、类、方法、即时变量
    输出hello world:

    #include <iostream>
    using namespace std;
     
    // main() 是程序开始执行的地方
     
    int main()
    {
       cout << "Hello World"; // 输出 Hello World
       cout << "Hello, world!" << endl; // 输出 Hello World并换行
       cout << "Hello, world!" << "\n"; // 输出 Hello World并换行
    }
    

    解析:

    • using namespace std;告诉编译器使用std命名空间

    1.C++标识符

    以字母、下划线开始,后接字母、下划线、数字

    2.C++注释

    // 单行注释
    /* */ 多行注释

    3.数据类型

    • 基本内置类型
      | 类型 | 关键字 |
      | ------- | ------ |
      | 布尔型 | bool |
      | 字符型 | char |
      | 整型 | int |
      | 浮点型 | float |
      | 双浮点型 | double |
      | 无类型 | void |
      | 宽字符型 | wchar_t |
    • typedef声明
      为一个已有的数据类型取一个新的名字

    例如:下面的语句会告诉编辑器,feet 是 int 的另一个名称,并且用这个新名字创建了一个整形变量

    typedef int feet;
    feet distance;
    
    • 枚举类型
      如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。所谓"枚举"是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内

    创建枚举,需要使用关键字 enum。枚举类型的一般形式为:

    enum 枚举名{ 
         标识符[=整型常数], 
         标识符[=整型常数], 
    ... 
        标识符[=整型常数]
    } 枚举变量;
    

    如果枚举没有初始化, 即省掉"=整型常数"时, 则从第一个标识符开始。

    例如,下面的代码定义了一个颜色枚举,变量 c 的类型为 color。最后,c 被赋值为 "blue"。

    enum color { red, green, blue } c;
    c = blue;
    

    默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,以此类推。但是,您也可以给名称赋予一个特殊的值,只需要添加一个初始值即可。例如,在下面的枚举中,green 的值为 5。

    enum color { red, green=5, blue };
    

    在这里,blue 的值为 6,因为默认情况下,每个名称都会比它前面一个名称大 1,但 red 的值依然为 0

    4.定义常量

    在 C++ 中,有两种简单的定义常量的方式:

    • 使用 #define 预处理器
      例如:
    #include <iostream>
    using namespace std;
     
    #define LENGTH 10   
    #define WIDTH  5
    #define NEWLINE '\n'
     
    int main()
    {
     
       int area;  
       
       area = LENGTH * WIDTH;
       cout << area;
       cout << NEWLINE;
       return 0;
    }
    
    • 使用 const 关键字
      例如:
    #include <iostream>
    using namespace std;
     
    int main()
    {
       const int  LENGTH = 10;
       const int  WIDTH  = 5;
       const char NEWLINE = '\n';
       int area;  
       
       area = LENGTH * WIDTH;
       cout << area;
       cout << NEWLINE;
       return 0;
    }
    

    5.C++修饰符类型

    C++ 允许在 char、int 和 double 数据类型前放置修饰符。修饰符用于改变基本类型的含义,所以它更能满足各种情境的需求。

    常见数据类型修饰符:signed、unsigned、long、short

    6.C++存储类

    存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期

    常见存储类:auto、register、static、extern、mutable

    三、C++函数

    实例:

    #include <iostream>
    using namespace std;
     
    // 函数声明
    int max(int num1, int num2);
    
    // 主函数
    int main ()
    {
       
       int a = 100;
       int b = 200;
       int ret;
     
       
       ret = max(a, b);
     
       cout << "Max value is : " << ret << endl;
     
       return 0;
    }
     
    // 函数定义:函数返回两个数中较大的那个数
    int max(int num1, int num2) 
    {
       
       int result;
     
       if (num1 > num2)
          result = num1;
       else
          result = num2;
     
       return result; 
    }
    

    四、C++数组

    实例:

    #include <iostream>
    using namespace std;
     
    #include <iomanip>
    using std::setw;
     
    int main ()
    {
       // 声明数组
       int n[ 10 ]; // n 是一个包含 10 个整数的数组
     
       // 初始化数组元素          
       for ( int i = 0; i < 10; i++ )
       {
          n[ i ] = i + 100; // 设置元素 i 为 i + 100
       }
       cout << "Element" << setw( 13 ) << "Value" << endl;
     
       // 访问数组元素:输出数组中每个元素的值                     
       for ( int j = 0; j < 10; j++ )
       {
          cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
       }
     
       return 0;
    }
    

    五、C++字符串

    1.C风格字符串

    用一维数组表示,末尾以\0结束

    char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
    

    依据数组初始化规则,您可以把上面的语句写成以下语句:

    char site[] = "RUNOOB";
    
    • 常见操作字符串的函数:
      1.strcpy(s1, s2):复制字符串 s2 到字符串 s1
      2.strcat(s1, s2):连接字符串 s2 到字符串 s1 的末尾。连接字符串也可以用 + 号
      3.strlen(s1):返回字符串 s1 的长度
      4.strcmp(s1, s2);如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回值小于 0;如果 s1>s2 则返回值大于 0
      5.strchr(s1, ch);返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置
      6.strstr(s1, s2);返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置

    2.C++中的字符串

    C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能

    #include <iostream>
    #include <string>
     
    using namespace std;
     
    int main ()
    {
       string str1 = "runoob";
       string str2 = "google";
       string str3;
       int  len ;
     
       // 复制 str1 到 str3
       str3 = str1;
       cout << "str3 : " << str3 << endl;
     
       // 连接 str1 和 str2
       str3 = str1 + str2;
       cout << "str1 + str2 : " << str3 << endl;
     
       // 连接后,str3 的总长度
       len = str3.size();
       cout << "str3.size() :  " << len << endl;
     
       return 0;
    }
    

    六、C++日期 & 时间

    1. 概述

    C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间操作的结构和函数

    为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 头文件

    2. 时间函数

    时间数据类型有:clock_t、time_t、size_t 和 tm

    类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数

    结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:

    struct tm {
      int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
      int tm_min;   // 分,范围从 0 到 59
      int tm_hour;  // 小时,范围从 0 到 23
      int tm_mday;  // 一月中的第几天,范围从 1 到 31
      int tm_mon;   // 月,范围从 0 到 11
      int tm_year;  // 自 1900 年起的年数
      int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起
      int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
      int tm_isdst; // 夏令时
    };
    

    列举几个 C/C++ 中关于日期和时间的重要函数:

    1.time_t time(time_t *time)
    该函数返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 -1
    
    2.char *ctime(const time_t *time)
    该返回一个表示当地时间的字符串指针
    字符串形式 day month year hours:minutes:seconds year\n\0
    
    3.char * asctime ( const struct tm * time )
    该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息
    返回形式为:day month date hours:minutes:seconds year\n\0
    
    4.struct tm *gmtime(const time_t *time)
    该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示
    
    5.struct tm *localtime(const time_t *time)
    该函数返回一个指向表示本地时间的 tm 结构的指针
    

    实例:
    当前系统的日期和时间,包括本地时间和协调世界时(UTC)

    #include <iostream>
    #include <ctime>
     
    using namespace std;
     
    int main( )
    {
       // 基于当前系统的当前日期/时间
       time_t now = time(0);
       
       // 把 now 转换为字符串形式
       char* dt = ctime(&now);
     
       cout << "本地日期和时间:" << dt << endl;
     
       // 把 now 转换为 tm 结构
       tm *gmtm = gmtime(&now);
       dt = asctime(gmtm);
       cout << "UTC 日期和时间:"<< dt << endl;
    }
    

    执行结果:

    本地日期和时间:Sat Jan  8 20:07:41 2011
    
    UTC 日期和时间:Sun Jan  9 03:07:41 2011
    

    3. 使用tm结构格式化时间

    实例:

    #include <iostream>
    #include <ctime>
     
    using namespace std;
     
    int main( )
    {
       // 基于当前系统的当前日期/时间
       time_t now = time(0);
     
       cout << "1970 到目前经过秒数:" << now << endl;
     
       tm *ltm = localtime(&now);
     
       // 输出 tm 结构的各个组成部分
       cout << "年: "<< 1900 + ltm->tm_year << endl;
       cout << "月: "<< 1 + ltm->tm_mon<< endl;
       cout << "日: "<<  ltm->tm_mday << endl;
       cout << "时间: "<< ltm->tm_hour << ":";
       cout << ltm->tm_min << ":";
       cout << ltm->tm_sec << endl;
    }
    

    输出:

    1970 到目前时间:1503564157
    年: 2017
    月: 8
    日: 24
    时间: 16:42:37
    

    七、C++ 基本的输入输出

    1. 头文件

    该文件定义了 cin、cout、cerr 和 clog 对象 分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流

    2. 标准输出流 cout

    预定义的对象 cout 是 iostream 类的一个实例。cout 对象"连接"到标准输出设备,通常是显示屏。cout 是与流插入运算符 << 结合使用的

    实例:

    #include <iostream>
     
    using namespace std;
     
    int main( )
    {
       char str[] = "Hello C++";
     
       cout << "Value of str is : " << str << endl;
    }
    

    当上面的代码被编译和执行时,它会产生下列结果:

    Value of str is : Hello C++

    3. 标准输入流 cin

    预定义的对象 cin 是 iostream 类的一个实例。cin 对象附属到标准输入设备,通常是键盘。cin 是与流提取运算符 >> 结合使用的

    实例:

    #include <iostream>
     
    using namespace std;
     
    int main( )
    {
       char name[50];
     
       cout << "请输入您的名称: ";
       cin >> name;
       cout << "您的名称是: " << name << endl;
     
    }
    

    当上面的代码被编译和执行时,它会提示用户输入名称。当用户输入一个值,并按回车键,就会看到下列结果:

    请输入您的名称: cplusplus
    您的名称是: cplusplus

  • 相关阅读:
    经常出现null错误之tostring
    删除sql计划 调用的目标发生了异常。 (mscorlib) 其他信息: 用户 'sa' 登录失败。
    js打印保存用户输入的内容
    vs调试有时能进去后台,有时不能进去
    vs当前不会命中断点,还没有为该文档加载任何符号
    动软数据库文档生成器 失败错误码HRESULT:0x80010105 解决办法
    关于ckeditor过滤掉html样式标签之我见
    快速批量插入sqlserver方法之我见
    vs找不到svn源代码管理插件之我见
    有关大学,有关爱好,有关学习,有关奋斗,有关理想:大学应该干些什么?我大学三年以来的感悟
  • 原文地址:https://www.cnblogs.com/wy0526/p/15650619.html
Copyright © 2011-2022 走看看