An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
Virtual Function:C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class. The concept of c++ virtual functions is different from C++ Function overloading.
-----------------------the codes for example of virtual class we made---------------------------
/* #include
using namespace std ;
//Monster class with pure virtual functions class Monster{
public:
//we declare a pure virtual function by using a pure specifier (= 0)
virtual void KeyGuardian() = 0; // A function declaration cannot have both a pure specifier and a definition. Example: virtual void KeyGuardian() = 0;
virtual void DaBoss() = 0;
};
//One of the derived classesclass Minion:public Monster{
public:
//virtual keyword need not to be repeated in the derived class virtual void KeyGuardian();//can also be named as just void KeyGuardian();
virtual void DaBoss();
};
//2nd derived classclass BoneFiend:public Monster{
public:
virtual void KeyGuardian() ;
virtual void DaBoss();
};
void Minion::KeyGuardian(){
cout << "Minion says hello in keyguardian mode " <<>DaBoss();
M1[0]->KeyGuardian();
M1[1]->DaBoss();
M1[1]->KeyGuardian();
//this way of definition can actually allow us to squeeze all the information of the monsters created into a single array
}
---------------------------------------------------(END of codes)------------------------------------------------
*/
reading the comments will help u understand the codes.
What will be shown below will be the sample output of those codes.
-----------------------The Sample Output for these codes----------------------------
Minion says goodbye in DaBoss modeMinion says hello in keyguardian modeBoneFiend says GoodBye in DaBoss modeBoneFiend says hello in keyguardian modePress any key to continue
---------------------------------------------------(END of Sample Output)------------------------------------------
The use of this virtual function is to be able to compile all the Monsters information into one single array for easier access for the information of these monsters.
Information about abstract classes and virtual functions are gotten for making this source code is from:
1)http://www.codersource.net/cpp_virtual_functions.html
2)http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr142.htm
3)And from the helpful lecturers ^^
To download the example of the source codes:
http://www.geocities.com/slhjlim321/lala.zip
No comments:
Post a Comment