Public Group

# I need a book (or somebody's project source code) which properly explains OOP

This topic is 913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

As mentioned in other threads I've "borrowed" (indefinitely) Bjarne Stroustrup's book, and I own Jazon Yamamoto's book The Black Art Of Multiplatform Games Programming. Neither of these books properly explain OOP (type objects). Bjarne Stroustrup's book, obviously, uses very detailed examples each which build on the previous. Very little of it makes sense to me and most of his chapter on OOP is snippets of code, not complete programs. Jazon Yamamoto's book has very elaborate example code - there's a space shooter game included - but the code is spread over 20 or 30 files. I can find a function prototype but then I struggle to find the function call. It's an expertly written program structured to be extendable, not structured easy to understand - "easy" would be poorly structured, with nearly everything in one or two files.

So...

I'm after a programming book (ideally C++ but possibly C# or Java) that properly explains how to handle custom objects. I know how to create an instance of something in C++ but then I have no clue how to call a function specific to the object's class. I thought Yamamoto's book would have covered this in easy-to-follow detail but his focus is on SDL, not the specifics of how to create an instance of a class and then call its functions to verify that it was set up.

There is heaps of programming books out there and all of them do a good job of explaining variables and functions but it seems none of them (none that I own) explain OOP in a practical way. Perhaps there is a book dedicated to the subject, but then I fear it will focus more on expert gibberish. Opinions please! Just be sure to state your experience, e.g. are you qualified, how many years have you been writing good code for.

Of course, if you have a simple game or program which is only one or two fairly small files you could simply email them to me and I'll take them apart :) That's how I learned OOP in Blitz Max, back when I was 16. I found a program that created objects and put them in lists, then it read through the list and drew the objects at their coordinates. I also learned to use type methods and calls to "self". Of course, C++ is a different story as it's much harder.

##### Share on other sites

In your post you state that you already know OOP but you don't know OOP with C++. That is pretty much how I started out. I knew a few other languages and had years of programming practice but only used C++ for a few arduino projects before.

I started coding in C++ because of one specific project. So I used the code that already was there and the internet as a reference. The main oop related thing that I did not know from the programming languages I used before was the proper use of pointers and references so for the most part that is the important stuff I needed to learn.

Make sure your C++ books are up to date (there are some great features in C++11 and C++14) because C++ is evolving and in a good way, too.

Also make sure to not shoot yourself in the foot by using raw pointers. Most of the time you won't need them (you want to use smart pointers in many cases).

There is still a lot not alright with this code but have a look at how an instance of the GameController is created  and how methods are called on the instance https://github.com/GlPortal/glPortal/blob/master/source/Game.cpp

this should at least get you started.

Edited by glportal

##### Share on other sites

See these  "Learn something in 24 hours" or "Teach yourself something in 21 days" books. ( for example java or c or c++ ). Very good for beginners in my opinion. But of course you won't learn anything in 21 days, you will just have a solid start. And after you know everything from these books, go to google, write "lazyfoo SDL tutorials" and you are going to start making very very cool 2d games.

Edited by Heelp

##### Share on other sites

C++ is not that much harder than any other language. You learn one, you learned them all. Right? ;)

I don't even think you need a book at this point. If you know how to program and want tips on OOP, I recommend Derek Banas on YouTube.

Highly recommended -->

Design Patterns Playlist

Never watched this, but probably useful -->

OOP Playlist

thenewboston on Youtube has stuff specifically for C++ in bite-sized videos. No simpler explanations on the web than this guy's.

C++ Playlist

##### Share on other sites
Ah that makes more sense now. I wondered why I couldn't do object.classFunction and it's because I didn't do the Class class thing as seen below in the first line:

MusicObserver musicObserver;
musicObserver.addCallback(Event::loadScene, std::bind(&MusicObserver::loadMap, musicObserver));

##### Share on other sites

Aha!

#include <iostream>

using namespace std;

// The abstract class
class Thing{
public:
Thing (int, int);
int x;
int y;

giveX(){return x;}

};

// The proto-type?
Thing::Thing (int a, int b){
x = a;
y = b;
}

int main()
{
// The constructor?
Thing thing (10, 12);
cout << "Hello world!";
cout << thing.giveX() << endl;
return 0;
}


It's an ugly output, it really needs a newline character but it does give me "hello world!10" 10 being the value of thing.x - so it works.

##### Share on other sites

What you call the "abstract class" is actually just a "class" - it's a definition of the data and behaviour for an instance of that class - an "object."

What you call the "prototype" is actually the definition of a constructor - a function that is called when an object is initialized or "constructed."

Judging by the content of your posts, I suspect you don't know OOP as well as you think you do, or learned it from a source that doesn't use the same terminology as the rest of the industry. It may help us give further advice if you were to more explicitly state your understanding of what it means to be "object-oriented" and, indeed, what an "object" is. For example, what do you mean when you say "type object?" While I have no direct experience with Bjarne Stroupstrup's book (nor am I even sure which book you're referring to), I imagine it should explain the gist of what you're looking for if perhaps not in terms you're familiar with.

Also, since part of the utility of OOP is managing the complexity of larger programs, you'll find that most "practical examples" of it are likely to be much larger than 2 files - "expertly-written programs structured to be extendable" is kind of the point of object-oriented design. In a well-written object-oriented program (or really any program, for that matter), you shouldn't need to understand the entire program to understand what any given piece of it is doing. If you need to understand the whole program to understand a piece of it, then your design is a failure. ;)

Edited by Oberon_Command

##### Share on other sites

Yes, I suspect that "abstract class" is a term one uses when there is an abstract class, with inheritance used for sub-classes - hence, the original class (not actually used directly) is called the abstract class. I'll edit my file now and add the proper terms  :)

##### Share on other sites

For OOP I would recommend the book "The Object Oriented Thought Process" by Matt Weisfeld. He tries to explain OOP in a language-agnostic way, but of course the code examples he provides throughout the book are in Java, however at the end of the chapters he also includes the code examples in C# and Visual Basic, but throughout the book he also mentions tips/differences in C++.

I have the 3rd edition of this book, the current edition is the 4th edition and I really don't know how much it has changed.

A great source for starting out with C++ would be to read the tutorials on learncpp.com. This site is completely free and has a collection of subjects in C++. Recently the author is updating them.

Then I'd recommend to try to get your hands on these books:

- "Effective C++", 3rd Edition by Scott Meyers, and
- "Professional C++", 3rd Edition by Marc Gregoire.

The book by Scott Meyers offers a handful of tips to avoid common pitfalls in C++, and the the book by Marc Gregoire is similar, except it goes into more detail and is more recent (covers some aspects of C++14 & C++11). However, these books are kind of intermediate C++ level, get them once you feel comfortable with C++'s basics.

And finally, sorry for my English since it is not my native language.

Edited by yazder

##### Share on other sites

Yesterday I was asking for an example of a program that creates instances and draws them on the screen. Now I've made it myself, and it even works without crashing:

Thing thing (10, 12);
Thing thing2 (25, 25);
thing.DrawMe(wallGFX, screen);
thing2.DrawMe(wallGFX, screen);
Now... apparently I just need to create a vector which stores pointers to each instance of Thing, then I should be able to make a for loop go over each object and draw the bitmap at the location of each instance.

In response to yazder: thanks, I might buy some more books when I have money.
Edited by jeffkiwi

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632952
• Total Posts
3009434
• ### Who's Online (See full list)

There are no registered users currently online

×