rokoder

Java to C++

by on Dec.29, 2011, under Programming

Many times people from Java endup writing C++ code and vice versa (for vice versa check notes below). This is the beauty of working in such a dynamic/vast programming eco-system. This blog is to help them create an analogy between the two.

Below we have a sample Java code which we like to convert to C++.

interface MyInterface {
  void func1();
  int func2(int x, int y);
}

interface MyAnotherInterface {
  void func3();
  int func4(int x, int y);
}

abstract class MyAbstract implements MyInterface {
  void func1() {
    System.out.print("In func1");
  }
}

class MyConcreteClass extends MyAbstract
  implements MyAnotherInterface {
  int func2(int x, int y) {
    System.out.print("func2 x=" + x + ",y=" + y);
  }

  void func3() {
    System.out.print("in func3");
  }

  int func4(int x, int y) {
    System.out.print("func4  x=" + x + ",y=" + y);
  }
}


Interface

Interface is a very popular Java as well as OOP construct. This implies program for contract and not for implementation (very popular design pattern too). In Java, `interface` cannot have method bodies/implementation. It just states the contract in terms of method declarations. Now to convert this to C++, we call for pure virtual functions and abstract classes. Pure virtual functions are functions which are there just to describe the contract and does not contain any body/implementation. The implementation is provided by the class who inherits it, which is similar to Java interface. In our scenario it will work as an interface works in Java, as long as all our methods in MyInterface and MyAnotherInterface are pure virtual functions. We cannot create instances of these C++ interface like classes as the methods are not defined so they work like Java interfaces.

Java

interface MyInterface {
  void func1();
  int func2(int x, int y);
}

interface MyAnotherInterface {
  void func3();
  int func4(int x, int y);
}

C++

class MyInterface {
  virtual void func1() = 0;
  virtual int func2(int x, int y) = 0;
}

class MyAnotherInterface {
  virtual void func3() = 0;
  virtual int func4(int x, int y) = 0;
}

In C++, `virtual` keyword means that it is part of polymorphism and will work according to its principals when derived class overrides it. And `= 0` means that there is no implementation for this method in this class and derived class must provide an implementation (unless that is an abstract class, we will cover this shortly).

Abstract Class

In Java, `abstract` class is a class whose instance cannot be created because a complete definition of class is missing. That is, not all methods are defined. C++ `abstract` class works the same and you cannot create instances of abstract class.

Java

abstract class MyAbstract implements MyInterface {
  void func1() {
    System.out.print("In func1");
  }
}

C++

class MyAbstract : public MyIterface {
  void func1() {
    printf("in func1");
  }
}

This part is simple just slight syntax difference. One other difference is `implements` vs `extends`. In Java, we implement interface but in C++ there is no `interface` keyword. Everything is a class and classes are extended.

Concrete Class

In Java and C++, concrete classes are the ones whose instance we can create.

Java

class MyConcreteClass extends MyAbstract
  implements MyAnotherInterface {
  int func2(int x, int y) {
    System.out.print("func2 x=" + x + ",y=" + y);
  }

  void func3() {
    System.out.print("in func3");
  }

  int func4(int x, int y) {
    System.out.print("func4 x=" + x + ",y=" + y);
  }
}

C++

class MyConcreteClass : public MyAbstract, public MyAnotherIterface {
  int func2(int x, int y) {
    printf("func2 x=%d,y=%d",x,y);
  }

  void func3() {
    printf("in func3");
  }

  int func4(int x, int y) {
    printf("func4 x=%d,y=%d",x,y);
  }
}

In Java we can implement multiple interfaces as shown in above example and in C++ there is no `interface` keyword. But C++ has multiple class inheritance which we are going to leverage here. In the above code, we are extending from two classes `MyAbstract` and `MyAnotherIterface` and it will behave as it will in Java.

There are some issues when you are using Multiple inheritance as I am using for MyConcreteClass. But if you only have one base with member variables and other base classes contain only pure virtual functions then this pattern acts exactly like Java, where the class containing some methods and member variables maps to extends and other classes which contain only pure virtual functions maps to `implements’.

Full C++ Code

class MyInterface {
  virtual void func1() = 0;
  virtual int func2(int x, int y) = 0;
}

class MyAnotherInterface {
  virtual void func3() = 0;
  virtual int func4(int x, int y) = 0;
}

class MyAbstract : public MyIterface {
  void func1() {
    printf("in func1");
  }
}

class MyConcreteClass : public MyAbstract, public MyAnotherIterface {
  int func2(int x, int y) {
    printf("func2 x=%d,y=%d",x,y);
  }

  void func3() {
    printf("in func3");
  }

  int func4(int x, int y) {
    printf("func4 x=%d,y=%d",x,y);
  }
}

This concludes this part…happy coding…:)

NOTES: If you are trying from C++ to Java then it is bit tricky, as what you can do in C++ might not be possible in Java. So if the C++ code is structured as mentioned above then reversing the process is straight forward. Just map the above example in reverse order.

:, , , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...

Archives

All entries, chronologically...