Accelerated C++ - Chapter 0
Solutions to exercises in Chapter 0, "Getting started."
Solutions to exercises in Chapter 0, "Getting started."
Compile and run the Hello, world! program.
HintMake sure that you locate the documentation and help files for your compiler and editing environment.
Solution
Consult the documentation for your particular compiler. Each will have its
own set of directives. For the GNU C++ compiler, assuming the code is in a file named hello.cpp,
the following may be entered at the command line:
g++ hello.cpp
This will produce a file named a.out, or on Windows systems a.exe,
that can then be executed like any other program.
Here is the source for the Hello, world! program.
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
|
Write a program that, when run, writes
This (") is a quote, and this (\) is a backslash.
HintThe backslash character has a special meaning in a string literal.
SolutionThe quote and backslash characters must be preceded by a backslash, which is the escape character.
#include <iostream>
int main()
{
std::cout
<< "This (\") is a quote, and this (\\) is a backslash."
<< std::endl;
return 0;
}
|
The string literal "\t" represents a tab character; different C++ implementations
display tabs in different ways. Experiment with your implementation to learn how it treats tabs.
SolutionFor now, self-directed study. More information coming.
Write a program that, when run, writes the Hello, world! program as its output.
Solution
The main issue here is properly escaping special characters such as quotes, and adding newline (\n)
characters where appropriate.
#include <iostream>
int main()
{
std::cout
<< "#include <iostream>\n"
<< "\n"
<< "int main()\n"
<< "{\n"
<< " std::cout << \"Hello, world!\" << std::endl;\n"
<< " return 0;\n"
<< "}\n";
return 0;
}
|
Is this a valid program? Why or why not?
#include <iostream> int main() std::cout << "Hello, world!" << std::endl; |
Solution
No, this is not a valid program. The main function is missing the
opening and closing braces. The corrected program appears as follows:
#include <iostream>
int main() { std::cout << "Hello, world!" << std::endl; }
|
Is this a valid program? Why or why not?
#include <iostream>
int main() {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}}
|
SolutionYes, this is a valid program. The main function nests 5 scopes within
the opening and closing braces. With indentation applied, the program
would appear as follows:
#include <iostream>
int main()
{
{
{
{
{
{
std::cout << "Hello, world!" << std::endl;
}
}
}
}
}
}
|
What about this one?
#include <iostream>
int main()
{
/* This is a comment that extends over several lines
because it uses /* and */ as its starting and ending delimiters */
std::cout << "Does this work?" << std::endl;
return 0;
}
|
SolutionNo, this is not a valid program because free-form comments cannot be nested.
...and this one?
#include <iostream>
int main()
{
// This is a comment that extends over several lines
// by using // at the beginning of each line instead of using /*
// or */ to delimit comments.
std::cout << "Does this work?" << std::endl;
return 0;
}
|
SolutionYes, this is a valid program. Single-line comments (which begin with //) can contain /* and */, which are normally free-form comment delimiters.
What is the shortest valid program?
Solution
A valid C++ program must implement the entry-point function, main.
The main function always has a return type of int, but the return statement may be
omitted. Therefore, the shortest-possible, valid program is as follows:
int main(){}
|
Rewrite the Hello, world! program so that a newline occurs
everywhere that whitespace is allowed in the program.
Solution
Following is the source with newlines inserted. Note that the whitespace between #include and <iostream>
may not be replaced with a newline because includes must be all on one line.
#include <iostream>
int
main
(
)
{
std
::
cout
<<
"Hello, world!"
<<
std
::
endl
;
return
0
;
}
|