Abstract Class Abstract class A base class that
Abstract Class
Abstract class A base class that defines only a generalized form of a function, that will be shared by all of its derived classes. Each derived class to fill in the details of the function which defined in the base class. One way this situation can occur is when a base class is unable to create a meaningful implementation for a function. So, cannot create an object of the base class This type of classes are called Abstract Class 2
Pure virtual function • A pure virtual function is a virtual function in base class that has no dentition. • A pure virtual function is declared using the specifier “= 0”. • Only a virtual member function can be pure. • virtual void show() = 0; • Declaring a virtual function pure is not the same as defining a virtual function with an empty body. • class Base{ • public: • virtual void show(){} // virtual but not pure • }; 3
Pure virtual function A pure virtual function can be declared by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration. class A { public: virtual void fun() = 0; // pure virtual function }; class B: public A { public: void fun() // pure virtual function implimentation { …. …… } }; 4
Abstract class • A class that has a pure virtual function is an abstract class. • Abstract class is used as an interface for its derived classes. • If a class derived from an abstract class, and this class doesn't override all the pure virtual function in the base class, then this class is also an abstract class. • No object can be created for an abstract class 5
Abstract class 6
- Slides: 6