Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.

Similar presentations


Presentation on theme: "Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that."— Presentation transcript:

1 Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that can be used to instantiate objects are called concrete classes.

2 Shape Circle rectangle Triangle Two dim shape has to be something. There is no situation you will need to create object of shape Shape can be defined as abstract class, only used to define circle, rectangle, and triangle, etc.

3 person Employee Student fulltimeparttimegrad under A person must belong to some category, either employee or student in this application. Person class can be defined as abstract class.

4 Pure virtual function A class is made abstract by declaring one or more of its virtual functions to be “pure”. A pure virtual function is one with an initializer of =0 in its declaration virtual void print() const=0;

5 Pure virtual function normally do not provide implementations. Every concrete class must override all base-class pure virtual functions and provide concrete implementations of these functions.

6 Difference:virtual function and pure virtual function A virtual function has an implementation and gives the derived class the option of overriding the function A pure virtual function does not provide an implementation and requires the derived class to override the function (for that derived class to be concrete)

7 Virtual Destructors A problem can occur when dynamically allocate objects of class hierarchy. If an object with a nonvirtual destructor is destroyed explicitly by applying the delete operator to a base-class pointer to the object

8 Person *ptr; ptr = new employee(); lname hour dept ssn fname The memory allocated delete ptr; The memory deleted lname dept fname ssn

9 #include “person.h” int main() { person *ptr; ptr = new employee(); ptr = new student(); return 0; } employee constructor called student constructor called Person destructor called

10 Solution to this problem Declare the base-class destructor virtual. This makes all derived-class destructors virtual even though they do not have the same name as the base-class constructor. If an object in the hierarchy is destroyed by applying the delete operator to base-class pointer, the destructor for the appropriate class is called based on the object to which the base- class pointer points

11 class person { public: person (string l, string f, char C, int id); virtual void print()const; // other member functions virtual ~person(); private: // data members };


Download ppt "Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that."

Similar presentations


Ads by Google