Python to C++ translation?

Mangabasi mangabasi at gmail.com
Mon Jul 18 17:39:22 EDT 2005


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




More information about the Python-list mailing list