E N D
OOP Abstract Classes and Concrete Derived Classes
Ealy and late binding • Binding means matching the function call with the correct function definition by the compiler. It takes place either at compile time or at runtime • In early binding, the compiler matches the function call with the correct function definition at compile time. It is also known as Static Binding or Compile-time Binding • We have learned about function overriding in which the base and derived classes have functions with the same name, parameters and return type. In that case also, early binding takes place
Early binding #include <iostream> using namespace std; class Animals { public: void sound() { cout << "This is parent class" << endl; } };
class Dogs : public Animals { public: void sound() { cout << "Dogs bark" << endl; } }; int main() { Animals *a; Dogs d; a= &d; a -> sound(); // early binding return 0; } On calling the function sound() which is present in both the classes by the pointer 'a', the function of the parent class got called
Late binding • In the case of late binding, the compiler matches the function call with the correct function definition at runtime. It is also known as Dynamic Binding or Runtime Binding. • In late binding, the compiler identifies the type of object at runtime and then matches the function call with the correct function definition • This can be achieved by declaring a virtual function
Virtual function • Virtual Function is a member function of the base class which is overridden in the derived class. The compiler performs late binding on this function. • To make a function virtual, we write the keyword virtual before the function definition virtual void sound() { cout << "This is parent class" << endl; }
Pure Virtual Function • Pure virtual function is a virtual function which has no definition. Pure virtual functions are also called abstract functions. • To create a pure virtual function, we assign a value 0 to the function as follows virtual void sound() = 0;
Abstract Class • An abstract class is a class whose instances (objects) can't be made. We can only make objects of its subclass (if they are not abstract). Abstract class is also known as abstract base class. • An abstract class has at least one abstract function (pure virtual function) • Subclasses of an abstract base class must define the abstract method, otherwise, they will also become abstract classes • In an abstract class, we can also have other functions and variables apart from pure virtual function
Concrete derive class • A concrete class is a subclass of an abstract class, which implements all its abstract method • The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share