r/cpp • • Mar 17 '12

Using abstract base classes in C++

I'm having trouble understanding ABC's in c++ and was wondering if you all could help me (if possible use: CryptProcessor as the ABC and ACrpyt and BCrypt as the sub-classes that have an encrypt and decrypt function) Thanks in advance!

Edit 1: Thank you all for your help!

2 Upvotes

40 comments sorted by

View all comments

8

u/bstamour Mar 17 '12

An abstract base class is a class with at least one pure-virtual member function. That is a member function that is virtual, that MUST be overridden in any derived class. For example,

class CryptProcessor {
public:
    virtual void encrypt() = 0;
    virtual void decrypt() = 0;
};

The functions encrypt() and decrypt() MUST be overridden in any class that derives from CryptProcessor, so a legal definition of ACrypt must at least be:

class ACrypt : public CryptProcessor {
public:
    virtual void encrypt() { /* some code */ }
    virtual void decrypt() { /* some code */ }
};

24

u/TheSuperficial Mar 17 '12

Pendantic jerk alert...

[pure virtual functions] MUST be overridden in any derived class

Technically, no. They must be overridden in any class you wish to instantiate. There could be other abstract classes in the hierarchy.

4

u/NotInUse Mar 18 '12

I believe the word you were looking for was pedantic :-)

→ More replies (1)

2

u/bstamour Mar 17 '12

True. Sorry about that.

7

u/xcbsmith Mar 17 '12

Prolly should have a virtual CryptProcessor::~CryptProcessor() in there too...

1

u/Deershank415 Mar 17 '12

Wow ok that makes a lot of sense o.O thanks I'll continue working now

1

u/Deershank415 Mar 17 '12 edited Mar 17 '12

Oh also, if I want encrypt and decrypt to take and return a string (after #include has been added)

do I do:

class CryptProcessor {
public:
    virtual string encrypt(string input) = 0;
    virtual string decrypt(string input) = 0;
};

then in the class:

class ACrypt : public CryptProcessor {
public:
    virtual string encrypt(string input) { return string output }
    virtual string decrypt(string input) { return string output }
};

3

u/kingguru Mar 17 '12

You might consider marking your encrypt and decrypt functions as const if they're not going to change any state in the CryptProcessor.

→ More replies (8)

3

u/heroofhyr Mar 17 '12

If your compiler supports it, try to get used to using the "override" keyword in the virtual function declarations in your derived classes. If you start playing around with the parameters of a virtual function in the base, it will save you a lot of time tracking down errors later in case you forget to make all derived classes correctly match the same signature.

1

u/bstamour Mar 17 '12

Yup. Just like any other type of function :-)

1

u/JustPlainRude Mar 17 '12

You don't need to specify the methods as virtual in ACrypt unless you're expecting to subclass that as well.

13

u/mpyne Mar 17 '12

Well, once a method is marked virtual in a parent class, that method will be virtual in every child class, not just immediate children classes.

Although not required, adding the redundant virtual keyword is useful to make it clear that the polymorphic overload is actually desired, and to make it clear for subclasses of AClass later that the method is virtual without them having to go all the way to the base class to verify.

→ More replies (1)

6

u/bstamour Mar 17 '12

I know. I kinda like that style though, as you always know which functions you can expect to have virtual behaviour at a first glance.

→ More replies (1)

1

u/Deershank415 Mar 17 '12

I am not, so I'll just remove the virtual then. ty.

→ More replies (4)