C++ motivational thread

There will never be a good time to get back to it, so might as well do it anyway. The book I’m using is Programming Principles and Practice Using C++, second edition. I had last left off just before Ch. 12 - A Display Model, which seems like forever ago. The first few chapters are essentially reading. Hopefully in a month or two I can be back to where I left off.

#include "std_lib_facilities.h"

int main()
	cout << "Hello again, world!\n";
	return 0;

Arbitrary internet pic

Good luck! If you like you can shoot any questions my way! I’m no expert but know my way around the beast and know how not to anger it for the most part.

I definitely don’t know all of the language, even Stroustrup says he doesn’t these days but the percentages are different. :slight_smile:

Thanks! I’ll likely take you up on that. I’ll be posting any semi-interesting or better exercises in here, or spinoff ideas of them.

The book that I’m restarting with, Programming Principles and Practice Using C++ - Second Edition, uses C++11 and C++14. I know that C++ versions have been moving fast (C++20 is current lastest, and C++23 is in the works). Aside from new features being added in newer versions, I’m wondering how much is changing across versions for syntax and constructs. It seems that reading other people’s C++ could get pretty hairy, given that there are so many versions.

C++11 was the major update that introduced a bunch of new stuff so you are okay with that book in terms of it being “Modern C++”. C++20 was another biggie, but I’ve kind of paused at C++17.

It’s a big language and standard library (STL). C++ thought leaders are on a mission to teach Modern C++ to beginners instead of the C constructs that you can use in C++ (most valid C is valid C++ up to a point). But at some point somebody is going to bump into libraries and code written in the traditional style and be lost so it’s kind of two languages.


// C style
#define SIZE 42
int my_array[SIZE];
// <fill it up>, then
for (int i = 0; i != sizeof(my_array) / sizeof(int); ++i) {
    printf("%d", my_array[i]);

// modern C++
constexpr int size = 42;
auto my_array = std::array<int, size>{ };
// <fill it up>, then
for (const auto& elm : my_array)
    std::cout << elm;

Yipes man. That’s looking very different to C style (what I know).

There was a full stop there instead of a comma, in std::array<int, size>. Maybe that’s better after the edit? :laughing:

It is nicer though, here’s most of the ways of iterating over an array, first with a C style basic array then onto the same std::array:

The syntax is still very different between C style and modern. The newer style syntax is alien to me. I guess I’ll get to that eventually.

Reloading on coffee and chugging along on the book today after work. Nothing too interesting going on yet. I’m surprised though at how much C++ syntax I have forgotten over a couple or years or so, where when I look at C code (mine or someone else’s) I remember all of the syntax.

There have definitely been changes over the years from the C origins. It’s like anything else, you get used to it when you look at it enough. The history does mean you’ll encounter the C stuff and all of the steps towards the most modern.

I remember it took me a while to get my head around pointer syntax, but that’s completely from C so it’s one of the transferable skills, and even if modernists frown upon using raw pointers, they are everywhere in real life.

I remember working out pointers by drawing labeled objects for memory, names, and data. The book I used for C didn’t explain pointers very well, making it seem more complicated than it is. I feel like this is the case for most things that seem complicated. The person trying to explain it hasn’t really put in the time to work out a good explanation.

I’ve read people swear blind that a pointer is an address, when it’s a variable that holds an address that the compiler knows is a pointer because of its type. Easy to check by seeing that the address of the pointer is different to the address that it stores!

Taking incorrect pieces of advice like that on board really can confuse the mental model.

I’m not back around to the pointers section in this C++ book yet (I remember it being good), but it has the best beginner explanation of variables that I have read anywhere. It says that an object is a region of memory that has a type which specifies what kind of information can be placed in it. And a variable is a named object. < The gist of it anyway. Most books I have read or skimmed don’t even try to explain what a variable actually is, only how to use them.

I might be mistaken, but I think the beginner C book I used explained pointers as being addresses (or made it seem that way). I remember working it out back and forth between my code and drawing images. And hating the explanation in the book for mucking it all up. Pretty sure I have an inkscape file saved somewhere that illustrates it in case I needed to revisit it later.

I suppose they are addresses in terms of the type in the same way an uint16_t is a 16 bit unsigned int, but they are really just containers for that information and are not the information itself.

A little example from the C++ book. Either a word can be entered per line, or a bunch of words can be entered at once.

// Counts repeated words
int main()
	int number_of_words = 0;
	string previous;
	string current;
		if(previous == current)
			cout	<< "Word number " << number_of_words
					<< " repeated: " << current << '\n';
		previous = current;

The explanation in the book for while(cin>>current) is that the >> operator ignores whitespace. But does it really? It seems here to use whitespace to delimit strings.

I think they must mean it ignores trailing whitespace.

I’ll see if I can dig out my old hard drive for my saved explanation on pointers (I definitely need a reminder at this point). It might be tomorrow. Just curious to see what I arrived at to remind myself.

That operator for cin takes the input and fires for every whitespace delineated sequence of chars. Never knew that!

Yes, that is what I’m seeing. It is acting upon the presence of whitespace between character strings.

I think of memory as a big chess board with rows and columns labelled with hex digits so that each square has a hex address that in turn can have a value in it. There are stacks, heaps etc to think of but that image is fine for visualising pointers n stuff.

Pointers are just variables that contain addresses that the compiler treats slightly differently because we’ve told it to.