Python to C++ translation?

M

Mangabasi

Hi there,

I need to translate the following code (rather something similar) to
C++. I have been studying C++ for the last two days but I could not
find an easy way to do the following Python snippet.


class A:
def __init__(self, x):
self.x = x
def methodA():
pass # Ignore the details
class B:
def __init__(self, x):
self.x = x
def methodB():
def methodB():
pass # Ignore the details
class C:
def __init__(self, A, B):
self.A = A
self.B = B

a = A(5)
b = B(5.5)
c = C(a, b)
print c.A.x
print c.B.x
#so far I can do it in C++
#how do I do the following in C++?
d = C(b, a)
print d.A.x
print d.B.x


Basically I want A and B to be instances of different classes if
necessary like the above code.

#include <iostream.h>
class A {
public:
int x;
A( ) {
}
A(int _x) {
x = _x;
}
};

class B {
public:
double x;
B( ) {
}
B(double _x) {
x = _x;
}
};

class C {
public:
A *propA;
B *propB;

C(A &_propA, B &_propB) {
propA = &_propA;
propB = &_propB;
}


};

main( ) {
A a = A(42);
B b = B(23.0);
C c1 = C(a, b);
cout << c1.propA->x << "\n";
cout << c1.propB->x << "\n";
}

How do I make this work for

C c2 = C(b, a)

as well?

Thank you in advance, I know this is somehow offtopic in the Python
group but I would not dare asking this in the C++ groups.

Abusing the civility of this grouply yours...

Peace
 
G

George Sakkis

Mangabasi said:
Hi there,

I need to translate the following code (rather something similar) to
C++. I have been studying C++ for the last two days but I could not
find an easy way to do the following Python snippet.


class A:
def __init__(self, x):
self.x = x
def methodA():
pass # Ignore the details
class B:
def __init__(self, x):
self.x = x
def methodB():
def methodB():
pass # Ignore the details
class C:
def __init__(self, A, B):
self.A = A
self.B = B

a = A(5)
b = B(5.5)
c = C(a, b)
print c.A.x
print c.B.x
#so far I can do it in C++
#how do I do the following in C++?
d = C(b, a)
print d.A.x
print d.B.x

In python this works thanks to "duck typing"; as long as both A and B instances have an attribute
'x', A.x and B.x work as expected without the need for type declarations. In C++ (and other
statically typed languages) there is no duck typing, so you can't translate it from python verbatim.
The typical (if not only) way to achieve the same in C++ is to have A and B inherit from a common
parent class, say X, and drag the common interface and implementation of A and B to X. Of course you
could (or perhaps should) do the same in python:

class X:
def __init__(self, x):
self.x = x

class A(X):
def methodA():
pass # Ignore the details

class B(X):
def methodB():
pass # Ignore the details

class C:
def __init__(self, x1, x2):
self.x1 = x1
self.x2 = x2

So the constructor of C would take two X instances, and the respective attributes x1 and x2 would be
declared as pointers to X.

HTH

George
 
S

szymon.rozga

Ok so here's what you have:
A a(42);
B b(23.0);

a is now of type A, and b is now of type B. Right?

The constructor for class C takes in two arguments. The first one _has_
to be of type A and the second one _has_ to be of type B. This is how
you defined it.
This is why C(a,b) works fine. But C(b,a) shouldn't work because of how
you defined the constructor for C. You could add a second constructor
to C as follows:

C(B &_propB, A &_propA) {
propA = &_propA;
propB = &_propB;
}

which would let you call C(b,a).
 
J

Jorgen Grahn

Hi there,

I need to translate the following code (rather something similar) to
C++. I have been studying C++ for the last two days but I could not
find an easy way to do the following Python snippet. ....
How do I make this work for

C c2 = C(b, a)

as well?

Thank you in advance, I know this is somehow offtopic in the Python
group but I would not dare asking this in the C++ groups.

In general, it is not a good idea to try to make a straight translation from
one language to another, especially between a very dynamic one like Python
to a very static one like C++ [1]. You might be able to do a straight
translation of some of the design, but soon you would find yourself working
against the language and missing out on its good features.

I think you'd be better off starting from scratch. (Or maybe not really from
scratch; having done a working version of the program once and maybe having
working tests is worth a lot.)

/Jorgen

[1] You might find C++ templates useful, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top