# Exercise 0-1

What does the following statement do?

3 + 4;


# Solution

The statement yields a result of integer (int) type, with the value of 7. As 3 + 4 = 7.

# In Detail

• The first operand, 3, is of type integer (int). The result is therefore of type int.
• The addition operator, +, takes in the second operand (4) and add to the first operand (3). The second operand must be of the same (int) type for the addition operator to work.
• The semi-colon at the end discards the result.
• Note that the result is not expected to show in the console output window (e.g. a command window) as it is of type int. Only result of type std::ostream (standard output stream) is displayed in the console output window. For instant, if we submit the following program…
#include <iostream>
int main()
{
3 + 4;
return 0;
}


We should expect to see the following output. i.e. the result of 7 does not get output to the console window.

Process returned 0 (0x0) execution time : 0.359 s
Press any key to continue.


To get the result to display the int result of 7, we use the std::cout (standard console output) facility, which is of type std::ostream (standard output stream).

#include <iostream>
int main()
{
std::cout << 3 + 4;
return 0;
}


This now output the result to the console window.

7
Process returned 0 (0x0) execution time : 0.248 s
Press any key to continue.


Just a one-line statement and we have covered lots of ground here!

# Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

# Exercise 0-0

Compile and run the Hello World program.

# Solution

The classic Hello World code looks like this.

// a small C++ program</p>

#include <iostream>

int main()
{
( (std::cout << "Hello, world!") << std::endl);
return 0;
}


# In detail

// a small C++ program

• The double forward slashes indicates comment. Comments are ignored by the program during implementation.
<br />#include <iostream>


• The #include directive ensures the <iostream> (input-output stream) facility is made available to the program.
int main()
{
}

• We declare that the main function would yield an integer (int) type result. (i.e. result = 0 if success. Otherwise non-zero). The main function is the first function that is run when we implement a C++ program – so we must define one.
• We define the main function within the two curly braces { }.
    ( (std::cout << "Hello, world!") << std::endl);

• The expression statement create a result of type std::ostream (standard output stream), defined by its first operand std::cout (standard console output). The utility cout (console output) is in the (namespace) scope std (standard library).
• The first (left associative) output operator << writes the string literal “Hello, world” to the std::cout. This causes a side-effect of displaying the string literal on the console command window.
• The second (left associative) output operator << allows the manipulator std::endl (standard end line) to manipulate the standard output stream, which in this case, ends the current line of output.
• Note that the brackets are not strictly required. I’ve included the brackets here purely to help visualising the logics associating with the left-associative output operator << .
• The entire expression statement yields std::cout as a value (a result) which is of type std::ostream (standard output stream), and as a side effect writes the string literal (Hello, World!) onto this standard output stream (as displayed in the output command window). The std::endl manipulator ends the output line. Once it’s displayed (which is what we wanted), the semi-colon at the end of the expression statement discards the std::cout value (which is appropriate as we don’t need to keep hold of this value any more).
    return 0;

• The return statement explicitly return an integer (int) value of 0 at the end of the main function to imply job run success. The semi-colon explicitly states that this is the end of this return statement.

# Result

Once submitted the program, the following text appears in command window as expected.

Hello, world!

Process returned 0 (0x0) execution time : 0.268 s
Press any key to continue.

# Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

# Accelerated C++ Summary to Chapter 0 (Getting Started)

Chapter 0 of the Accelerated C++ book (by Koenig and Moo, reference at bottom) has provided me with an in-depth introduction of the classic Hello World C++ program in just 6 pages. As I see that there is an end of chapter exercise section and the book does not come with a solution (for good reason I guess!), I’ve decided to have a go doing these exercises and post my solutions in separate posts. Despite the book pretty much has covered it all, I would aim to include my own version of explanation for the benefit of learning and understanding. (My solution/explanation is subject to error as it’s just a scientific programming sketchbook after all!)

Although the (Hello World) program we’ve written is simple, we’ve covered a lot of ground in this chapter.

Key concepts learnt from this chapter: Hello World program; comment; #include directive; standard library vs core language; input-output stream (iostream); standard header; angle brackets; the main function; curly braces; operand type (int and std::iostream); output operator (<<); namespace (std); standard output stream (std::cout); stream manipulator (std::endl); the return statement; expression; result; side effects; operator vs operand; left-associative operator; scope; qualified name (std::cout); scope operator (::); free and non-free form structure; special string literals; escape using backslash; semicolons; expression statement; null statement.

# References

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2009

# Add new Code::Block Editor Theme: Son of Obsidian

The Code::Block IDE by default only comes with one editor scheme called “default”, which is a standard white background with code syntax highlighting. I wasn’t a fan of the default theme and wondered whether there might be a way to import a readily built alternative theme without having to go through the trouble to manually create one myself.

After doing some “Google-ing” I came across this very good Code::Block Forum which led me to Pizaro’s instruction on Exporting and Importing Code::Blocks Themes and the Son of Obsidian theme config file. So I had a go using both the instruction and the theme config file.

The end result? It worked! Now in my Code::Block I have a new Son of Obsidian Theme which is much more user friendly (and cool) to work with. It has a dark background with sharper syntax highlighting.

If you are looking to do the similar I highly recommend you to check out these articles.

# Posting Source Code in WordPress Posts

Found this handy article on Posting Source Code in WordPress Posts today which I hope may enable me to start posting C++ codes via posts. It also says that for wordpress.org users (which I am) this the SyntaxHighlighter Evolved Plugin is required. I’ve decided to give it a try – to post the classic Hello World C++ Code in t his post and see what I get.


#include <iostream>

using namespace std;

int main()
{
cout << "Hello world!" << endl;
return 0;
}



Did it work?

# My solutions to the book Accelerated C++ (A. Koenig and B. Moo)

C++ is one of the most popular programming languages used in the scientific programming area. So I’ve decided to have a go learning it.

I’ve been recommended to start with this book called Accelerated C++ by Andrew Koenig and Barbara E. Moo (Published in 2000 by Addison Wesley). The book is very compacted (~300 pages) and is said to be suitable for beginner. The tag line is “Practical Programming by Example” – teaching only the bits and pieces that are required to solve most of the problems.

The book contains 17 chapters. At the end of each chapter is a list of problems to solve. As part of my learning I shall have a go solving these and post my solutions and feedback via posts.

It’s just learning.