wood burning stoves 2.0*
The moose likes Java in General and the fly likes C++ to java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "C++ to java" Watch "C++ to java" New topic
Author

C++ to java

Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Hi,
I don't know C++ and i work with java.

I have code in C++ and i don't know what is equivalent of this code in java.
please help me if you can write it to java .

code is:


employee_type is an enum --> enum employee_type { tmanager,tscientis,tlaborer};

[Edit - added code tags - MAB]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40


Hint: instanceof operator


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
what you mean?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
You can create enums in Java™ too, but they have much more functionality than C++ enums.That saves messing about with instanceof and the == operator. Lots more about enums in the Java Tutorials.

And welcome to the Ranch Sheila Jorge.
[edit: add last line]
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 307
    
    2

Sheila Jorge wrote:Hi,
I don't know C++ and i work with java.

I have code in C++ and i don't know what is equivalent of this code in java.
please help me if you can write it to java .

code is:


employee_type is an enum --> enum employee_type { tmanager,tscientis,tlaborer};

[Edit - added code tags - MAB]


One of the problem with C++ is that everything can be overridden so each program looks like a different language. Some think this is an advantage.

The snippet you show could mean a couple of things.

The simplest for me is employee is a base class and manager, scientist and laborer are derived from employee. employee_type is an enum.

What Henry suggested is that you have a method in your base class that does what your C++ function does using the instance_of operator see http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

something like:


What Campbell described is more Java like and defines the type as a member variable of the derived class.

Joe "likes to restate the obvious" Areeda


It's not what your program can do, it's what your users do with the program.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

Sheila Jorge wrote:I have code in C++ and i don't know what is equivalent of this code in java.

It's actually quite similar to equivalent Java, apart from (as Cambell pointed out) the enum.

It defines a method get_type() that is part of an employee class, that returns an enum defined by employee_type. Unfortunately, without the code for the typeid() method, it's difficult to know exactly what's going on; but it appears to be comparing this object's type ID (whatever that is) with some "dummy" employees set up with specific types.

So essentially it's converting one code (type id?) to another code (employee_type).

please help me if you can write it to java.

Well it doesn't look like a shining example of C++ to me; and there are far better ways to do it in Java (as indeed there are in C++), so I definitely wouldn't do a straight translation.

Just one possibility would be to set up an Employee (not employee) class, and then add proper subclasses for each type. That way you wouldn't need the enum. But since this is presumably only a fraction of the code you have to go through, it's difficult to be certain.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Thanks all,My problem solved . All your guidance were good and helpful.
Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
Campbell Ritchie wrote:You can create enums in Java™ too, but they have much more functionality than C++ enums.That saves messing about with instanceof and the == operator. Lots more about enums in the Java Tutorials.

And welcome to the Ranch Sheila Jorge.
[edit: add last line]


your help was very helpful .Thanks a lot
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
You’re welcome
 
wood burning stoves
 
subject: C++ to java
 
Similar Threads
Descending order and associating Array Elements in sort method to another's elements
create objects of my classes dynamically and put them into linkedlist
JSP, beans and sharing ArrayLists
help - compiled error
Factory Design-Pattern: (PDFFactory) can you access the db in the getPDF?