473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python to C++ translation?

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

Jul 21 '05 #1
3 2764
"Mangabasi" <ma*******@gmail.com> wrote:
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
Jul 21 '05 #2
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).

Jul 21 '05 #3
On 18 Jul 2005 14:39:22 -0700, Mangabasi <ma*******@gmail.com> wrote:
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.

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Aug 7 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stephen Ferg | last post by:
I need to translate some Perl scripts into Python. When I went looking for a tool that would help automate the translation, I was rather surprised that I couldn't find anything. BridgeKeeper,...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 241 open ( -6) / 2622 closed (+26) / 2863 total (+20) Bugs : 764 open ( +6) / 4453 closed (+38) / 5217 total (+44) RFE : 150 open...
13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
50
by: Gosi | last post by:
It is quite easy to call J from Python http://groups.google.com/group/J-Programming/browse_thread/thread/5e84b75667f5f64e
3
by: galadhad | last post by:
Okay, here goes. I'm new to Python (and relatively new to programming/scripting in general - I understand basics, but complex concepts are still beyond my limited knowledge). I am working on a...
4
by: Joe Hrbek | last post by:
Could someone help me translate to something that would close to it in python? The anonymous functions are giving me problems. var dataListener = { data : "", onStartRequest: function(request,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.