C++ Learning Community Forum
August 01, 2010, 03:27:19 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Chapter 1  (Read 1616 times)
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« on: July 13, 2007, 05:01:10 AM »

Exercise 1-1
Quote
Are the following definitions valid? Why or why not?
Code:
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

Yes, the definitions are valid.

They are the same as the example given earlier in the chapter except a string literal (", world") is concatenate onto the end of a string (hello), rather than the other way round.
« Last Edit: July 13, 2007, 05:07:19 AM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #1 on: July 13, 2007, 05:01:19 AM »

Exercise 1-2
Quote
Are the following definitions valid? Why or why not?
Code:
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;

No, the definitions are not valid.

"Hello" and ", world" are both string literal. Two string literal may not be concatenated using the + operator as stated earlier in the chapter.
« Last Edit: July 13, 2007, 05:07:10 AM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #2 on: July 13, 2007, 05:01:28 AM »

Exercise 1-3
Quote
Is the following program valid? If so, what does it do? If not, why not?
Code:
#include <iostream>
#include <string>

int main()
{
    { const std::string s = "a string";
      std::cout << s << std::endl; }
   
    { const std::string s = "another string";
      std::cout << s << std::endl; }
    return 0;
}

Yes, the program is valid.

A string variable s is initialized with a string literal and then outputted. This happenes twice.

C++ have what's call the One Definition Rule (ODR). What it basically means is that a variable may only be defined once. Also, no two variables may share the same name within the same scope. The ODR is not violated here because the two string initialization happens within completely separate scope, namely they are contained within separate block ({ ... }).
« Last Edit: July 13, 2007, 05:17:38 AM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #3 on: July 13, 2007, 05:01:37 AM »

Exercise 1-4
Quote
What about this one? What if we change }} to };} in the third line from the end?
Code:
#include <iostream>
#include <string>

int main()
{
    { const std::string s = "a string";
      std::cout << s << std::endl;
    { const std::string s = "another string";
      std::cout << s << std::endl; }}
    return 0;
}

Yes, the program is valid.

In this case, the ODR is not violated because the second string s is defined within a nested block to the first, meaning again different scope. In this case, the second string s hides the first one, and hence the second output statement will output another string and not a string.

It is possible to access hidden named variable using the scope resolution operator :: if they are either in global scope, or in a named namespace. It is not possible to access a hidden named variable otherwise, e.g. it is not possible to access the first string s within the scope where the second string s is after it has been defined.

Changing }} to };} have no effect as the ; is simply an empty statement and have no effect on scope resolution.

Note: A variable is only hidden in any particular scope if there exist a variable with the same name in that scope. See the next exercise for when a variable is not hidden.
« Last Edit: July 13, 2007, 05:37:57 AM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #4 on: July 13, 2007, 05:01:47 AM »

Exercise 1-5
Quote
Is this program valid? If so, what does it do? If not, say why not, and rewrite it to be valid.
Code:
#include <iostream>
#include <string>

int main()
{
    { std::string s = "a string";
    { std::string x = s + ", really";
    std::cout << s << std::endl; }
    std::cout << x << std::endl;
    }
    return 0;
}

No, the program is not valid.

At the end of the first }, the string x have come to the end of the scope where it is defined and is hence then destroyed. One may then no longer access the string x as it no longer exist.

There are a number of different way to rewrite this program to be valid. Two methods are shown below.

Code
GeSHi (cpp):
#include <iostream>
#include <string>
 
int main()
{
   { std::string s = "a string";
   std::string x = s + ", really";
   std::cout << s << std::endl;
   std::cout << x << std::endl;
   }
   return 0;
}
Created by GeSHI 1.0.7.18
This one remove the most nested block ({ ... }) making the string x still valid when we get to the output statement.

Code
GeSHi (cpp):
#include <iostream>
#include <string>
 
int main()
{
   { std::string s = "a string";
   std::string x;
   { x = s + ", really";
   std::cout << s << std::endl; }
   std::cout << x << std::endl;
   }
   return 0;
}
Created by GeSHI 1.0.7.18
This one move the definition of the string x to the outer scope to which the output statement is at. The effect is the same as the first solution above.
« Last Edit: July 13, 2007, 05:35:07 AM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #5 on: July 13, 2007, 05:01:57 AM »

Exercise 1-6
Quote
What does the following program do if, when it asks you for input, you type two names (for example, Samuel Beckett)? Predict the behavior before running the program, then try it.
Code:
#include <iostream>
#include <string>

int main()
{
    std::cout << "What is your name? ";
    std::string name;
    std::cin >> name;
    std::cout << "Hello, " << name
              << std::endl << "And what is yours? ";
    std::cin >> name;
    std::cout << "Hello, " << name
              << "; nice to meet you too!" << std::endl;
    return 0;
}

As explained earlier in the book, the >> operator will only read in one word. Hence, during the first input statement, the name Samuel is read in, and subsequently outputted. It should be noted here the greetings outputted is on a newline from the question and user input due to the user having press the Return key to signal the end of user input*. During the second input statement, there is still text in the input stream buffer ( Beckett). This mean the program doesn't wait for user input, instead reading in this second word as the second name (ignoring initial whitespace) and outputting it. It is to be noted the second greetings outputted is not on a new line from the question as program have not been told to start on a newline either by the code, or from user input*.

Nitpicker's corner
* : Standard interactive terminal behaviour considered.
« Last Edit: July 13, 2007, 05:51:34 AM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!