Jump to content
xisto Community
Sign in to follow this  
sourabhj

A C++ Program Print Hello With Nothing Written In Main() Method Its the magic of oops

Recommended Posts

Hi guys can u make a program to write hello on screen without writing anything in main() :lol: u might be thinking hows that possible it is neccesary for every c/c++ program to start with main and execute statement written in main but here no statements in main how is it possible?,dont worry it is possible ill show u how:

#include<stdio.h>#include<iostream.h>class hello	{		int a;		public:		 	hello(){cout<<"hello";};	}magic; void main()	{	}

Can u guess how it worked
since we are creating an global object of hello class it gets executed at the first and its constructor in which we have written hello that statement gets executed as a result hello gets printed on the screen and then the main comes in to existence
cool na ,its the magic of oops

:P:D
Edited by sourabhj (see edit history)

Share this post


Link to post
Share on other sites

Clever coding. Of course, it's not the most efficient thing to go about creating global declarations in order to execute code, but... nice. :lol:Two (technical) questions:I) why'd you include stdio.h? I'm not seeing any c functions. Just adding kilobytes to the program size.II) why is everyone assuming a using std statement here? It doesn't make sense to do it in a public forum where some people *cough*me*cough* use ANSI strict styles for coding. It can also confuse newbies to C++.~just a syntax argument.

Share this post


Link to post
Share on other sites

Clever coding. Of course, it's not the most efficient thing to go about creating global declarations in order to execute code, but... nice. :lol:
Two (technical) questions:
I) why'd you include stdio.h? I'm not seeing any c functions. Just adding kilobytes to the program size.
II) why is everyone assuming a using std statement here? It doesn't make sense to do it in a public forum where some people *cough*me*cough* use ANSI strict styles for coding. It can also confuse newbies to C++.
~just a syntax argument.

Yes u are right there is no use of stdio.h,actually its a like template ,i have been using, since std functions are sometimes needed and its time consuming and pain to write include statement in turboc 3.0 so i made a format which i copy paste in program then start coding.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.