Category Archives: C++

C++ Programming related.

Accelerated C++ Solution to Exercise 0-1

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

Accelerated C++ Solution to Exercise 0-0

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