-2

in c# program below value of baseclass deffers from derived class same variable which is inherited, baseclass is abstract, but variable is declared in baseclass only. Here is the C# console program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestApplication
{
    class MainClass
    {
        abstract class CBaseClass
        {
            public int iCount;

            public CBaseClass()
            {
                Console.WriteLine("CBaseClass created");
                iCount = 30;
            }

            public virtual void Add() { iCount += 20; }
            public virtual void Add(int iAdd) { iCount += iAdd; }
            public void Subtract() { iCount -= 20; }
            public abstract void Subtract(int iSubtract);
            public override string ToString()
            {
                return base.ToString() + ".. Method of BaseClass";
            }
            ~CBaseClass() { Console.WriteLine("CBaseClass deleted"); }
        };

        class CDerivedClass : CBaseClass 
        {
            public CDerivedClass() 
            { 
                Console.WriteLine(" CDerivedClass created.");
                iCount = 50;
            }

            public override void Add()
            {
                iCount += 20;
            }

            public void Add(int iAdd)
            { 

            iCount  += iAdd;
            }

            public void Subtract() 
            {
                iCount -= 40;
            }

            public sealed override void Subtract(int iSubtract)
            {
                iCount -= 10;
            }
            ~CDerivedClass() { Console.WriteLine("CDerivedClass deleted \n"); }

      };


        static void Main(string[] args)
        {
            CDerivedClass deriveObject = new CDerivedClass();
            CBaseClass basePointer = new CDerivedClass();
            CDerivedClass notUsed;

            Console.WriteLine("1:" + deriveObject.iCount);
            deriveObject.Add();
            Console.WriteLine("2:" + deriveObject.iCount);
            basePointer.Add(30);
            Console.WriteLine("3:" + basePointer.iCount);
            basePointer.Subtract();
            Console.WriteLine("4:" + basePointer.iCount);
            basePointer.Subtract(20);
            Console.WriteLine("5:" + basePointer.iCount);
            Console.WriteLine("6:{0}",basePointer);
            Console.ReadLine();

        }
    }
}

Here is the output:

CBaseClass created
CDerivedClass created.

CBaseClass created
CDerivedClass created.
1:50
2:70
3:80
4:60
5:50
6:TestApplication.MainClass+CDerivedClass..Method of BaseClass

here in above output i can't understand .. when we call base class method add(30).

basePointer.Add(30); When we call this method... before that value of iCount is 70. but after this its becoming 50. how ? i expect output of 3: 100

PATIL DADA
  • 379
  • 1
  • 4
  • 12
  • which values did you expect? – thumbmunkeys Aug 16 '14 at 18:30
  • basePointer.Add(30); When we call this method... before that value of iCount is 70. but after this its becoming 50. how ? – PATIL DADA Aug 16 '14 at 18:31
  • 1
    I'm not exactly sure what you're asking, but `iCount` is an instance field, meaning it is not shared between `deriveObject` and `basePointer` since they are two separate objects. – Andrew Whitaker Aug 16 '14 at 18:36
  • Related: http://stackoverflow.com/q/25311126/497356 – Andrew Whitaker Aug 16 '14 at 18:37
  • can you get rid of all that whitespace and other clutter like the destructors. I think you simply forgot the `override` in some of your subclass methods – thumbmunkeys Aug 16 '14 at 18:37
  • then how basePointer.iCount is 50 before this basePointer.Add(30) method – PATIL DADA Aug 16 '14 at 18:39
  • I also think it's noteworthy that your `derivedObject` and `basePointer` are the same type of object, they're both of type CDerivedClass`. So they're going to behave identically. That's fine, but I think this example is illustrating a bit of a misunderstanding of the fundamentals of polymorphism. – Matthew Haugen Aug 16 '14 at 18:40
  • i expect output of 3: 100 – PATIL DADA Aug 16 '14 at 18:41
  • `CBaseClass` is never instantiated in the code. Its constructor is called from a subclass. – Andrew Whitaker Aug 16 '14 at 18:44
  • @AndrewWhitaker - I see that now, the code is somewhat confusing ;) – Erik Funkenbusch Aug 16 '14 at 18:45
  • every thing was going fine in my interview i was in 2nd last round then... they asked me o/p of this sh88t. plz help me erik and matthew – PATIL DADA Aug 16 '14 at 18:49
  • This code is way more complex than it needs to be.. For instance, Add does the same thing in base and derived.. there is no reason to override it. – Erik Funkenbusch Aug 16 '14 at 18:50
  • I think you should review Andrew's answer below, but also just remove one of the variables all together. Run your tests with only one of them present, then run the tests with the other. That might clean up some of what's confusing you. Because it's pretty difficult for us to comprehend what you're trying for right now. – Matthew Haugen Aug 16 '14 at 18:51
  • thanks matthew, eric – PATIL DADA Aug 16 '14 at 19:02

1 Answers1

2

Let's step through the code line-by-line to see what's happening to each object's iCount field:

CDerivedClass deriveObject = new CDerivedClass();
CBaseClass basePointer = new CDerivedClass();
CDerivedClass notUsed;

// deriveObject.iCount == 50
// basePointer.iCount == 50

deriveObject and basePointer both have iCount initialized to 50 because of the way constructors are called on derived objects in C#.

In this case:

  • The base class constructor is called, first setting the `iCount` value to 30 and printing the base class message.
  • Next, the subclass constructor is called, setting `iCount` to 50 (and overwriting the 30).
Console.WriteLine("1:" + deriveObject.iCount); // 1:50
deriveObject.Add();
// CDerivedClass.Add is called on deriveObject
// deriveObject.iCount == 70

Console.WriteLine("2:" + deriveObject.iCount); // 2:70
basePointer.Add(30);
// CDerivedClass.Add is called on basePointer
// basePointer.iCount == 80

Console.WriteLine("3:" + basePointer.iCount); // 3:80
basePointer.Subtract();
// CBaseClass.Subtract is called on basePointer, since `CDerivedClass` does not
// override the Subtract method.
// basePointer.iCount == 60

Console.WriteLine("4:" + basePointer.iCount); // 4:60
basePointer.Subtract(20);
// CDerivedClass.Subtract is called on basePointer. *Note* that this subtracts 10.
// basePointer.iCount == 50

Console.WriteLine("5:" + basePointer.iCount); // 5:50
Console.WriteLine("6:{0}",basePointer);

Console.ReadLine();

deriveObject and basePointer reference two different instances of CDerivedClass.

The only tricky part is with the call to basePointer.Subtract() ("3"). Since CDerivedClass does not mark its Subtract method as "override", the base class' method is called.

Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307