Virtual Functions
Virtual Functions
BIT-008-0043/2009
BIT 2203 ASSIGNMENT III
A: define
(i)Abstract class
In C++, a class is referred to as abstract if it serves (only) as a base or parent for other
classes. That is, you cannot declare a variable of that class nor can you use it as you
would a regular class. To create an abstract class in C++, at least one of its methods must
be pure virtual. A pure virtual method is one that is not implemented or defined in that
class. It is created using = 0; on its right side when declaring it.
(ii)Virtual method
This is a function or method whose behavior can be overridden within an inheriting class
by a function with the same signature.
class student
{
public:
virtual void read() const { std::cout << "I like reading." << std::endl;
}
virtual ~ student() {}
};
};
int main()
{
std::vector< student*> student;
student.push_back( new student() );
student.push_back( new undergraduate () );
student.push_back( new postgraduate () );
student.push_back( new Otherstudent() );
return 0;
}