Reference Variables and Typecasting in C++

 #include<iostream>

using namespace std;

int c = 45;
int main()
{
    // *****************Build in Data types******************
    int a, b, c;
    cout<<"Enter the value of a : "<<endl;
    cin>>a;
    cout<<"Enter the value of b : "<<endl;
    cin>>b;
    c = a + b;
    cout<<"The Sum is : "<<c<<endl;
    //  To take the value of Global variable, scope resolution operator :: is used
    //  5as shown below :
    cout<<"The Global value is : "<<::c<<endl;

    //************ Float, double and long double Literals ****************
    float d = 34.4;
    long double e = 34.4;
    cout<<"The value of d is : "<<d<<endl<<"The value of e is : "<<e<<endl;
    cout<<endl;

    // By default 34.4 is considered as double but if we denote as 34.4f then only it
    //will considered as float value
    cout<<"The Size of 34.4 is "<<sizeof(34.4)<<endl;   //Double value
    cout<<"The Size of 34.4f is "<<sizeof(34.4f)<<endl; //Float value
    cout<<"The Size of 34.4F is "<<sizeof(34.4F)<<endl; //Float value
    cout<<"The Size of 34.4l is "<<sizeof(34.4l)<<endl; //Long double value
    cout<<"The Size of 34.4L is "<<sizeof(34.4L)<<endl; //Long double value
    cout<<endl;

    //***************** Reference Variables ********************
    float x = 32;   //Here variable is only one but there are like multiple names
    float & y = x;  //Here the value of x will be passed to the value of y
    cout<<x<<endl;
    cout<<y<<endl;
    cout<<endl;

    //***************** Typecasting *******************
    //Typecasting means changing one variable Data Type to another Data Type
    int m = 45;
    float n = 56.32;
    double v = 89.21;
    cout<<"The value of a is : "<<(float)m<<endl;
    cout<<"The value of a is : "<<float(m)<<endl;   //Both are same as above

    cout<<"The value of b is : "<<(int)n<<endl; //Here value is converting from float to integer
    cout<<"The value of b is : "<<int(n)<<endl; //Both are same as above

    cout<<"The value of v is : "<<(int)v<<endl;//Here value is converting from double to integer
    cout<<"The value of v is : "<<int(v)<<endl; //Both are same as above

    int v2 = int(n);    // Assigning value of n into variable v2
    cout<<"The value of v is : "<<int(v2)<<endl;
    cout<<endl;

    cout<<"The Expression is : "<<m + n<<endl;
    cout<<"The Expression is : "<<m + int(n)<<endl;
    cout<<"The Expression is : "<<m + (int)n<<endl;

}
//OUTPUT:
// Enter the value of a :
// 55
// Enter the value of b :
// 55
// The Sum is : 110
// The Global value is : 45
// The value of d is : 34.4
// The value of e is : 34.4

// The Size of 34.4 is 8
// The Size of 34.4f is 4
// The Size of 34.4F is 4
// The Size of 34.4l is 12
// The Size of 34.4L is 12

// 32
// 32

// The value of a is : 45
// The value of a is : 45
// The value of b is : 56
// The value of b is : 56
// The value of v is : 89
// The value of v is : 89
// The value of v is : 56

// The Expression is : 101.32
// The Expression is : 101
// The Expression is : 101

Comments

Popular posts from this blog

Control Structures, If Else and Switch Case Statements in C++

Basic Input/Output & More in C++