Bewildered!!

Discussion in 'C++' started by obsynthnode, May 12, 2011.

  1. obsynthnode

    obsynthnode New Member

    Joined:
    May 12, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello everyone i am a beginner with C++ and have a very basic grasp of the language! If anyone could help explain the concept of polymorphism, i am having a hard time especially with dynamic polymorphism and the virtual and abstract functions.

    Below is a basic program using the virtual function:
    Code:
    #include<iostream.h>
    #include<conio.h>
    class c1
    {
    public:
       virtual  void disp()
        {
          cout<<"hello";
          }
     };
     class c2:public c1
     {
     public:
        void disp()
        {
        cout<<"well";
        }
    };
    int main()
    {
    clrscr();
    c1 obj;
    obj.disp();
    
    getch();
    return 0;
    }
    
    Declaring classes and getting them to display is understandable, my question is how does the virtual function work in this case? My understanding is that the virtual function stops the second class from inheriting the value of the first class. How does this make the program more effecient? It does not produce any palpable results that i can define. I know that it helps to streamline the runtime right? But beyond that i am flabbergasted! Please if anyone would be so gracious and come to my rescue it would be much appreciated!!

    Norman
     

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