How to output a map from a class with operator overloading

Pages: 1234
I have 6 C++ programming books


Which ones?

When designing, simply you can think in terms of 'has' and 'is'. A simple example. A Car is a Vehicle. A Bicycle is a Vehicle. A Vehicle has Brakes and a Name. A Car has an Engine. A Bicycle has Pedals. So as a start, you'd have a base abstract class Vehicle with a members Brake and Name. Class Bicycle would be derived from Vehicle and have a member Pedals. Class Car would also be derived from Vehicle and have a member Engine.

So in terms of this game, as a starter you have a base class Item. Weapon is an Item, Bullet is an Item. Weapon has Bullet. Character has a Name and has a collection of Item. So you'd have a base class Character with a member name and a member container (vector?) of Item (or pointer to Item for polymorphism). Item would be an abstract base class with a member Name. Bullet would be derived from Item. Weapon would be derived from Item and have a member Bullet (actually a pointer or ref to Bullet so that there is only one bullet object of each type). Then from this you then add other class members as required and the necessary class functions etc - building up the design. Then you start coding...

Enjoy!
Last edited on
Which ones?


I have:

Professional C++ by Marc Gregoire
A Tour of C++ Second Edition by Bjarne Stroustrup
C++ Primer Fifth Edition by Stanley B. Lippman
Programming Principles and Practice Using C++ Second Edition by Bjarne Stroustrup
The C++ Programming Language Fourth Edition by Bjarne Stroustrup

Ive looked through Professional C++ the most

Yeah I have to look into pointers as well, thats one of my weaknesses too. If i can strengthen my class/polymorphism/inheritance/pointer skills then I should be ok. I seem to struggle with that the most. The has a and is a relationship concept is is pretty solid to me, but i seem to struggle with how to implement things correctly.
Once my code gets to this size it gets hard for me to keep track of stuff and its harder for me to make things work properly.
You need a design.

I think its actually a good idea to keep rewriting this until its coded properly. I mean im not trying to get a job in programming or anything, i just want to learn enough to be able to code a 2D game in SFML without running into too many snags.
You still need a design.

The single page homework assignment with a handful of bullet points of things to do is fairly easy to "wing it" without doing much of anything. You write and debug in a single session, hand it in, then forget it.

But if you can't instantaneously visualise the whole program in your head, then you need to start writing some stuff down.

See seeplus's post on UML.

You don't need to go to extremes, but a few hours sketching out the major classes and their relationships will serve you well when implementing the code.

A good thing to have is an A6 pad / index cards / postit notes (anything approx 4"x6"). On each one, you write out a class.
You don't have to list everything, but you should capture it's essential points.
+--------------------+--------------------+
|              Class Name                 |
| One line description of it's purpose    |
+--------------------+--------------------+
| Member Functions   | Member Variables   |
|                    |                    |
|                    |                    |
|                    |                    |
|                    |                    |
|                    |                    |
|                    |                    |
|                    |                    |
+--------------------+--------------------+
If it's not fitting on the card, that's a hint you need to be splitting the class up into smaller units.

You start with user action like "User wants to fire the pistol".
Right there, you may see
- an abstract class called weapon, and a specialisation of that called pistol
- the need for a 'fire' method (or perhaps more generally, use).
- the need for a consumable resource like ammunition.

Guns, bows have ammunition that can run out (or maybe it doesn't, it's your program).
Swords have indefinite use.

Then you think about other things your user wants to do.

The point of all this is that you can quickly revise your design in minutes.

When you're done, you should be in the position where you can mentally walk through your class structure given an action like "fire pistol".


You don't embark on a long journey without looking at a map.
Don't code large programs without a design - it's your map to the code!.
M'ok, Ch1156, you've done some work with purchasing and reading for the most part decent C++ books. 6 books is a good START.

There is a decent online and free C++ tutorial you might want to look at as well:

https://www.learncpp.com/

As salem c points out you really need to plan out on paper before you write a single line of code a basic design of what you want to accomplish. Coding as you go without a "blueprint" makes fixing problems as they crop up harder than they should be.
design is the most difficult thing coders do, at least my opinion.

from scratch, you have a daunting big problem in front of you and breaking it down is a gigantic (necessary) effort. If you get something wrong, you have to spot it before coding so you don't code up a bunch of stuff that does not work together as planned.

And coming into a project, you are often trying to slice into an existing design with things that were not part of that design, making it fit the existing way of doing things, if you even can find/access the original design docs.

There are too many classes in school about syntax and theory and not enough on design; you are lucky to do 1 group project that has a design with any real difficulty to it.
I have:

Professional C++ by Marc Gregoire
A Tour of C++ Second Edition by Bjarne Stroustrup
C++ Primer Fifth Edition by Stanley B. Lippman
Programming Principles and Practice Using C++ Second Edition by Bjarne Stroustrup
The C++ Programming Language Fourth Edition by Bjarne Stroustrup

Ive looked through Professional C++ the most


I count 5...

I concur IMO that Professional C++ is the best of these. Which Edition have you got (The current is 5th)?

Note that 'C++ Primer', 'Programming Principles' and 'The C++ Programming Language' are now well out of date and no new up-to-versions are available.

For C++, you might also like to consider:
'Beginning C++20: From Novice to Professional' by Ivor Horton
https://www.amazon.co.uk/Beginning-C-20-Novice-Professional/dp/1484258835

I'd also suggest:
'API Design for C++' by Martin Reddy. The C++ used is only C++11, but the design principles covered are still valid.
https://www.amazon.co.uk/API-Design-C-Martin-Reddy/dp/0123850037/ref=sr_1_1

For C++ OO class design etc, I'd suggest:
'Deciphering Object-Oriented Programming with C++: A practical, in-depth guide to implementing object-oriented design principles to create robust code' by Dorothy Kirk
https://www.amazon.co.uk/Deciphering-Object-Oriented-Programming-depth-object-oriented/dp/1804613908/ref=sr_1_1

There's also:
'Object-Oriented Programming with C++' by David Parsons. An oldie (1997 - before even C++98!) but a goodie (don't expect the code to compile without changes). Now very cheap on Amazon second-hand
https://www.amazon.co.uk/Object-Oriented-Programming-C-David-Parsons/dp/0826454283/ref=sr_1_5

and as George suggests above, the site
https://www.learncpp.com/

Last edited on
There are too many classes in school about syntax and theory and not enough on design


I agree. When I did my degree many, many, ...many moons ago, we were taught design as part of programming. For every programming exercise we did, we had to first produce a design - which had to be submitted and marked before we started to code. Any design issues could then be sorted out before coding started (code had to match the design - so if you changed the design after, you had to submit the new design with the code, but no extra design marks!). Each exercise was marked out of 20 - with max 10 for design, 7 for code and 3 for testing. So without a good initial design, the best maximum mark you could get was 10/20 (which was almost impossible with no/bad design).

you are lucky to do 1 group project that has a design with any real difficulty to it.


Yep. This seems to have changed now where even with group projects, it's just the final result that's marked - not the initial design etc.

Design and debugging are major parts of being a programmer and both seem to be glossed over.... It's a shame as IMO students are now being 'short-changed'.
Thanks for all the replies, I've been working on a technical document that has some pseudocode and classes all planned out. I think the problem isn't so much planning things out, the stuff I usually make in console is pretty small so the scope can be easily kept in my head. But the real issue is that i'm trying to program my small console games using features of the language where my knowledge is lacking, so I try to fill in the gaps with what I know and it turns out disastrous. I'm going to try to fill those gaps though. Right now my weakest points are inheritance, polymorphism etc. class stuff. I was learning that stuff a long time ago but like i said, sometimes i go 2-6 months without touching code so i forget stuff, if i refresh my memory then i think i'll remember some things. Luckily i keep notes for just such a case. however my knowledge can still be expanded.

But if you can't instantaneously visualize the whole program in your head, then you need to start writing some stuff down.


I can for most console programs, i write, if i was writing in SFML and actually making a 2D game though, that's a whole other beast and I would need a game design document, and a technical design document, art design document, etc for sure, there's no way I could do something that size without one. That console program I posted in this thread was about 70-80% done, all I needed was an enemy class, and program in battles and that's it, that's as far as i would have taken it.


There is a decent online and free C++ tutorial you might want to look at as well:

https://www.learncpp.com/


Ah, yes, I know of this site, its quite a fantastic resource to have, and it looks like he's updated it quite a bit since i last looked at it too which is nice, ill be reading through that for sure.


from scratch, you have a daunting big problem in front of you and breaking it down is a gigantic (necessary) effort. If you get something wrong, you have to spot it before coding so you don't code up a bunch of stuff that does not work together as planned.


I learned this the hard way about 6 years ago. I had a design document for a game but it wasnt planned out very well. I hired someone to help me make a 2D game in Unreal Engine 4 using the visual scripting system Blueprint. Long story short, Looking at my financial records for my game design stuff, I spent $5,371.69 on that project, and about $3K of that was due to me not planning out things properly, and the game never even got finished so its a total loss, I wont make that mistake again haha.


I count 5...


Ah, my mistake, the 6th book is not a programming book, itsa game design philosiphy book written by the creator of Rimworld Tynan Sylvester. The title is Designing Games: A Guide to Engineering Experiences.


I concur IMO that Professional C++ is the best of these. Which Edition have you got (The current is 5th)?


Yeah that ones my favorite, I have the 5th edition currently.

Note that 'C++ Primer', 'Programming Principles' and 'The C++ Programming Language' are now well out of date and no new up-to-versions are available.


Thats really unfortunate to hear, all in all thats about $370 worth of books, its a shame i cant use them effectively. I'll check out those books you suggested as well, im always up for new learning material!
Thats really unfortunate to hear


The 'issue' is that C++ is updated every 3 years. The current version is C++20 with C++23 due in the next few months. Every version brings new features, new ways of doing things and sometimes removal of old features. Resources can only ever be as up-to-date as the version available at the time they were written. All C++ programmers are in constant 'learn' (or should be). They get a couple of years to learn new features before the next version comes along. For those familiar with C++, there are resources available that detail just new version features.

Those older books are not necessarily 'wrong' but don't cover the new C++ features since they were written.

I suggest looking at:
C++17: The Complete Guide
https://leanpub.com/cpp17

C++17 In detail
https://leanpub.com/cpp17indetail

C++20: The Complete Guide
https://leanpub.com/cpp20

C++20
https://leanpub.com/c20

There are also other C++ books on leanpub.com you might find of interest. Books by Bartłomiej Filipek (bartek) can be recommended. His blog site is:
https://www.cppstories.com/

Also books by Grimm (not the reaper!) and his blog:
http://www.modernescpp.com/

For a summary of new features available in the various C++ versions and which compilers support them, see:
https://en.cppreference.com/w/cpp/compiler_support
Last edited on
Something else about books, even if they are written for a later standard....

They are more often than not outdated even before they are published. The dead tree versions will never be updated except for possible new editions that require spending more money.

eBooks, depending on the author(s), may be updated. seeplus gave links to one of the better ePub sites along with several book suggestions.

You mentioned "Professional C++," do you know about "Beginning C++20 - From Novice to Professional"?

https://www.amazon.com/gp/product/1484258835/
Those older books are not necessarily 'wrong' but don't cover the new C++ features since they were written.

I suggest looking at:
C++17: The Complete Guide
https://leanpub.com/cpp17

C++17 In detail
https://leanpub.com/cpp17indetail

C++20: The Complete Guide
https://leanpub.com/cpp20

C++20
https://leanpub.com/c20

There are also other C++ books on leanpub.com you might find of interest. Books by Bartłomiej Filipek (bartek) can be recommended. His blog site is:
https://www.cppstories.com/

Also books by Grimm (not the reaper!) and his blog:
http://www.modernescpp.com/

For a summary of new features available in the various C++ versions and which compilers support them, see:
https://en.cppreference.com/w/cpp/compiler_support


Thanks for those book links im going to check those out for sure.


They are more often than not outdated even before they are published.


Well if thats the case then i think its bes tto just learn the most current version and update yourself on the standard each 3 years.

You mentioned "Professional C++," do you know about "Beginning C++20 - From Novice to Professional"?


Yeah it seems like a nice book, I wishlisted it on amazon, that one and Deciphering Object-Oriented Programming with C++ seem the most intriguing to me. I'll probably buy those first.
Before you buy new books you better study the books you have first.
In a previous post you struggled with getting a value from a map, that's very basic knowledge from C++98. No need to learn the latest C++20 when you don't know the basics.
Right now my weakest points are inheritance, polymorphism etc. class stuff


Well these 3 books certainly cover this:
Professional C++
C++ Primer Fifth Edition
Programming Principles and Practice Using C++

However, C++ is quite a complicated language when you move away from the basics. It really needs to be studied like you would study a topic at college. Just reading a few pages 'now and again' isn't going to do it. Nor is 'dipping into' a book to find out how to 'do something'. These books are designed to be read and understood from the beginning to the end. Understanding being the operative word here. It's one thing reading about something, it's another when you come to try to do it yourself in your own program. That's why you need to consolidate what you're read with actually using in code. This needn't be long code - just some test/example code to try out features. For new C++ features that's what I still do (and I guess others as well) before they're used in production code. When you're then comfortable with using a particular technique (eg obtaining a value from a map) then you can use it in your code. IMO there's nothing more frustrating then trying to write an 'involved' program, getting bogged down in how to do things, can't get things to work and then give up. To paraphrase, "don't try to run before you can crawl".

all in all thats about $370 worth of books, its a shame i cant use them effectively


Yes you can. IMO I'd ignore 'The C++ Programming Language Fourth Edition by Bjarne Stroustrup' as that is a pure reference book and is more likely to hinder your learning than help. But if you read and understand the others, then you'll have a good grasp of the basics of C++. Then you can supplement them with say e-books on C++17 & C++20 etc. Having a good basic understanding of C++ is more important than knowing about some of the latest language additions. They can 'easily' be learnt once you have the good grounding.

If you come across code constructs that you don't yet understand, a decent place to start is https://en.cppreference.com/w/cpp which documents the complete language. Once you have the knowledge necessary to understand what it's saying, then you can learn about new elements etc.

And don't forget the reference part of this website! It's not up-to-date (only some of C++14) but IMO is easier to 'read' than the cppreference site if it has what you're looking for. If you want info about say a map or vector methods then this is a good starting place.

Have fun!
C++ programming books recommended by people


What one person thinks is a 'good' book, another may think is poor! IMO assuming roughly equal technical content then the 'style' can become important. One style can click with one person and be off-putting to another.

The Horton book 'Introduction to C++' has been recommended here (and by myself) - but an alternative is 'C++20 For Programmers' by Deitel.
https://www.amazon.co.uk/gp/product/0136905692

Some people swear by the Deitel books and others don't really like them (I'm not keen on their style). Much is personal preference.

PS. If you look for C++ books, you'll see many published by Packt. IMO the quality of these is very variable - depending upon the author and the editor. Some I've seen are IMO quite good, and some are quite poor. No. I'm not naming titles!
Yeah, I will study those books for sure, im going to take notes on them as well so i can remember stuff. I hoestly didnt like C++ Primer too much, I read through quite a lot of that and found myself confused by the course structure. I like Professional C++ so i will read through that first. Should i be learning from the learncpp.com site or just use that as reference? Im reading through the chapters that cover classes and inheritance/polymorphism right now. I will read through the entire site though once im done reading about that and refresh myself on pointers. I'll check that one C++ 20 for programmers out.
Should i be learning from the learncpp.com site or just use that as reference?


I wouldn't classify learncpp.com as a 'reference' - it's primarily a learning resource.
Ok well thats good cause thats how i was using it haha. Im going to really study this stuff and take notes. Ive been learning C++ on and off since i was 18, im 30 now. Its time I actually dedicate a real chunk of my life to this stuff if i want to make games. I appreciate all the responses from everyone on here and the feedback and resources! If i have any further questions or anything ill just post them here.
Im going to really study this stuff and take notes


The 3 best ways to learn is to write code, write code and write code!

Reading is fine for knowledge, looking at other's code is helpful but nothing makes it sink in more than writing your own code. When you read about a new technique, syntax etc, then write a small program that uses it. It needn't do anything useful but the process is helpful in the learning process.
Oh i will for sure, even though im only learning C++ to make a game, i do enjoy programming and programming languages in general, i find them interesting. However when im learning, i often find that when im learning about a feature, I try to figure out how it will be useful when im programming a game, and sometimes certain features have no apparent use, they obviously do but i cant see one at the time, and thats why some of my knowledge has gaps in it and why i might know some advanced things but not others. Sometimes its hard to figure out why a certain way of doing things is useful, or most importantly, when it should be used and how.
Pages: 1234