Shaki wrote:
Also virtuals are used in polymorphism, as if you have something with a non-true type you can set a function as virtual so that the current type will always run the function.
an example of a virtual is this:
Code: Select all
class Mother{
public:
Mother();
~Mother();
void doStuff();
};
class Child{
public:
Child();
~Child();
virtual void doStuff();
};
Child * kid = new Child;
Mother * mother = new Mother;
Mother * kid2 = new Child;
This means that the
true type is more important now.
So even though its a 'Mother' pointer, it's value is child, and it will run the child's
virtual function over the mother function..
If that made sense :p.
Using some of the situation from above it goes like this.
class Mother
{
public:
void breathing();
void sleeping();
void eating();
virtual void urinating();
};
class Child : public Mother
{
void urinating();
};
Mother and child would have the same functions and the difference would be breathing, eating and sleeping would be carried out the same for both the only differences would be the members ( sizeofbite, LengthOfSleep, or BreathsPerMin ). Whereas, urinating, where if child is a male and of course mother being female the process to urinate is different, but they both urinate.
The project I am working on with cameron, another user on this forum, he has setup his project so that he defined a base class that does the bulk of the work, and there are virtual functions for just a few different class type ( derivative classes ). The virtual functions are listed in the base class, and the altered functions go in the derivative ( child ) classes is what I really was trying to get at.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com