B.TECH CSE BT ECE(oop's): Abstract Classes in C++
Sometimes implementation of all function cannot be provided in a base
class because we don’t know the implementation. Such a class is called
abstract class. For example, let Shape be a base class. We cannot
provide implementation of function draw() in Shape, but we know every
derived class must have implementation of draw(). Similarly an Animal
class doesn’t have implementation of move() (assuming that all animals
move), but all animals must know how to move. We cannot create objects
of abstract classes.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.
#include<iostream> using namespace std; class Base { int x; public: virtual void fun() = 0; int getX() { return x; } }; // This class inherits from Base and implements fun() class Derived: public Base { int y; public: void fun() { cout << "fun() called"; } }; int main(void) { Derived d; d.fun(); return 0; }
Comments
Post a Comment