Showing posts with label chapter 2. Show all posts
Showing posts with label chapter 2. Show all posts

Wednesday, 21 February 2024

Chapter 2 // Exercise 1, 2, 3, 4, 5, 6, 7 - The C++ Programming Language


I bought this book a long time ago. The current edition was printed in 2013 and features up to C++11. On his website, Bjarne said he's never felt a need to update it but with all the new features in C++17 and C++20, I feel a 5th edition should be on the horizon and I'm wondering if it'll get released when I'm halfway through this one.

But either way. Welcome to this post featuring the exercises starting at chapter 2. I've done it this way as the first chapter was just writing exercises. 

Unlike Principles & Practice, the exercises can be found online and are not actually in the book:

Chapter 2 - A Tour of C++: The Basics
Exercise 1
When first reading this chapter, keep a record of information that was new or surprising to you. Later, use that list to focus your further studies

Will do.

Exercise 2
What does a compiler do? What does a linker do?

A compiler processes the source text from a project and turns it into object files. The linker turns the object files into an executable.

Exercise 3
Get the ‘‘Hello, world!’’ program (§2.2.1) to run. This is not an exercise in programming. It is an exercise to test your use of your edit-compile-link-execute tool chain.


Hello, world my old friend. I'm using Visual Studio Community 2022 with C++20 enabled. I'm also using my modified version of "std_lib_facilities" that's given in Principles & Practice. It can be found here:

Exercise 4
List three (or more) C++ compilers

GNU GCC, Clang, Intel C++ Compiler

Exercise 5
Write out a bool, a char, an int, a double, and a string.


Exercise 6
Read in a bool, a char, an int, a double, and a string.


Exercise 7
What is an invariant and what good might it do?

From what I can understand from Section 2.4.3.2 an invariant is a way of checking or asserting that some logic on class member variables is correct. So if you have an int member variable and that can only be initialised with positive numbers, the constructor would throw an error if you passed a negative number to it. There are other ways to enforce these "conditions" such as subscript operators checking that the value passed to it is within range of the size of the container.

Thursday, 23 June 2016

Chapter 2 Drill - Principles & Practice Using C++

This drill is extremely straight forward. I'm doing a comment on it though because I noticed that the link provided in the book for the header file is now out of date and header file it provides will not work.

After some digging (for a guy who created a programming language, his website is poorly organised with a ton of missing links. I don't know if this is done on purpose because you know, irony or not) I managed to find the correct header file which you can download from his website below:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

I'm using Visual Studio 15 and so far I've found it to be an excellent compiler. The amount of errors it catches is amazing and I love that most of the time it will even pick up on spelling mistakes and suggest what you might have meant. 9 times out of 10 I've not even had to compile for it to bring up errors as it just underlines in the same red squiggle you would see on Word.