Good C++ OOP Book

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Good C++ OOP Book

Postposted on Sun Feb 03, 2013 9:25 pm

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.
Gigabyte Z77-DS3H | Core i7 3770 | 2x8 GB Crucial Ballistix DDR3-1600| Samsung 830 128GB SSD | Gigabyte HD7950 3GB | Win 7 Pro x86-64
CampinCarl
Graphmaster Gerbil
 
Posts: 1318
Joined: Mon Jul 04, 2005 9:53 pm

Re: Good C++ OOP Book

Postposted on Sun Feb 03, 2013 10:42 pm

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?
Image
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
Flying Fox
Gerbil God
 
Posts: 24439
Joined: Mon May 24, 2004 2:19 am

Re: Good C++ OOP Book

Postposted on Sun Feb 03, 2013 10:49 pm

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.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37732
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Good C++ OOP Book

Postposted on Sun Feb 03, 2013 10:52 pm

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.
Gigabyte Z77-DS3H | Core i7 3770 | 2x8 GB Crucial Ballistix DDR3-1600| Samsung 830 128GB SSD | Gigabyte HD7950 3GB | Win 7 Pro x86-64
CampinCarl
Graphmaster Gerbil
 
Posts: 1318
Joined: Mon Jul 04, 2005 9:53 pm

Re: Good C++ OOP Book

Postposted on Sun Feb 03, 2013 11:05 pm

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;
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37732
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Good C++ OOP Book

Postposted on Sun Feb 03, 2013 11:18 pm

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").
Code: Select all
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.
Image
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
Flying Fox
Gerbil God
 
Posts: 24439
Joined: Mon May 24, 2004 2:19 am

Re: Good C++ OOP Book

Postposted on Mon Feb 04, 2013 5:50 pm

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.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4200
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Re: Good C++ OOP Book

Postposted on Mon Feb 04, 2013 5:51 pm

Hold on while I e-mail ESR and ask if he'll officiate this bout.
Life is hard; but it's harder if you're stupid. Big Al.
Captain Ned
Global Moderator
Gold subscriber
 
 
Posts: 20311
Joined: Wed Jan 16, 2002 7:00 pm
Location: Vermont, USA

Re: Good C++ OOP Book

Postposted on Mon Feb 04, 2013 5:54 pm

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.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37732
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Good C++ OOP Book

Postposted on Mon Feb 04, 2013 6:05 pm

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
Last edited by Buub on Mon Feb 04, 2013 6:20 pm, edited 1 time in total.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4200
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Re: Good C++ OOP Book

Postposted on Mon Feb 04, 2013 6:07 pm

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.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4200
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

Re: Good C++ OOP Book

Postposted on Mon Feb 04, 2013 6:10 pm

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
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4200
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 2 guests