Skip to main content

C++ Primer Ch1. Introduction


Different compilers use different suffix conventions; the most common include .cc, .cxx, .cpp, .cp, and .C

To compile:

$ CC prog1.cc
prog1.exe is generated on Windows, a.out is generated on UNIX

To run:
$prog1 or $.\prog1



The command used to run the C++ compiler varies across compilers and operating systems.
$ g++ -o prog1 prog1.cc

Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support.

Typically, we put all the #include directives for a program at the beginning of the source file.


std::cout << "Enter two numbers:" << std::endl; 
Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second.  

Equivalent to: (std::cout << "Enter two numbers:") << std::end;

 Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.

Comments:

/* and */
//
Comment Pairs do not nest.



while (std::cin >> value)
Evaluating the while condition executes the expression std::cin >> value
The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. This condition, therefore, tests std::cin.


When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid—that is, if the stream hasn’t encountered an error— then the test succeeds. An istream becomes invalid when we hit end-of-file or encounter an invalid input, such as reading a value that is not an integer. An istream that is in an invalid state will cause the condition to yield false.

 Entering an End-of-File from the Keyboard
When we enter input to a program from the keyboard, different operating systems use different conventions to allow us to indicate end-of-file. On Windows systems we enter an end-of-file by typing a control-z—hold down the Ctrl key and press z—followed by hitting either the Enter or Return key. On UNIX systems, including on Mac OS X machines, end-of-file is usually control-d.


Using File Redirection
It can be tedious to repeatedly type these transactions as input to the programs you are testing. Most operating systems support file redirection, which lets us associate a named file with the standard input and the standard output:
$ addItems <infile >outfile
Assuming $ is the system prompt and our addition program has been compiled into an executable file named addItems.exe (or addItems on UNIX systems), this command will read transactions from a file named infile and write its output to a file named outfile in the current directory.

 

Comments

Popular posts from this blog

C++ Primer Sec. 2.6 Defining our own data structures

Defining our own data structures Class definition must have ; at the end. Preprocessor The most common technique for making it safe to include a header multiple times relies on the preprocessor. #include Header guards #ifndef SALES_DATA_H #define SALES_DATA_H #include struct Sales_data { std::string bookNo; unsigned units_sold = 0; double revenue = 0.0; }; #endif SALES_DATA_H can be any name, just it has to be unique throughout the program. preprocessor variables usually are written in all uppercase.

C++ Primer Sec. 2.5 Dealing with Types

Dealing with Types type alias typedef double wages; // wages is a synonym for double typedef wages base, *p; // base is a synonym for double, p for double* C++11: alias declaration using SI = Sales_item; // SI is a synonym for Sales_item As usual, a const that appears in the base type modifies the given type. The type of pstring is “pointer to char.” So, const pstring is a constant pointer to char—not a pointer to const char. It can be tempting, albeit incorrect, to interpret a declaration that uses a type alias by conceptually replacing the alias with its corresponding type. typedef char *pstring; const pstring cstr = 0; // cstr is a constant pointer to char const pstring *ps; // ps is a pointer to a constant pointer to char const char *cstr = 0; // wrong interpretation of const pstring cstr C++11: auto. auto tells the compiler to deduce the type from the initializer. The initializers for all the variables in the declaration using auto must have types that a...

C++ Primer Sec.2.4 const qualifier

const Qualifier Because we can’t change the value of a const object after we create it, it must be initialized just as reference const int i = get_size(); // ok: initialized at run time const int j = 42; // ok: initialized at compile time const int k; // error: k is uninitialized const when we use an object to initialize another object, it doesn’t matter whether either or both of the objects are consts. int i = 42; const int ci = i; // ok: the value in i is copied into ci int j = ci; // ok:the value in ci is copied into j By Default, const Objects Are Local to a File When we really need to share const between files, define a single instance of a const variable, we use the keyword extern on both its definition and declaration(s). // file_1.cc defines and initializes a const that is accessible to other files extern const int bufSize = fcn(); // file_1.h extern const int bufSize; // same bufSize as defined in file_1.cc References to const const int ci = 102...