Let's say I have two custom classes in Java, class A and class B:
class A {
int x;
int y;
public A(int x, int y)
{
this.x = x;
this.y = y;
}
}
class B {
A a;
int z;
public B(A a, int z)
{
this.a = a;
this.z = z;
}
}
And I want to translate this situation into C++.
class A will translate more or less as it is, but when I go to class B and write such code:
class B {
A a;
int z;
public:
B(A a1, int z1){
a = a1;
z =z1;
}
};
it complains saying that class A does not have default constructor, so when I declare
A a;
at the top of class B it cannot instantiate my "a" variable (Java does not instantiate at declaration, whereas C++ does, as I understand).
So what would be the normal C++ way to deal with this situation: should I add default constructor without arguments to class A, or this is not the right way to go?
Thanks a lot.