May 4, 2011

My C++ Experiments

The following program will access the private variable outside the class which is theoretically not possible in C++.
'info' private variable will be assigned using the ‘int’ pointer variable ‘a’.

#include<iostream.h>
#include<conio.h>
#include<process.h>

class Node
{
private :
int info;
public :
void disp()
{
cout<<info<<"\n";
}
};

void main()
{
Node *n;
int *a;
clrscr();
a = (int *) n;
*a=30;
cout<<*a<<"\n";
n->disp();
}


The following program a very interesting example of 'for loop'. Do you think the program will give syntax error or it will go in infinite loop?
It won’t. It will just print ‘1’. To know how, you should learn 'for loop' very carefully or visit me personally or left your email in comments because it is very 

difficult to explain here. 

#include<iostream.h>
#include<conio.h>
void main()
{
int i=0;
for(;i++;cout<<i)  ;
cout<<i;
}

Popular Posts