...

View Full Version : Overridable



Aymen++
12-30-2002, 07:34 PM
i'm allways seeing this term "overridable function".
what does it mean?

Spookster
12-31-2002, 01:38 AM
Are you sure you didn't mean "overload"?

Aymen++
12-31-2002, 07:30 PM
yes, i see it in VC++, MFC programming.

codefox
01-01-2003, 11:12 AM
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,


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.

Aymen++
01-01-2003, 07:53 PM
what's meaning of virtual function?

codefox
01-02-2003, 06:06 AM
Virtual functions come in handy when you want a base class pointer to call derived class functions. Consider this example:



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:



virtual void Show() {
cout << "Base::Show()";
}


virtual function is an example of polymorphism.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum