C++ References

#include<iostream>
using namespace std;

int main()
{
    int i;
    double d;

    int &r=i;
    double &s=d;

    i=5;
    cout<<"Value of i :"<<i<<endl;
    cout<<"Value of i reference : "<<r<<endl;

    d=11.7;
    cout<<"Value of d: "<<d<<endl;
    cout<<"Value of d reference : "<<s<<endl;


    return 0;
}

 References as parameters
References as Return Value:

#include<iostream>
#include<ctime>

using namespace std;

double vals[]={10.1,12.6,33.1,24.1,50.0};
double &setValues(int i){
return vals[i];
}
int main()
{
    cout<<"Value before change"<<endl;
    for(int i=0;i<5;i++){
        cout<<"vals["<<i<<"]: ";
        cout<<vals[i]<<endl;
    }

    setValues(1)=20.23;
    setValues(2)=70.8;

    cout<<"Values after changes "<<endl;
    for(int i=0;i<5;i++){

    cout<<"vals["<<i<<"]: ";
    cout<<vals[i]<<endl;

    }


    return 0;
}

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *