You should call the inherrited constructor too, i.e.
Point_2D::Point_2D() : Point_1D(),_y =3D 0 {}
Point_2D::Point_2D(const float x,const float y) : Point_1D(x),_y =3D y {}
Point_2D::Point_2D(const Point_2D &p) : Point_1D(p),_y =3D p.y() {}
Point_2D::Point_2D(const Point_1D &p,const float y) : Point_1D(p),_y =3D y
{}
Also - no new in your operators. Who would release your memory? The
receiver doesn't expect a pointer, but a reference, so the receiver won't
release it. Declare a local variable
{ Point_2D res; xxxxx; return res; }
Also, you normally uses friend operators when overriding binary operators.
Here's a sample Vec3 class - sorry strange indent but the cut-paste went
bananas :-)
struct Vec3 {
double x,y,z;
friend Vec3& operator+=3D(Vec3 &p,const Vec3 &s)
{ p.x +=3D s.x; p.y +=3D s.y; p.z +=3D s.z; return p; }
friend Vec3& operator-=3D(Vec3 &p,const Vec3 &s)
{ p.x -=3D s.x; p.y -=3D s.y; p.z -=3D s.z; return p; }
friend Vec3& operator*=3D(Vec3 &p,const Vec3 &s)
{ p.x *=3D s.x; p.y *=3D s.y; p.z *=3D s.z; return p; }
friend Vec3& operator*=3D(Vec3 &p,real s)
{ p.x *=3D s; p.y *=3D s; p.z *=3D s; return p; }
friend Vec3& operator/=3D(Vec3 &p,real s)
{ p.x /=3D s; p.y /=3D s; p.z /=3D s; return p; }
friend const Vec3 operator+(const Vec3 &a,const Vec3 &b)
{ Vec3 res; res.x =3D a.x+b.x; res.y =3D a.y+b.y; res.z =3D a.z+b.z; re=
turn res; }
friend const Vec3 operator-(const Vec3 &a,const Vec3 &b)
{ Vec3 res; res.x =3D a.x-b.x; res.y =3D a.y-b.y; res.z =3D a.z-b.z; re=
turn res; }
friend const Vec3 operator*(const Vec3 &a,const Vec3 &b)
{ Vec3 res; res.x =3D a.x*b.x; res.y =3D a.y*b.y; res.z =3D a.z*b.z; re=
turn res; }
friend const Vec3 operator*(real s,const Vec3& a)
{ Vec3 res; res.x =3D a.x*s; res.y =3D a.y*s; res.z =3D a.z*s; return r=
es; }
friend const Vec3 operator*(const Vec3 &a,real s)
{ Vec3 res; res.x =3D a.x*s; res.y =3D a.y*s; res.z =3D a.z*s; return r=
es; }
friend const Vec3 operator/(const Vec3 &a,real s)
{ Vec3 res; res.x =3D a.x/s; res.y =3D a.y/s; res.z =3D a.z/s; return r=
es; }
friend const Vec3 operator/(const Vec3 &a,const Vec3& b)
{ Vec3 res; res.x =3D a.x/b.x; res.y =3D a.y/b.y; res.z =3D a.z/b.z; re=
turn res; }
friend const Vec3 operator-(const Vec3 &a)
{ Vec3 res; res.x =3D -a.x; res.y =3D -a.y; res.z =3D -a.z; return res;=
}
friend const Vec3 abs(const Vec3& a)
{ Vec3 res; res.x =3D a.x >=3D 0 ? a.x : -a.x; res.y =3D a.y >=3D 0 ? a=
=2Ey : -a.y;
res.z 0 ? a.z : -a.z; return res; }
bool operator=3D=3D(const Vec3& a)
{ return IS_SMALL(a.x-x) && IS_SMALL(a.y-y) && IS_SMALL(a.z-z); }
bool operator!=3D(const Vec3& a)
{ return !IS_SMALL(a.x-x) || !IS_SMALL(a.y-y) || !IS_SMALL(a.z-z); }
/Per W
On Wed, 26 Feb 2003, Micka=EBl Wolff wrote:
> Hello,
>
> I restart my C++ learning. For do this, I write some C++ classes. I
> made a first class 'Point'. I shall be use in the future, in other
> relative classes (triangle class, circle class, etc ...).
>
> The first derivate class are Point_1D, it runs good. The second
> inheritance from Point_1D to Point_2D do a lot of problemes with
> constructors. I wrote its into my sources (in the attachement). Can you
> say me what is wrong ?
>
> Thanks for your help !
>
> PS.: I hope they are not off topics ! :o)
>
|