1. in & out
iostream: standard library
istream & ostream
cin; cout; cerr; clog
#include <iostream>
int main()
{
std::cout << "enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << " the sum of " << v1 << " and " << v2 << " is " << v1+v2 << std::endl;
return 0;
}
<< & >> oprator
- two operands
- ostream object << the value needs to print
- << writes the value into the ostream object (write the right thing into the left thing)
- ( std::cout << “enter two numbers:” ) << std::endl
- “enter two numbers:” is a string literal
- endl: a manipulator; to end this line; to ensure the content in the buffer is refreshed into the device; more about buffer
namespace
std::cout & std:endl
- the cout is defined in the std namespace
- to avoid duplicate name
- :: scope operator
The standard library defines different << and >> for different types of variables.
2. comments
one line comments: //
multiple lines comments: /* */
- comments cannot be nested
3. flow of control
3.1 while
#include <iostream>
int main()
{
int sum=0, val=1;
while(val <= 10){
sum += val;
++val;
}
std::cout << "sum is " << sum << std::endl;
return 0;
}
grammar:
while (condition)
statement
- block: multiple statements included in a curly bracket
- +=: compound assignment operator
- ++: prefix increment operator
3.2 for
#include <iostream>
int main()
{
int sum=0;
for (int val=1; val<=10; ++val)
sum += val;
std::cout << "sum is " << sum << std::endl;
return 0;
}
** grammar**
for (init-statement; condition; expression)
3.3
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
- istream is a condiftion; to test the state of a stream
- invalid state of a stream: hit end-of-file signal / invalid input
3.4 complier
one of its job is to detect error (in format)
- syntax error; type error; declaration error;
3.5 if
#include <iostream>
int main()
{
int currVal = 0, val = 0;
if (std::cin >> currVal) { //read one data and make sure there is data avaliable
int cnt = 1;
while (std::cin >> val){ // read remaining data
if (val == currVal)
++cnt;
else {
std::cout << currVal << " occurs "
<< cnt << " times " << std::endl;
currVal = val;
cnt = 1;
}
}
std::cout << currVal << " occurs " << cnt << " times " << std::endl;
}
}
4.
buffer A region of storage used to hold data. IO facilities often store input (or output) in a buffer and read or write the buffer independently from actions in the program. Output buffers can be explicitly flushed to force the buffer to be written. By default, reading cin flushes cout; cout is also flushed when the program ends normally.
member function Operation defined by a class. Member functions ordinarily are
called to operate on a specific object.
method Synonym for member function.
- main function returns 0 by default (only main)
- compile: turn textin programming languages into a binary execuable file for computer to recognize
- compile each cpp file individually into object file, the linker works to stitch each obj file into one exe file
extra viedo How the C++ Compiler Works
- complie: cpp file -> obj file; for cpp, no definition of ‘file’, file is only the source to provide code to compile
- cpp file != translation unit
- pre-process stage: the compiler go through all the pre-processing statements (include, defice, f, etc.)