Solutions to Chapter 1

1-0. Compile, execute and test the programs in this chapter.

Answer: not applicable.

1-1. Are the following definitions valid? Why and why not?

const std::string hello = “Hello”;

const std::string message = hello + “, world” + “!”;

Answer:  Yes.

1-2. Are the following definitions valid? Why and why not?

const std::string exclam = “!”;

const std::string message1 = “Hello” + “, world” + exclam;

Answer: Unfortunately, the second statement will cause an “invalid operands to binary expressions(‘const char*’ and ‘const char*’). Which happend at ‘”Hello” + “, world”‘.

There is no ‘+’ operator to add two C-style strings – char *, char[]. while the “Hello” and “, world” alike strings are actually C-style strings. But the string from standard library contains the ‘+’ operator which should do the right job. So simply change the code to the following and it will work:

const std::string exclam = “!”;

const std::string message1 = std::string(“Hello”) + std::string(“, world”) + exclam;

 

to be continued…

 

Solutions to Chapter 0

0-0. Compile and run the Hello, world! program.

Answer: on my Mac, simply ” g++ main.cpp”, then ” ./a.out”

0-1. What does the following statement do?

3+4;

Answer: It is an expression, result is 7; the statement has no side effects.

0-2. Write a program that, when run, wirtes

This (“) is a quote, and this (\) is a backslash.

Answer: Just use string literals to represent ‘”‘ and ‘\’. the code should be like

std::cout << “This (\”) is a quote, and this (\\) is a backslash.”;

0-3. 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.

Answer: This question does not have a specific answer. On my Windows 7, one tab is as long as four spaces; On my Mac Lion, one tab is as long as three spaces. Therefore, it is recommended not to use TAB but use space instead, to avoid the potential problem with different length of TAB across different system.

0-4. Write a program that, when run, wirtes the Hello, world! program as its output.

Answer: Just remember to use string literals to represent “\”  or “‘”.

0-5. Is this a valid program? Why or why not?

#include <iostream>

int main() std::cout << “Hello, world!” << std::endl;

Answer: This is NOT a valid program. It will be a compiler error: “Expected function body after function declaration.

0-6. Is this a valid program? Why or why not ?

#include <iostream>

int main() {{{{{{ std::cout<<“Hello, world!”<<std::endl;}}}}}}

Answer: This is a valid program. The exterior bracer and others are different. the exterior is required for the function body. The other are just defining blocks.

0-7. What about this one?

int main()

{

/* This is a comment….

because it uses /* and */ as blah blah */

std::cout<<“Does this work?”<<std::endl;

return 0;

}

Answer: It does NOT work. the second line of comment, after the first ending comment tag

“*/”, “as blah blah..” will be recognised as code, and the compiler will give an error.

Here, the error is “Use of undeclared identifier ‘as’ “.

0-8. …and this one?

#include <iostream>

 

int main (int argc, const char * argv[])

{

 

// insert code here…

//This is a comment that extends over several lines

// by suing // at the begining of each line insted of using /*

// or */ to delimit comments.

std::cout << “Hello, World!\n”;

return 0;

}

Answer: It is correct. the ‘//’ will make the compiler take all things behind within the same line as comment.

0-9. What is the shortest valid program?

Answer: on my MacOS, I wrote a program with an empty main function, which doesn’t have a return statement, while the return type of main is int. But still there is no error.

0-10. Rewrite the Hello, world! program so that a new line occurs everywhere that whitespace is allowed in the program.

Answer:

#include <iostream>

 

int

main (

)

{

 

// insert code here…

std::cout

<<

“Hello, World!\n”;

return

0;

}

 

Hi Visitors!

Greetings to all visitors. I am a amateur in C++, and I am using the book “Accelerated C++” to study C++.

I will share any experience AND any problems I encountered during the learning.

I would like to hear from you advices and comments.

Thank you, in advance.