Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Good C++ OOP Book

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 AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Good C++ OOP Book

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

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Good C++ OOP Book

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.
Nostalgia isn't what it used to be.
 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Re: Good C++ OOP Book

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 AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Good C++ OOP Book

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;
Nostalgia isn't what it used to be.
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Good C++ OOP Book

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

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: Good C++ OOP Book

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.
 
Captain Ned
Global Moderator
Posts: 28704
Joined: Wed Jan 16, 2002 7:00 pm
Location: Vermont, USA

Re: Good C++ OOP Book

Mon Feb 04, 2013 5:51 pm

Hold on while I e-mail ESR and ask if he'll officiate this bout.
What we have today is way too much pluribus and not enough unum.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Good C++ OOP Book

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.
Nostalgia isn't what it used to be.
 
Buub
Maximum Gerbil
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: Good C++ OOP Book

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
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: Good C++ OOP Book

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
Posts: 4969
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA
Contact:

Re: Good C++ OOP Book

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

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On