Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
package Garden;



public class OP
{
float num1;
public OP(float num1){this.num1=num1;}
public OP(){
}
public float getNum1(){
return num1;
}
public void setNum1(float num1){
this.num1=num1;
}
public String print(){
return "num1= " + num1;
}
public void operator 1(){
++num1;
}
public float operator 2(){
num1--;
return num1;
}
}
public class TestOP{
public static void main(String[] args){
OP p=new OP(2);
p.operator1();
system.out.prinln(p.print));
p.operator2();
system.out.println(p.print());
}
}

What I have tried:

hi
how i can convert this code to c++ oop?
Posted
Updated 18-Aug-20 21:39pm
Comments
Richard MacCutchan 18-Aug-20 3:19am    
You cannot redefine numbers as operators.

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be “good code” in the target language – they are based on very different frameworks, and what makes something work in one language does not always “translate” directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work “straight out of the box”.
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it w=from scratch using the original as a “template”. You will get a much, much better result that will save you a lot of time in the long run.

In this specific case, if you can't produce your own version of that - trivial - code, you should be considering changing courses: a second week student should be able to write that in C++ without any help at all.
 
Share this answer
 
It isnt to hard rewrite the code in C++, but you need to learn it from some tutorial like Learn C++.

Tip: use Visual Studio
 
Share this answer
 
It is not difficult to convert this Java code to C++ because it does not use advanced Java syntax or libraries. Just replace the all public and private keyword with public: and private: and also replace this.num1 with this->num1 and also replace String with std::string. Add a semi-colon at the end of the OP class (from } to };)

C++ main() does not need to be in a class because C++ is not strictly OOP, so I remove the TestOP class

Inside main(), replace system.out.println code with std::cout. Do not need to instantiate OP object on the heap with new keyword. Just on the stack will do fine.

C++
#include <iostream>
#include <string>

class OP
{
private:
    float num1;
public:
    OP(float num1) { this->num1 = num1; }
    float getNum1() {
        return num1;
    }
    void setNum1(float num1) {
        this->num1 = num1;
    }
    std::string print() {
        return std::string("num1= ") + std::to_string(num1);
    }
    void operator1() {
        ++num1;
    }
    float operator2() {
        num1--;
        return num1;
    }
};

int main()
{
    OP p(2);
    p.operator1();
    std::cout << p.print() << std::endl;
    p.operator2();
    std::cout << p.print() << std::endl;

    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900