Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Overridable

  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Overridable

    i'm allways seeing this term "overridable function".
    what does it mean?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Are you sure you didn't mean "overload"?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, i see it in VC++, MFC programming.

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you have a function in the base class and a function with the same signature in the derived class, the derived class function is said to "override" the base class function. For example,
    Code:
    class Base {
    public:
        void Test(int p1, char p2);
    };
    class Derived: public base {
    public:
        // Test() overrides Test() in base
        void Test(int p1, char p2);
        // An overloaded function
        void Test();
    };
    To ensure a function in the base class is overridden in the derived class, you should make the function in the base class a pure virtual function.

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what's meaning of virtual function?

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Virtual functions come in handy when you want a base class pointer to call derived class functions. Consider this example:

    Code:
    class Base {
    public:
        void Show() {
            cout << "Base::Show()";
        }
    };
    
    class Derived: public Base {
    public:
        void Show() {
            cout << "Derived::Show()";
        }
    };
    
    class Derived2: public Base {
    public:
        void Show() {
            cout << "Derived2::Show()";
        }
    };
    
    int main()
    {
        Derived *d = new Derived();
        Base *b = d;
    
        b->Show();	// Displays Base::Show()
    
        return 0;
    }
    By assigning the derived class object *d to the base class pointer b, you could now access the embedded base part in the derived object. I mean, using the pointer b you could call the members of the derived class object *d. But, you could access only the base class-specific members. Thus, in the example above, though b now points to an object of type derived, b->Show() displays "Base::Show()", whereas you would want "Derived::Show() to be displayed. By declaring the base class function Show() as virtual, b->Show() would display "Derived::Show()". You may wonder why you would want to use a base class pointer to point to a derived class object. Suppose you have many derived objects (Derived, and Derived2 in the example), you could use just the base class pointer to access any of the members of it's derived class objects! To make the base class function virtual in the example, declare Show() in Base as follows:

    Code:
    virtual void Show() {
    	cout << "Base::Show()";
    }
    virtual function is an example of polymorphism.


  •  

    Posting Permissions

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