The online notes for C++ may be kind of sparse for the next few lectures as I will be trying to keep notes on paper instead (typing with one hand has become increasingly frustrating). The good news is that I intend to follow the C++ book more linearly (with a few minor exceptions) than I followed the C book. As my arm heals, I may come back and redo these notes.
Many of these examples are directly from Koenig & Moo (K&M). You can find a copy of this text on reserve in the library.
Hello world! program (review) (K&M Chapter 0)
Bug! in g++ 2.96. The standard library names are automatically place in the global namespace, This is nonstandard behaviour. g++ 3.x gets it right.
cout and <<
string's (K&M Chapter 1)
std::string::size_type
String operations (concatenation, construction).
/*
Sample execution:
Please enter your first name: Estragon
********************
* *
* Hello, Estragon! *
* *
********************
*/
// ask for a person's name, and generate a framed greeting
#include <iostream>
#include <string>
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;
// build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";
// build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";
// build the first and fifth lines of the output
const std::string first(second.size(), '*');
// write it all
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;
return 0;
}
Last modified: Mon Feb 10 15:31:26 2003