Page 1 of 1

Good C++ OOP Book

Posted: Sun Feb 03, 2013 9:25 pm
by CampinCarl
Does anyone know a good book for object-oriented programming for C++? In terms of high level languages I come from a Java background, but currently doing dev in C/C++. Having trouble trying to draw parallels between the two, so looking for a good book to explain how C++ handles OOP.

Re: Good C++ OOP Book

Posted: Sun Feb 03, 2013 10:42 pm
by Flying Fox
OOP is OOP, you still deal with classes, inheritance, interfaces, etc. The concepts stay roughly the same. C++ is somewhat not the best expressive language for those concepts. A lot of those C++ OOP books don't really teach you what you need but either just an overview of language features (may not even be related to OO), or way more into OO design.

What kind of parallels are you looking for?

Re: Good C++ OOP Book

Posted: Sun Feb 03, 2013 10:49 pm
by just brew it!
The biggest differences are that C++ implements multiple inheritance (which, quite frankly, tends to obfuscate code unless used sparingly), and has a lot of excess baggage that isn't object oriented since it is essentially a superset of C (and inherits all of C's low-level features like pointers, explicit memory management, etc.)

I've always kind of viewed Java as a cleaned up derivative of C++.

I guess I'm with Flying Fox on this -- I'm not sure what sort of "parallels" you mean.

Re: Good C++ OOP Book

Posted: Sun Feb 03, 2013 10:52 pm
by CampinCarl
Well, mostly with trying to deal with class design/structure, class inheritance hierarchies, etc.. For instance, it doesn't seem like there is any existing language support for a generic interface class. How is that accomplished in CPP? Some sort of strange virtual class or something? Or not at all?

Plus other questions I can't think of off the top of my head.

Re: Good C++ OOP Book

Posted: Sun Feb 03, 2013 11:05 pm
by just brew it!
Any class that declares one or more "pure virtual" member functions is considered to be an abstract base class, and cannot be instantiated directly. The syntax for declaring a pure virtual member function is:

virtual <function prototype> = 0;

Re: Good C++ OOP Book

Posted: Sun Feb 03, 2013 11:18 pm
by Flying Fox
CampinCarl wrote:
Well, mostly with trying to deal with class design/structure, class inheritance hierarchies, etc.. For instance, it doesn't seem like there is any existing language support for a generic interface class. How is that accomplished in CPP? Some sort of strange virtual class or something? Or not at all?

That one is easy. Don't need a book. ;) Because C++ does not really have features for a real interface, you just need a virtual destructor to make sure that this "class" gets cleaned up (you do need to declare the destructors virtual on any derived classes, especially when you have >2 levels of inheritance after the "interface").
class SomeInterface
{
    virtual ~SomeInterface() {}

    // methods
    virtual void Foo(int someIndex) = 0;
    virtual in Bar() = 0;

    // you have to roll your own setter/getter
    virtual void Enabled(const bool& flag) = 0;
    virtual bool Enabled() = 0;  // forgot offhand if you can declare this function const :P
};

class RealClass : public SomeOtherClass, public SomeInterface, public SomeOtherInterface
{
public:
    RealClass() : _enabled(false) {}
    virtual ~RealClass() {}  // pretty much should declare your destructors virtual more often than not

    // SomeInterface implementation
    virtual void Foo(int someIndex) { /* ... */ }
    virtual in Bar() { /* ... */ }
    virtual void Enabled(const bool& flag) { _enabled = flag; }
    virtual bool Enabled() { return _enabled; }

    // implement other overrides and the virtual=0 functions

private:
    bool _enabled;    // store the property
};


If you want to stay pure OO, you just sort of follow the above. Now you can start "cheating" a little bit by refactoring implementations into some "intermediary" base class. Like in the above example, the "property" pretty much will be the same throughout your hierarchy, you can either use an intermediate derived class to put the implementation there, or just put into the "interface" class (the cheating part). A lot of people tend to put the prefix "I" in front of the class name to denote an interface, but the notation is somewhat religious that depends on who you talk to.

I think I start to see what you need. C++ Coding Standards is what I recommend, but it covers more than the OO side of things, but (almost) all of C++. However, for class design/structure and inheritance hierarchies you should still use regular OO principles and design patterns. These concepts should remain more or less the same across languages.

Edit: PS: Coming from Java, you will need to read up on the RAII pattern (no one link I would recommend, go read up on what you can find from your favourite search engine), but it does not really have to do with high level OO.

Re: Good C++ OOP Book

Posted: Mon Feb 04, 2013 5:50 pm
by Buub
just brew it! wrote:
The biggest differences are that C++... is essentially a superset of C (and inherits all of C's low-level features like pointers, explicit memory management, etc.)

I strongly disagree. It may have started that way, but modern C++ is a very rich language all its own, and only works best when you move away from C paradigms. Logically, I feel it's far more useful to consider C++ a deep object-oriented language with very good C compatibility.

Re: Good C++ OOP Book

Posted: Mon Feb 04, 2013 5:51 pm
by Captain Ned
Hold on while I e-mail ESR and ask if he'll officiate this bout.

Re: Good C++ OOP Book

Posted: Mon Feb 04, 2013 5:54 pm
by just brew it!
Buub wrote:
just brew it! wrote:
The biggest differences are that C++... is essentially a superset of C (and inherits all of C's low-level features like pointers, explicit memory management, etc.)

I strongly disagree. It may have started that way, but modern C++ is a very rich language all its own, and only works best when you move away from C paradigms. Logically, I feel it's far more useful to consider C++ a deep object-oriented language with very good C compatibility.

Yes, I agree that is how it is *intended* to be used. But in the real world, you see an awful lot of what I would call "hybrid code," and the language itself does nothing to prevent (or even discourage, for that matter) this.

Re: Good C++ OOP Book

Posted: Mon Feb 04, 2013 6:05 pm
by Buub
Great authors include:

Herb Sutter
Scott Myers
Andrei Alexandrescu
Marshall Cline
Many others (it's a short list)

The C++ FAQ is also an excellent resource (the book has more content than the web site):
http://www.parashift.com/c++-faq-lite/

Other suggestions:
http://www.parashift.com/c++-faq-lite/h ... n-cpp.html

Re: Good C++ OOP Book

Posted: Mon Feb 04, 2013 6:07 pm
by Buub
just brew it! wrote:
Buub wrote:
just brew it! wrote:
The biggest differences are that C++... is essentially a superset of C (and inherits all of C's low-level features like pointers, explicit memory management, etc.)

I strongly disagree. It may have started that way, but modern C++ is a very rich language all its own, and only works best when you move away from C paradigms. Logically, I feel it's far more useful to consider C++ a deep object-oriented language with very good C compatibility.

Yes, I agree that is how it is *intended* to be used. But in the real world, you see an awful lot of what I would call "hybrid code," and the language itself does nothing to prevent (or even discourage, for that matter) this.

Well I agree, which is why I'm trying to do my part to discourage it here! :-) There are numerous benefits to embracing the newer C++ styles.

Re: Good C++ OOP Book

Posted: Mon Feb 04, 2013 6:10 pm
by Buub
As others have said, but I'll make it simpler: an Interface is just a Class, with the expectation that it only be derived and not directly used.

To formalize the prevention of use, you can do the pure virtual thing, which prevents the class from being used itself, but still allows it to be derived (this makes it abstract as well).

You can also do other tricks like making the constructor(s) protected rather than public, which means only a derived class can instantiate the base class (this makes it potentially non-abstract, as in it may be a fully functional class, but it's still derive-only because it cannot be instantiated without being derived).

http://www.parashift.com/c++-faq-lite/abcs.html