Thread: upcasting

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    upcasting

    I do have a homework problem that I am having trouble with. I am new to programing, so I will explain this the best I can.

    ClassB is derrived from ClassA and has the same method names as ClassA. One is overriden and one is new. Class A has one virtual function that can be overriden.

    I want to set string example5 to the string in InstanceB.TestB. "TestB from ClassB" I have tried casting IntanceB to class B, but am not getting any where with that. I also have tried using the is and as operators and am getting errors. I keep getting the string equal to "TextB from ClassA".

    The classes are initiated the way they are supposed to be an I believe it has something to do with that.

    ClassA InstanceB = new ClassB();

    Any pointers in the right direction or help understanding why you would initiate InstanceB in such a way would be appreciated.


    Code:
    namespace TestIt
    {
        class Program
        {
    
        class ClassA
        {
            public virtual string TestA()
            {
                return "TestA from ClassA";
            }
    
            public string TestB()
            {
                return "TestB from ClassA";
            }
    
            public virtual string TestC()
            {
                return "TestC from ClassA";
            }
    
        }
    
        class ClassB : ClassA
        {
            public override string TestA()
            {
                return "TestA from ClassB";
            }
    
            new public string TestB()
            {
                return "TestB from ClassB";
            }
        }
    
    
            static void Main(string[] args)
            {
                ClassA InstanceA = new ClassA();
                ClassA InstanceB = new ClassB();
                ClassB InstanceC = new ClassB();
    
                object p = InstanceC;
                InstanceB = p as ClassB;
    
                string example5 = InstanceB.TestB();
    
           }
        }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    string example5 = (InstanceB as ClassB).TestB();
    I believe InstanceB was initiated the way it was to demonstrate an object-oriented feature. Even through InstanceB is declared as a ClassA, it can hold a reference to a ClassB object because ClassB derives from ClassA.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    That does work.

    I tried to call TestA from ClassA from instanceB using this code and it does not work. Is it due to methodA of classB overriding A?

    Code:
    string example6 = (InstanceB as ClassA).TestA();
    However you can use a cast and it can work as bellow, but just curious why this does not work in the above example.

    Code:
    object o = InstanceA;
    InstanceB = (ClassA)o;
    
    string example7 = InstanceB.TestA();
    This calls ClassA.TestA.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Correct, that is because methodA is overridden.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upcasting of templatized types.
    By mtbusche in forum C++ Programming
    Replies: 11
    Last Post: 05-26-2009, 03:14 PM
  2. Upcasting and identifying objects by address
    By New++ in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2005, 05:47 PM