virtual methods

Can anybody efficiently explain virtual methods ????
Found a load of examples here;
http://tinyurl.com/27f45pw
Last edited on
See the section about OOP:
http://cplusplus.com/doc/tutorial/
They exist virtually doing nothing.
When you call member functions, the compiler uses the type of the object to determine which class's member function to call.

However if the function is virtual, then the "actual" type of the object is used instead of the "apparent" type of the object.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Parent
{
public:
  void no();
  virtual void yes();
};

class Child : public Parent
{
public:
  void no();
  virtual void yes();
};


int main()
{
  Child obj;
  Parent* p = &obj;  // 'p' is a 'Parent' pointer, but it "actually" points to a 'Child'

  p->no();  // non-virtual function.  Calls Parent::no because 'p' "appears" to be
    // a 'Parent' (because it's a Parent pointer)

  p->yes();  // virtual funcion.  Calls Child::yes because 'p' "actually" points to a 'Child'
    // even though it's a Parent pointer
}
High!

Such a method leaves no option other than to begin an ultimate
ancestor, ultra abstract, A Prior with a long list of methods,
each of one them without a purpose except for a form of the verb as
its name. There may be parameters as well.
I never heard of anyone speaking of calling parent's virtual method
from a child.
... what?
Hello maurepm,

If you ever stumble on at least one virtual expression then
beware of the unexpected because such a class is under construction,
though virtual may point to no operation at all unlike pure virtual
function pointer.
Was any of the above sufficient?
ultimate ancestor,
ultra abstract
i somehow read that in the UT announcer voice
virtual may point to no operation at all


Um, no? virtual is required to be defined just as much as a pure virtual one, the only difference is that non-pure virtual means the base class has a definition that can be used.
Topic archived. No new replies allowed.