Sunday, April 19, 2015

C++ Programming

Use of sizeof()

This function is used for finding the size of any variable include array ,structure and class .

Syntax:

if we want to find the size of integer variable :

int a,b;
b=10;
a=sizeof(b);
cout<< b <<endl ;

or
cout <<sizeof(b)<<endl;

if we want to find the size of general data type :

cout<<sizeof(int)<<endl;
cout<<sizeof(float)<<endl;
cout<<sizeof(bool)<<endl;

OOP using c++

Pass by Reference Function Example

#include<iostream>//for cin and cout objects
using namespace std;//definition of cin and cout object lies in std namespace
void swap(int &a,int&b)//function prototype

//use address operator (this operator return the memory address of variable)
{
int temp;
temp=a;
a=b;
b=temp;
}
void main()
{
int x=2;
int y=4;
swap(x,y);
cout << x << endl ;
cout << y << endl ;
        system("pause");
}