Use Polymorphism

Use Polymorphism: Programs, especially those converted from non-OOP languages like C, sometimes use state where they should use inheritance. For example,

1225_polymorphism1.jpg

The variable type is used to explicitly store the type of an instance of class A or B. It is initialized in the constructors of both classes and is used in the A::foo() method to determine the exact type of the object so that the appropriate operations are carried out. Besides being error prone (type initialization, branch completeness) and breaking fundamental object-oriented abstraction rules (base classes should not have to know about their concrete derived classes), this produces more code (the branches) and more data (type fields) that have to be handled by the model checker. You can use the inheritance mechanism in C++ to do this more elegantly, as shown below:

856_polymorphism2.jpg

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.