当前位置:   article > 正文

C++程序设计【一】之 C++ 语言简介_int &ref = oneint

int &ref = oneint

第一章:C++ 语言简介

一、C++ 语言的发展简史

在这里插入图片描述

二、C++ 语言的特点

在这里插入图片描述

1.与C语言相比,C++语言的优点

在这里插入图片描述

2.基本的输入 / 输出

在这里插入图片描述
在这里插入图片描述

额外记住的知识点:
a 对应的 ASC码 97
A 对应的 ASC码 65

3.头文件和命名空间

在这里插入图片描述
在这里插入图片描述

4.强制类型转换运算符

在这里插入图片描述
在这里插入图片描述

5.函数参数的默认值

在这里插入图片描述

6.引用和函数参数的传递

在这里插入图片描述

#include <iostream>

using namespace std;
int main() {
    int oneInt = 1;
    int &ref = oneInt;  // ref是oneInt的引用,ref等价于oneInt
    const int &refc = oneInt;   // 定义常引用
    ref = 2;    // 修改ref也相当于修改了oneInt和refc;
    cout << "oneInt=" << oneInt << "," << "ref=" << ref << endl;    // 输出oneInt=2,ref=2
    cout << "refc=" << refc << endl;    // 输出refc=2
    ref = 4;    // 修改ref也相当于修改了oneInt和refc;
    cout << "oneInt=" << oneInt << "," << "ref=" << ref << endl;    // 输出oneInt=4,ref=4
    cout << "refc=" << refc << endl;    // 输出refc=4

    oneInt = 3; // 修改oneInt也相当于修改了ref和refc
    cout << "oneInt=" << oneInt << "," << "ref=" << ref << endl;  // 输出oneInt=3,ref=3
    cout << "refc=" << refc << endl;    // 输出refc=3

    int &ref2 = ref;    // ref2和ref都是oneInt的引用
    cout << "ref2=" << ref2 << endl;    // 输出ref2=3

    // refc = 5;    // 错误,不能使用const定义的常引用对所引用的变量进行更改
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

#include <iostream>

using namespace std;
void SwapValue(int a, int b) {
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    cout << "在SwapValue()函数中:\t\ta=" << a << ",b=" << b << endl;
}
void SwapRef(int & a, int & b) {    // a、b值互换
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    cout << "在SwapRef()函数中:\t\ta=" << a << ",b=" << b << endl;
}
int main() {
    int a = 10, b = 20;
    cout << "数据交换前:\t\ta=" << a << ",b=" << b << endl;
    SwapValue(a, b);
    cout << "调用SwapValue后::\t\ta=" << a << ",b=" << b << endl;

    SwapRef(a, b);
    cout << "调用SwapRef后::\t\ta=" << a << ",b=" << b << endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在这里插入图片描述

7.const 与指针共同使用

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

简单易懂的规则:
例子:const char * const p = “ABC”;
*号左侧的const修饰所指的数据
*号右侧的const修饰指针
俩侧都有就是全都修饰。相当于全是常量不可变

8.内联函数

在这里插入图片描述
在这里插入图片描述

9.函数的重载

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

10.指针和动态内存分配

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

11.用string对象处理字符串

在这里插入图片描述
求数组长度:

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

int main() {
    string citys[] = {"a", "bb", "ccc", "dddd"};
    cout << "cityArr: " << sizeof(citys) / sizeof(string) << endl; // 求数组长度
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

三、C++ 语言的程序结构

在这里插入图片描述

下一篇:C++程序设计【二】之面向对象的基本概念

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/917556
推荐阅读
相关标签
  

闽ICP备14008679号