Thread: Equals()

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question Equals()

    I was reading MSDN:
    Code:
    class TwoDPoint : System.Object
    {
        public readonly int x, y;
    
        public TwoDPoint(int x, int y)  //constructor
        {
            this.x = x;
            this.y = y;
        }
    
        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return false;
            }
    
            // If parameter cannot be cast to Point return false.
            TwoDPoint p = obj as TwoDPoint;
            if ((System.Object)p == null)
            {
                return false;
            }
    
            // Return true if the fields match:
            return (x == p.x) && (y == p.y);
        }
    
        public bool Equals(TwoDPoint p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }
    
            // Return true if the fields match:
            return (x == p.x) && (y == p.y);
        }
    
        public override int GetHashCode()
        {
            return x ^ y;
        }
    }

    Code:
    class ThreeDPoint : TwoDPoint
    {
        public readonly int z;
    
        public ThreeDPoint(int x, int y, int z)
            : base(x, y)
        {
            this.z = z;
        }
    
        public override bool Equals(System.Object obj)
        {
            // If parameter cannot be cast to ThreeDPoint return false:
            ThreeDPoint p = obj as ThreeDPoint;
            if ((object)p == null)
            {
                return false;
            }
    
            // Return true if the fields match:
            return base.Equals(obj) && z == p.z;
        }
    
        public bool Equals(ThreeDPoint p)
        {
            // Return true if the fields match:
            return base.Equals((TwoDPoint)p) && z == p.z;    }
    
        public override int GetHashCode()
        {
            return base.GetHashCode() ^ z;
        }
    }
    Look at the bold line. How it casts ThreeDPoint class to TwoDPoint? ThreeDPoint class has three fields, while TwoDLine has two.
    Last edited by siavoshkc; 09-26-2006 at 09:00 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    2d class is the base class, 3d is the derived class with 2d as a base class.

    When you create a 3d class object , in fact you pass 2 of the 3 arguments to the constructor of the base class, this is not a one way street, so going back from a 3d object to a 2d object is fine, as long as 3d is derived from 2d ( of course all the properties/methods of the 3d class will not be available to the downcasted object which now is a 2d object ).

    Now in the 3d class you can use any of the base class methods that are public.

    I would not write the Equals method of the 3d class like that, but that's just something personal I think.

    So there's nothing wrong with what the code is doing.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yeah, there is. There is no IS-A relationship between a 2d- and a 3d-point. Deriving one from the other is a mistake.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So if 3d was not derived from 2d that downcasting wouldn't work, correct?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So if 3d was not derived from 2d that downcasting wouldn't work, correct?
    Ask yourself this: when does a cast work between two independent types?
    My best code is written with the delete key.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Ask yourself this: when does a cast work between two independent types?
    Till now I didn't know it works.

    I found this today:
    Code:
    public class A
    {
        public A() { }
    }
    
    public class B : A
    {
        public B() { }
    }
    The new class—the derived class—then gains all the non-private data and behavior of the base class in addition to any other data or behaviors it defines for itself. The new class then has two effective types: the type of the new class and the type of the class it inherits.

    In the example above, class B is effectively both B and A. When you access a B object, you can use the cast operation to convert it to an A object. The B object is not changed by the cast, but your view of the B object becomes restricted to A's data and behaviors. After casting a B to an A, that A can be cast back to a B. Not all instances of A can be cast to B—just those that are actually instances of B. If you access class B as a B type, you get both the class A and class B data and behaviors. The ability for an object to represent more than one type is called polymorphism. For more information, see Polymorphism (C# Programming Guide). For more information on casting, see Casting (C# Programming Guide).
    It brights.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make a if a char equals something
    By Megamanenm in forum C++ Programming
    Replies: 18
    Last Post: 04-15-2009, 05:02 AM
  2. Volume of a Cone Equation always equals 0
    By Devolution in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 03:13 AM
  3. don't know where to start
    By spikerotc04 in forum C Programming
    Replies: 5
    Last Post: 04-26-2005, 04:21 PM
  4. structure woes equals undesired output
    By skeptik in forum C Programming
    Replies: 3
    Last Post: 07-23-2004, 02:20 AM
  5. Why strcmp equals zero
    By barim in forum C Programming
    Replies: 5
    Last Post: 07-21-2004, 08:43 PM