17

Just wanted some clarification. Should abstract base classes never have private members? For example

class abc{
public:
  virtual void foo()=0;
private:
  int myInt;
}

you can never access myInt since you cannot create an instance of abc and it will not be in a derived class since its private. Is there any situation where you would use private members in abstract base classes or is this just wrong?

valmo
  • 1,156
  • 2
  • 12
  • 22
  • A private member like that doesn't make sense in an abstract class, just as it doesn't make sense in *any other* class. – R. Martinho Fernandes Apr 06 '11 at 15:46
  • @MartinhoFernandes: Are you saying that private data members never make sense or are you saying something about `int` or `myInt` in particular? – CB Bailey Apr 06 '11 at 15:57
  • I'm saying that private data members that are not used *anywhere* do not make sense. `class abc{ public: virtual void foo(){}; /*not abstract anymore*/ private: int myInt;` does not make myInt any more useful. – R. Martinho Fernandes Apr 06 '11 at 16:00
  • @Charles: no it might not. `foo` has no implementation because it's pure virtual. – R. Martinho Fernandes Apr 06 '11 at 16:31
  • @Martinho Fernandes There's no rule that says an abstract method can't have an implementation. – Mark B Apr 06 '11 at 16:58
  • @Mark: thanks. I didn't knew that. In that case, how would it differ from a regular virtual method? – R. Martinho Fernandes Apr 06 '11 at 17:00
  • @Martinho Fernandes You *still* have to override it in all child classes. The implementation of the pure method can provide some additional parent-level behavior. – Mark B Apr 06 '11 at 17:07
  • @MarkB: I know this is probably what you meant, but just to be more stricly correct: the final overriding function for any virtual function must not be pure virtual for any class for which you instantiate an object. Intermediate classes in a hierarchy can leave a pure virtual function un-overriden. – CB Bailey Apr 06 '11 at 20:51

6 Answers6

17

In C++ you can have an abstract class that has non pure virtual methods. In that case, and depending on the design it can make sense to have private members:

class base {
   std::string name;
public:
   base( std::string const & n ) : name(n) {}
   std::string const & getName() const { return name; }

   virtual void foo() = 0;
};

That code ensures that every object that derives from base has a name, that is set during construction and never changes during the lifetime of the object.

EDIT: For completion after Charles Bailey reminded me of it in his answer

You can also define pure-virtual functions, and in that case, private attributes could also make sense:

// with the above definition of base
void base::foo() {
   std::cout << "My name is " << name << std::endl;
}
Community
  • 1
  • 1
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
3

It's normally not advisable to have data members in an abstract class but there is nothing technically wrong with your example. In the implementation of foo, which is publicly accessible you can use myInt for whatever purposes you like.

For example:

class abc{
public:
  virtual void foo()=0;
private:
  int myInt;
};

class xyz : public abc
{
    virtual void foo();
};

#include <iostream>
#include <ostream>

void xyz::foo()
{
    std::cout << "xyz::foo()\n";
    abc::foo();
}

void abc::foo()
{
    std::cout << "abc::foo(): " << myInt++ << '\n';
}

#include <memory>

int main()
{
    std::auto_ptr<abc> p( new xyz() ); // value-initialization important
    p->foo();
    p->foo();
}

Output:

xyz::foo()
abc::foo(): 0
xyz::foo()
abc::foo(): 1
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

Not all methods in an abstract base class must be pure virtual. You might have some methods which are useful to all subclasses. Thus if you have some functionality in the base class which is modifying internal state you would have private members for those.

Justin Waugh
  • 3,975
  • 1
  • 21
  • 14
1

If you use the Template Method design pattern (to implement the open/closed principle), it is quite common to have private members of an abstract base class.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
0

As it stands, your example makes no sense.

However, abstract base classes are allowed to have member function definitions, which in turn are allowed to access private member data in the base class.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
-7

You can access Private Members through this shortcut way

Code is in PHP

abstract class myclass1 
{ 
           private $var="46789";
       public function test($valuetoset)
       {
            echo $this->var = $valuetoset;

       } 
} 

class myclass2 extends myclass1
{ 


} 

$obj = new myclass2();
$obj->test(78);
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
hardik
  • 9,141
  • 7
  • 32
  • 48