The concept behind the “upcast”.

Discussion in 'C' started by d_arin100, Sep 30, 2009.

  1. d_arin100

    d_arin100 New Member

    Joined:
    Sep 25, 2009
    Messages:
    10
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Bangalore

    Introduction



    The aim of this article is to explain the upcasting. Before going through this article I expect people have some preliminary idea about inheritance mechanism.

    Background



    The term upcasting comes from the class inheritance diagram. In the class inheritance diagram we generally draw the base class in the top and the derived class grows downward.
    Code:
     
    --------------------
    |       Base       |
    --------------------
              |
    --------------------
    |      Derived     |
    -------------------- 
    
    Since the casting from derived class to base class moves upwardin the inheritance diagram, so it is generally referred to as upcasting. The compiler allows upcasting because this type of cast is from specific type to a more generic type. Upcasting is safe but one thing that can happen for this upcast is object slicing (Lose of data and member function) .

    The code



    Code: Cpp
    Code:
    #include <iostream>
    using namespace std;
     
    class Base 
    {
    public:
     void Information()
     {
     cout << "We are in Base Information" <<endl;
     }
    };
     
    class Derived : public Base 
    {
     void Information() 
     {
     cout << "We are in Derived Information" <<endl;
     }
    };
     
    void Generic(Base &base) 
    { 
     base.Information();
    }
     
    int main() {
     Derived derived;
     Generic(derived); /// Upcasting
     return 0;
    }
    
    Prevent object slicing in pass by value mechanism
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. kilstima

    kilstima New Member

    Joined:
    Oct 19, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Out of the many posts, this one attract my attention. I believe it is possible for anyone to participate.
    Excellent ! I like it very much.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. bibtrita

    bibtrita New Member

    Joined:
    Nov 3, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    15 minutes till showtime!
     
  6. sulscrishri

    sulscrishri New Member

    Joined:
    Nov 4, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Good work !
    Very cool, looking great so far. Keep going, I wanna see it finished!:surprised
     
  7. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Hello, this is great article. I have blog and I thanks to say you thanks. Regards!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice