We know that our program gave precedence to local variable
so to print the value of Global variable we use Scope Resolution Operator.
ex -> cout<< : : c; // c is the name of Global variable.
REFERERENE VARIABLE
Reference variable is defined as another name for already existing variable.
Ex-> float x = 455;
float &y = x;
so if we will print the value of x and y ans will be 455.
TYPECASTING
Converting one data type into another data type is known as typecasting.
ex-> int f = 45;
float g = 45.37;
cout<<(float)f<<endl;
cout<<(int)g<<endl;
And also we can give the value of one intiger to new intiger in our program.
int h = int(g);
SOURCE CODE
//TYPECASTING AND REFERENCE VARIABLE IN C++.
#include <iostream>
using namespace std;
//******BUILT IN DATA TYPES******
int c = 37; //This is a Global variable..
int main()
{
cout<<"___BUILT IN DATA TYPES___"<<endl;
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; //we know that it is our Arithmatical operator.
cout<<"the sum is = "<<c <<endl ;
cout<<"the value of global variable is = "<<::c <<endl;
//to print the value of GLOBAL VARIABLE we use Scope Resolution operators..(<<:: )...
cout<< "******FLOAT,DOUBLE AND LONG DOUBLE LITERALS******"<<endl;
float d = 34.4;
long double e = 34.4;
34.4 ;//By default decimal waala literal hammare program mai double hotta hai..
//to agar hm specify krna chate hai ki ye number fLOAT hai to hm (f)/(F) lagate
//hain number ke baad...
34.4f; // ab ye number as a float pass hogga...
34.4F;
34.4l; //agar hm (l)/(L) lagay to wo decimal as a Long double pass hogga..
34.4L;
cout<<"the value of d is = "<<d <<endl <<"the value of e is = "<<e <<endl;
cout<<"******REFERENCE VARIABLES******"<<endl;
cout<<"ek Admi hai uska naam AYUSH hai ->ghr mai usse JUNGADI bulate hain and wo khud ko AYU bulata hai.."<<endl;
//AYUSH ----> JUNGADI ----> AYU ....
float x = 455; //same upper ki tarha ye variable bhi ak he haai so hamme x and y
float & y = x;//variable bs naam alag ho gay value ak he hai..
cout<<x <<endl;
cout<<y <<endl;
cout<<"****TYPECASTING****"<<endl;//used to interchange DATA TYPES.
int f = 45;
float g = 45.37;
cout<<"the value of f is = "<<(float)f<<endl;
cout<<"the value of g is = "<<int(g)<<endl;
cout<<"the value of f + g = "<<f+g<<endl;
cout<<"the value of f + int(g) = "<<f+int(g)<<endl;
cout<<"the value of f + (int)g = "<<f+(int)g<<endl;
int h = int(g);//this type we are able to put value of int g in h..
cout<<"The vaue of h is : "<<h<<endl ;
return 0;
}
// OUTPUT
// ___BUILT IN DATA TYPES___
// enter the value of a =
// 3
// enter the value of b =
// 33
// the sum is = 36
// the value of global variable is = 37
// ******FLOAT,DOUBLE AND LONG DOUBLE LITERALS******
// the value of d is = 34.4
// the value of e is = 34.4
// ******REFERENCE VARIABLES******
// ek Admi hai uska naam AYUSH hai ->ghr mai usse JUNGADI bulate hain and wo khud ko AYU bulata hai..
// 455
// 455
// ****TYPECASTING****
// the value of f is = 45
// the value of g is = 45
// the value of f + g = 90.37
// the value of f + int(g) = 90
// the value of f + (int)g = 90
// The vaue of h is : 45
Comments
Post a Comment