0

Hi I have another question concerning style

I have a (parent) class with several virtual methods

 class parent
 {
     virtual void A() {}
     virtual void B() {}
     virtual void C() {}
 }

Then I have several child-classes

    class child1 : public parent 
    class child2 : public parent 
    class child3 : public parent 

When I do not need some of the methods ( like B and C in Child 2/Child3):

1. Would I write them in the .h/.cpp without filling the body of the method (like in Child 3)

2. or would I leave them out like in class child 2

    class child1 : public parent 
    { 
           virtual void A() {qDebug()<< "I am A of child 1" ; }
           virtual void B() {qDebug()<< "I am B of child 1" ;}
           virtual void C() {qDebug()<< "I am C of child 1" ;}
    }

    class child2 : public parent 
    {    
        virtual void A() {qDebug()<< "I am A of child 2" ; }
    }
    class child3 : public parent 
    {            
        virtual void A() {qDebug()<< "I am A of child 3" ; }
        virtual void B() {}
        virtual void C() {}
    }

Thank you

I know that you do not have to but is it good style to do so or would one leave them out for style-reasons.

user3443063
  • 1,455
  • 4
  • 23
  • 37
  • Now the edited question seems to me to attract opinion-based answers. – Steeve Feb 20 '17 at 11:21
  • 1
    You need to define your requirement of "*I do not need some of the methods*". Do you want to get a compile error when you call these methods? In this case, your question is a duplicate of [this question](http://stackoverflow.com/q/24609872). If you want the functions to just do nothing when they are called, you can override them to do nothing (the way you do it in `child3`). If the base implementations for these functions are documented to do what you want (do nothing in this case), and you are sure that this will never change later, then you can leave them out (the way you do it in `child2`). – Mike Feb 20 '17 at 11:38

0 Answers0