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;
}

4 comments:

yogeshnc said...

Hello dear, second Program have semicolon (;) at the end of for(); line that why after going to increment part it print 1 and exit becouse of semicolon

Anonymous said...

plz tell me how it works. my email: raviclalwani@gmail.com

Madhuri Kolhe said...

Semicolon will end that for loop at first time.
but as while executing the line at first time will increment the value of i.
so it will print 1.

Tushar T. Chaudhari said...

No guys...there is nothing to do with that semicolon after for loop...

@Ravi it is not possible to explain it through email...otherwise I already did that....

Popular Posts