python>>c++



  • hey
    i have such a code on python that i would like to translate to c++ i dont know much about neither of them can anyone help with questions like:
    how to define a function that have a class as return ?
    and what is the meaning of this self and how can we get the same meaning on a c++ code ?

    class quaternion:
    def __init__(self, a, b, c, d):
    self.R = float(a) # real part
    self.I = float(b) # first imaginary part
    self.J = float(c) # second imaginary part
    self.K = float(d) # third imaginary part
    def normalize(self):
    n = self.norm()
    qret = quaternion(self.R, self.I, self.J, self.K)
    if n == 0:
    print 'cant normalize, is 0'
    else:
    qret.R = self.R/n # real part
    qret.I = self.I/n # first imaginary part
    qret.J = self.J/n # second imaginary part
    qret.K = self.K/n # third imaginary part
    return qret
    return quaternion(a, b, c, d)



  • abbl1 schrieb:

    how to define a function that have a class as return ?

    Use the class name as return type.

    abbl1 schrieb:

    and what is the meaning of this self and how can we get the same meaning on a c++ code ?

    By convention, self is the usual name for the first method parameter, which is a reference to the instance used for the method call. It is used to access the class instance's data attributes. In C++, you don't need it.



  • so is that right if i write this for this python code?

    class quaternion {

    float R,I,J,K;

    public:

    quaternion (float,float,float,float);

    quaternion normalize() {

    n=norm();
    qret=quaternion(R,I,J,K);
    qret.R=R/n;
    qret.I=I/n;
    qret.J=J/n;
    qret.K=K/n;
    return qret;
    }



  • MFK schrieb:

    By convention, self is the usual name for the first method parameter, which is a reference to the instance used for the method call. It is used to access the class instance's data attributes. In C++, you don't need it.

    The equivalent in C++ is named "this".
    --> http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm



  • abbl1 schrieb:

    so is that right if i write this for this python code?

    - Definition of qret is missing
    - quaternion constructor definition is missing
    - You're not checking for n==0



  • thanks guys 😃
    just one last check :
    class quaternion {

    float R,I,J,K;
    public:

    quaternion (float a ,float b ,float c ,float d ) {

    R=a; I=b; J=c; K=d; }
    quaternion normalize() {

    quaternion qret;
    float n;

    n=norm();
    qret=quaternion(R,I,J,K);
    qret.R=R/n;
    qret.I=I/n;
    qret.J=J/n;
    qret.K=K/n;
    return qret;
    }
    } return quaternion(a, b, c, d);
    (forget about the n==0 )



  • There are still some problems, but how to fix them depends on what you want:

    Do you want to be as close to the Python code as possible, or do you want good C++?
    Do you want code that works? Because right now your C++ code won't work, just like the Python code.



  • so how would be the perfect code for it?
    i dont know much about it so i based my work on the python code
    PLEASE HELP 😞



  • @abbl1
    The perfect code for what?
    And say we give you "the perfect code". What would you do with it? You're obviously not an able programmer, so what would be the point of you having it? I mean, you wouldn't be able to use it anyway.

    And after considering this, it's beginning to smell distinctively like homework.



  • @ hustbaer
    you're right i'm not a programmer i'm an electrical engineer and i'm doing an internship whithin the first step i have to do is to translate a very very long python code to c++ so i can do something with it in reality and unfortantly as you said i'm not a programmer ,i'm reading books, i'm trying to do something good with this programme i'm trying hard but as every aknowlege person who want to do something good, i'm asking for help i though with this forum maybe i can find something, unfortuntly there is people like u who smell homework everywhere so if u want help, please don't, but at least let people who want to do something TRY.
    (and i've chosen this part of the programme because it resumes all the problemes that i have with classes and functions )



  • @abbl1
    I understand that this might be frustrating for you. However I do also think that it's my right to make an observation.

    Let me make another observation: the code you posted with the question if it's OK, doesn't even compile. What do you want to do with a piece of code, if you cannot even compile it?
    And then the phrase "just one last check". One last check before ... what?

    So you tell me: If this doesn't smell like homework, then what does?

    I will however not interfere if anyone wants to help you - aside from making my "observations" that is.

    ----

    That aside...
    Maybe you should start with settings up a development environment where you can compile and run C++ code.
    The chances of learning C++, even just "a little bit", without a working development environment, are close to zero.


Log in to reply