c++ - How can I avoid code repetition without losing the interfaces? -


below current code design (sample). how can avoid repetition of code "methodparent()" (in implementation of both child classes) without losing interface classes?

//interfaces //=======================================================  class interfaceparent() //interface class  {    public:      virtual void methodparent() = 0;  };   class interfacechild1() : public interfaceparent //interface class  {     public:      virtual void methodparent() = 0;       virtual void methodchild1() = 0;  };   class interfacechild2() : public interfaceparent //interface class  {     public:     virtual void methodparent() = 0;      virtual void methodchild2() = 0; };   // concrete classes //=========================================================   class child1() : public interfacechild1  // concrete class  {     public:      void methodparent() { cout << "parent_method";  }       void methodchild1() { cout << "child_1_method"; }  };  class child2() : public interfacechild2 // concrete class {     public:       void methodparent() { cout << "parent_method";  }        void methodchild2() { cout << "child_2_method"; }  };  

thanks in advance !

santosh

if see "methodparent()", need implement in child classes , lots of repetition of code (as maintenance hurdle) me.

then implement abstract class instead of interface. it's interface (containing pure virtual methods need implemented in childrens) containing "non-pure" (already implemented) virtual methods, can overriden later if needed.

in general abstract class used define implementation , intended inherited concrete classes. more here

i this:

// abstract class aparent() //abstract class  {    public:     virtual void methodparent() { ... }; // give first implementation can overriden later on, if needed     virtual void methodechild() = 0  };  //now concretes class child1() : public aparent  {     public:      virtual void methodparent() { ... }; // override (as example, if needed)      virtual void methodchild() { ... }; //implement };   class interfacechild() : public aparent  {     public:     //void methodparent() // inherited aparent     virtual void methodchild() { ... }; // implement }; 

edit if can't change go this:

but... it's ugly :)

//interfaces //=======================================================  class interfaceparent() //interface class  { public:  virtual void methodparent() = 0;     };  class interfacechild1() : public interfaceparent //interface class  { public:  virtual void methodparent() = 0;   virtual void methodchild1() = 0;  };  class interfacechild2() : public interfaceparent //interface class  { public: virtual void methodparent() = 0;  virtual void methodchild2() = 0; };   //abstract //======================================================= //an abstract class transition betwin interfaceschildxx , concrete classes class achildxx() : public interfacechildxx  // concrete class { public:  virtual void methodparent() { cout << "parent_method";  } //it's implemented here childrens, can still overriden  virtual void methodchildxx() = 0;  };  // concrete classes //=========================================================  class child1() : public achildxx  // concrete class { public:  //void methodparent() { cout << "parent_method";  } //it's inherited  void methodchild1() { cout << "child_1_method"; }  };  class child2() : public achildxx // concrete class { public:   // void methodparent() { cout << "parent_method";  } //it's inherited   void methodchild2() { cout << "child_2_method"; }  };  

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -