CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: real world polymorphism

    We responded at the same time, but don't use 'new' keyword in your derived class implementations. Use 'override' instead.

    'new' hides the derived class' implementation. If you use 'new', then you can't do:

    Human human = new Superhero();

    You can, but then all human.Member calls will be calling Human versions instead of Superhero versions.

    You must do:

    Superhero human = new Superhero();

    If you want to use the 'new' version of each implemented member.

    The easy solution is to change 'new' to 'override' in your derived classes.

    Then you can do:

    Human human = new Superhero();

    And human.Method will call the Superhero() overriden version instead of the base Human version.

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: real world polymorphism

    Quote Originally Posted by sidhu688 View Post
    ok so here is the program parts and the questions





    protected new int power = 50; why the new keyword is used here ?


    public int HumanPower { get { return base.power; } } // will this return the power of the base class?


    public new int Power { get { return this.power; } } // is this used for returning power of the current object ?




    Console.WriteLine("is human a human? " + human is Human);

    Everything is coming false when i am running this code and a green line is coming below these and says the given expression is never of the provide ('oops_Concepts.Human) type ?



    Console.WriteLine("memloo's human power: " + ((Human)memeloo).Power);

    ((Human).memeloo) what does this mean ?
    Have you tried running the code? If not, try running the code and think about the output that you see. It might answer some of your questions.

  3. #18
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: real world polymorphism

    Quote Originally Posted by sidhu688 View Post
    public int HumanPower { get { return base.power; } } // will this return the power of the base class?

    public new int Power { get { return this.power; } } // is this used for returning power of the current object ?
    modify the code, run it and see it for yourself.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #19
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: real world polymorphism

    Quote Originally Posted by mariocatch View Post
    We responded at the same time, but don't use 'new' keyword in your derived class implementations. Use 'override' instead.
    you're right. it was stupid here to use the "new". I'll correct it right away.

    edit: corrected
    Last edited by memeloo; February 5th, 2010 at 04:23 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured