C++函数与值传递

简介: C++函数与值传递

一、布尔类型


#include <iostream>
using namespace std;
int main(void)
{
    bool b = false;
    //boolalpha:流控制符,以字符串形式打印bool
    cout << boolalpha << "b=" << b << endl;
    cout << "sizeof(b)=" << sizeof(b) << endl;
    b = 3+5;
    cout << "b=" << b << endl;//true
    b = 1.2*3.4;
    cout << "b=" << b << endl;//true
    char *p = NULL;//NULL-->(void*)0
    b = p;
    cout << "b=" << b << endl;//false
    return 0;
}


二、函数重载


#include <iostream>
using namespace std;
int foo(int i){
    cout << "foo(int)" << endl;
}
void foo(int i,int j){
    cout << "foo(int,int)" << endl;
}
void foo(int a,float b){
    cout << "foo(int,float)" << endl;
}
int main(void)
{
    foo(10);
    foo(10,20);
    foo(10,1.23f);
    //函数指针的类型决定其匹配的重载版本
    void (*pfoo)(int,float) = foo;
    pfoo(10,20);//foo(int,float)
    return 0;
}


#include <iostream>
using namespace std;
//char->int:升级转换
void foo(int i){
    cout << "foo(1)" << endl;
}
//char->const char:常量转换
void foo(const char c){
    cout << "foo(2)" << endl;
}
//short->char:降级转换
void fun(char c){
    cout << "fun(1)" << endl;
}
//short->int:升级转换
void fun(int i){
    cout << "fun(2)" << endl;
}
//省略号匹配
void hum(int i,...){
    cout << "hum(1)" << endl;
}
//double->int:降级转换
void hum(int i,int j){
    cout << "hum(2)" << endl;
}
int main(void)
{
    char c = 'a';
    foo(c);
    short s = 100;
    fun(s);
    hum(10,1.23);
    return 0;
}


三、函数声明和定义


#include <iostream>
using namespace std;
//函数声明
void func(int a=10,int b=20,int c=30);
//void func(int i){//注意调用时的歧义错误
//};
int main(void)
{
    func(11,22,33);//11 22 33
    func(11,22);//11 22 30
    func(11);//11 20 30
    return 0;
}
//函数定义
void func(int a/*=10*/,int b/*=20*/,int c/*=30*/){
    cout << "a=" << a << ",b=" << b << ",c="
        << c << endl;
}


四、数组


#include <iostream>
using namespace std;
int main(void)
{
/*    int* parr = new int[10];
    for(int i=0;i<10;i++){
        parr[i] = i;
        cout << parr[i] << ' ';
    }*/
    //new数组同时初始化,C++11语法支持
    int* parr =
        new int[10]{0,1,2,3,4,5,6,7,8,9};
    for(int i=0;i<10;i++){
        cout << parr[i] << ' ';
    }
    cout << endl;
    delete[] parr;
    parr = NULL;
    return 0;
}


五、值引用


#include <iostream>
using namespace std;
int main(void)
{
    int a = 60;
    int& b = a;//b引用a,b就是a的别名
   
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    cout << "&a=" << &a << endl;
    cout << "&b=" << &b << endl;
    b++;
    cout << "a=" << a << endl;//61
    cout << "b=" << b << endl;//61
    //int& r;//error
    int c = 80;
    b = c;//ok,但不是修改引用目标,仅是赋值操作
    cout << "a=" << a << endl;//80
    cout << "b=" << b << endl;//80
    cout << "&b=" << &b << endl;
    cout << "&c=" << &c << endl;
    //char& rc = c;//error
    return 0;
}
#include <iostream>
using namespace std;
int func(void){
    int num = 100;
    cout << "&num=" << &num << endl;
    return num;//int tmp = num;
}
int main(void)
{
    //int r4 = tmp;
    //int& r4 = func();
    const int& r4 = func();
    cout << r4 << endl;//100
    cout << "&r4=" << &r4 << endl;
/*
    //int& r = 100;//error
    const int& r = 100;//ok
    cout << r << endl;//100
    int a=3;
    int b=5;
    //(a+b) = 10;//error
    //a+b表达式结果是一个临时变量(右值)
    //int& r2 = a+b;//error
    const int& r2 = a+b;//r2引用就是临时变量
    cout << r2 << endl;//8
    char c = 'a';
    //首先需要将c隐式转换为int,隐式转换的结果
    //是临时变量,r3实际引用的就是临时变量
    //int& r3 = c;//error
    const int& r3 = c;//ok
    cout << r3 << endl;//97
    cout << "&c=" << (void*)&c << endl;
    cout << "&r3=" << &r3 << endl;
*/
    return 0;
}


#include <iostream>
using namespace std;
int main(void)
{
    int a = 1;
    cout << ++a << endl;//2
    cout << a << endl;//2
    ++a = 10;//ok
    cout << a << endl;//10
    ++++++a;//ok
    cout << a << endl;//13
   
    int& ra = ++a;
    cout << ra << endl;//14
    cout << (a = 10) << endl;//10
    cout << a << endl;//10
    (a = 10) = 20;
    cout << a << endl;//20
    int& ra2 = (a+=30);//ok
    cout << ra2 << endl;//50
    return 0;
}
目录
相关文章
|
3天前
|
C++ 编译器 程序员
C++ 从零基础到入门(3)—— 函数基础知识
C++ 从零基础到入门(3)—— 函数基础知识
|
3天前
|
自然语言处理 编译器 C语言
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
本文章是我对C++学习的开始,很荣幸与大家一同进步。 首先我先介绍一下C++,C++是上个世纪为了解决软件危机所创立 的一项面向对象的编程语言(OOP思想)。
36 1
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
|
3天前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
15 1
|
3天前
|
存储 算法 数据安全/隐私保护
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
7 0
|
3天前
|
存储 自然语言处理 C++
刷题用到的非常有用的函数c++(持续更新)
刷题用到的非常有用的函数c++(持续更新)
18 1
|
3天前
|
存储 编译器 C++
【C++】内存管理和模板基础(new、delete、类及函数模板)
【C++】内存管理和模板基础(new、delete、类及函数模板)
25 1
|
3天前
|
存储 C++
c/c++宏定义(函数)
c/c++宏定义(函数)
|
3天前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高
|
3天前
|
C++
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
|
3天前
|
存储 C++
C++从入门到精通:2.1.1函数和类
C++从入门到精通:2.1.1函数和类