c++学习 打卡第三天《引用》 补发

引用是标识符的别名,定义一个引用时,必须同时对它进行初始化,使它指向一个已存在的对象。符号为&,两个程序区别一下:

1:

#include <iostream>
using namespace std;
void swap(int a,int b)
{
    int t=a;
    a=b;
    b=t;
}
int main()
{
    int x=5,y=10;
    cout<<"printf the x and y"<<x<<"+"<<y<<endl;
    swap(x,y);
    cout<<"printf the x and y"<<x<<"+"<<y<<endl;
}

2:引用形式下

#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
    int t=a;
    a=b;
    b=t;
}
int main()
{
    int x=5,y=10;
    cout<<"printf the x and y"<<x<<"+"<<y<<endl;
    swap(x,y);
    cout<<"printf the x and y"<<x<<"+"<<y<<endl;
}

两个不同的形式,都是为了想实现x和y的互换,但是1中最后的值还是x=5,y=10,而2中可以实现x和y的互换。

3。带默认参数值的函数

#include<iostream>
#include<iomanip>
using namespace std;
int louvm(int length,int width=2,int height=3);//函数声明
int main()
{
    const int x=10,y=20,z=30;
    cout<<"the box data is"<<endl;
    cout<<louvm(x,y,z)<<endl;
    cout<<"the box data is"<<endl;
    cout<<louvm(x,y)<<endl;  
    cout<<"the box data is"<<endl;
    cout<<louvm(x)<<endl; 
    return 0;
}
int louvm(int length,int width,int height)
{
    cout<<setw(5)<<length<<setw(5)<<width<<setw(5)<<height<<endl;
    return height*width*length;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值