• Post Reply Bookmark Topic Watch Topic
  • New Topic

reference id  RSS feed

 
abhinav sood
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this program gives the output null and zero can any one explain that.....



[moderator edit: added code tags]
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Abhinav !

can any one explain that.....


yes:

First you call:


This will create a new emp() object, call its get() member, and then loses the reference to the object (you don't store the object in a variable).

Then you call:


This will create a brand new object, call its show() method -- that will print null and 0 because you didn't use get() to set its members, and then loses the reference to the object too.



What should you have done:

when creating the object, store it in a variable of type emp (emp myEmp = new ....)
then call the members of that variable (myEmp.get(), myEmp.show() )



Some style remarks:
A class by convention starts with upper case (class Emp)
A method to set properties should be called set..(), not get..().
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!