Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

C++ to java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hint: instanceof operator
 
Sheila Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you mean?
 
Marshal
Posts: 79532
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79532
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Their achilles heel is the noogie! Give them noogies tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic