c++ - Any design pattern to apply same operations to all the derived objects -


I have a scenario in which I am thinking that if I can apply a design pattern. The scenario is like this: There are 2 derived classes in a base class and in the main function, we need the same operation on both the derivative classes. I want it in C ++

For example:

  class base {virtual bund DoWeHaveToPerformOperation () = 0; Virtual Zero Display Operation () = 0; }; Class Derived1: Public Base {bool DoWeHaveToPerformOperation (); Zero performance operation (); }; Class Derived2: Public Base {bool DoWeHaveToPerformOperation (); Zero performance operation (); }; int main () {Derived1 d1; If (D1) Dovohettopar operation ()) {d1. Perform Operation (); } Derived2D2; If (d2.DoWeHaveToPerformOperation ()) {d2.PerformOperation (); }}   

Instead of typing in the main as above, I am thinking that there is something that I can customize the code (or if any pattern I can think of moving forward the normal code at least in a different function and it is

  checkonrapet (base * b) {if (b -> gt;; DoWeHaveToPerformOperation ()) {B-> Display Operation (); }}   

and it is called both for derivative object .. but I think it can still be optimized ..

  int main () {base * b1 = new derived1 (); CheckAndOperate (B1); Base * b2 = new derivative 2 (); CheckAndOperate (B2); Delete B1; Remove B2; }   

Any suggestions please

Patterns usually deals with this type of thing.

  class base {public: zero performance operation (if (doewaterform operation ()) {duperform operation ()); }} Protected: Virtual Bund DoWeHaveToPerformOperation () = 0; Virtual Zero DoPerformOperation () = 0; }; Class Derived1: Public Base {bool DoWeHaveToPerformOperation (); Zero DoPerformOperation (); }; Class Derived2: Public Base {bool DoWeHaveToPerformOperation (); Zero DoPerformOperation (); }; Int main () {Derived1 d1; d1.PerformOperation (); Derived2D2; d2.PerformOperation (); Return 0; }    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -