Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C++ to java

 
Sheila Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: instanceof operator
 
Sheila Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you mean?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheila Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,My problem solved . All your guidance were good and helpful.
 
Sheila Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic