scope resolution operator, literals, reference variables, typecasting in c++

 #include<iostream>


using namespace std;

int c=15; //global variable

int main(){
    int a, b; //local variables (preference is given to local variables)
    cout<<"enter the value of a: "<<endl;
    cin>>a;
    cout<<"enter the value of b: "<<endl;
    cin>>b;
    int c=a+b;
    cout<<"the sum is "<<a+b<<endl;
    cout<<"the value of c is "<<::c<<endl; //this (::) is scope resolution operator, it is used to print a global variable inside the function

    //LITERALS IN C++

    //float x=5.7;
    //long double y=5.7; //now 5.7 is a double by default by the compiler, we can use specific keywords to make a variable float double or long double
    //cout<<"the value of x is "<<x<<endl<<"the value of y is "<<y<<endl;

    float x=5.7f; //now this is floating poing number
    long double y=5.7l; //and this is long double number
    //we can use f or F and l or L it is the flexibility of the syntax
    cout<<"the value of x is "<<x<<endl<<"the value of y is "<<y<<endl;
    cout<<"the size of 5.7 is "<<sizeof(5.7)<<endl;
    cout<<"the size of 5.7f is "<<sizeof(5.7f)<<endl;
    cout<<"the size of 5.7F is "<<sizeof(5.7F)<<endl;
    cout<<"the size of 5.7l is "<<sizeof(5.7l)<<endl;
    cout<<"the size of 5.7L is "<<sizeof(5.7L)<<endl;
    cout<<endl;
   
    //reference variables in c++

    //shalini-->strawberry-->blueberry--->princess

    int s=5;
    int & k=s; //s is the original variable and k is the reference variable like strawberry
    cout<<s<<endl;
    cout<<k;
    cout<<endl;

    //typecasting in c++

    int u=75;
    cout<<"the value of u is "<<float(u)<<endl;
    float v=7.9;
    cout<<"the value of w is (where w is the gif of v) "<<int(v)<<endl;
    int w=int(v);
    //we can us int(a) or (int)a, it is the flexibility of syntax

    return 0;
}

Comments

Popular posts from this blog

Function Overloading in C++

oops recall, basic operations and nesting of member functions

OBJECT ORIENTED PROGRAMMING, classes public and private access modifiers (syntax)