Showing posts with label General. Show all posts
Showing posts with label General. Show all posts

Saturday, May 12, 2012

Why not to use void main

In college, while using turbo c, we learn to use "void main()". But this is against c++ standard. Actually this shouldn't compile. Main function must return a int value. Main function should be declared like "Int main()". And at the end "return 0" may be added to indicate that function executed successfully. It may return 1 in order to indicate that some error has occurred. This is also referred as Exit Status. It can be EXIT_SUCCESS (traditionally 0) and EXIT_FAILURE. If return is not provided by programmer, it will return 0 by default.  For other exit statuses you may have a look in stdlib.h(cstdlib).

Friday, May 11, 2012

Assignment operator in RVALUE

I came across a line of code which looked a bit strange to me. It reads like this..
x= m * (n=7);
The above code is actually equivalent to
n=7
x=m*n

Actually c++ supports using assignments on the right hand side of assignments operator. And we are all familiar with that. I bet you must have seen assignments like this…
a=b=c;
This and the previous code follows the same rule.

Calling constructor from constructor


I was trying a few whiteboard programs, just for fun. In one of them I called a constructor from within another constructor. Code compiled without any fuss,but code didn’t worked the way I was expecting.  I found out that when calling a constructor from within a constructor a temporary unnamed constructor is created. That is the reason why program is not behaving the way I was expecting. Check out this…


using namespace std;
class foo
{
int x;
public:
foo()
{
x=20;
}
foo(int d)
{
x=d;
foo();//value should be changed to 20, but it wont
}
void show()
{
foo a;
a.show();// will print 20
foo b(10);
b.show();// will print 10. I was expecting it to print 20
return 0;
}
(This one is from my old blog.)

Saturday, February 4, 2012

#{Header}

Hi,
    It's late night in 4th feb. I was wasting time re inventing wheels , i mean i was working on some basic c++ codes. I have practiced them a number of times, but at present i couldn't remember them. I think it's time to make a note of all these. So I am creating this blog for myself. I will post all cpp related stuffs which i feel necessary for me. If you find this site useful, please get in touch. I would love to see you as a contributer.

Regards
Rwik