0

I have Java code using interfaces and objects. However I cannot write it with C++. Can you help me about it?

There is an interface named Operation.java Here is the code.

public interface Operation {
    int evaluation(int x, int y); 
}

There is a class implement Operation class named Multiply.java Here is the code.

public class Multiply implements Operation {
  public int eval(int x, int y) {
    return = x*y;
  }
}

And there is a another class implement Operation class named Summation Here is the code.

public class Add implements Operation {

public int eval(int x, int y) {
    return = x+y;
}

And the final, it declares this array.

private static final Operation[] OPERATIONS = {new Multiply(), new Summation()};

In here, I am trying to write this code in C++. However I don't have deep knowledge about C++'s OOP. I understand the Java code totally, but I cannot write in C++. Could you help about it.

Thank you.

Mehmet S.
  • 27
  • 4
  • 1
    In C++ an "interface" is a class with only pure abstract functions. And for polymprhism to work in C++ you need *pointers* (or references) to the *base* class which you initialize with pointers to the concrete objects. Which [many decent C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should have taught you. – Some programmer dude May 27 '21 at 15:28
  • 4
    And a general tip when it comes to translating languages: Translating between two programming languages (no matter how similar they might seem) is very much like translating between two spoken or written languages. And you need to be knowledgeable in *both* languages to do it well. And also, don't do direct translations, they almost never turn out well. Instead take the design and reimplement it in the new language, using all the capabilities and perks of that language. – Some programmer dude May 27 '21 at 15:30
  • 3
    Going along with what's already been said, often in C++ you don't *need* late dispatch polymorphism (which is basically Java's blunt instrument solution to most problems). While you *can* use it in C++, in your case it's probably better to just write a function called `add`. That function (pointer) is a first-class object which can be passed around and, with appropriate use of templates, can be used anywhere you need it to. OOP inheritance is not the only tool at your disposal, and in C++ it's frankly rarely the best tool. – Silvio Mayolo May 27 '21 at 15:32

2 Answers2

2

First you should know that in c++ we dont have interface keyword or other thing. but interface in java it similar to c++ class with pure virtual function

class Operation
{
public:
  virtual int eval(int x, int y) = 0; 
}

which is very like interface now every class that inherit Operation must implement evaluation. in c++ unlike java we have multi inheritance.

now you can write code like:

class Multiply: public Operation {
public:
  int eval(int x, int y) override {
    return x*y;
  }
}

Note: override keyword is important.

and then you can write:

Operation* a = new Multiply();

but also note that you can convert your OOP code to simple functional code in c++ world.

and so on..

N0ll_Boy
  • 500
  • 3
  • 6
0

Given that your eventual goal seems to be calling these things, I might suggest not making objects in the first place. C++ (and recent versions of Java, for that matter) has first-class functions, which means that you can just write functions and treat them like objects for free.

#include <vector>
#include <functional>

int add(int x, int y) {
  return x + y;
}

int multiply(int x, int y) {
  return x * y;
}

int main() {
  std::vector<std::function<int(int, int)>> arr { add, multiply };
}
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116