c++ II

Posted by Hux Blog on May 24, 2016

Chapter 1

compile & execute in command line

  1. g++ main.cpp(enable possible warnings,use g++ -Wall,e.g.Warnings All)
    • g++ -o result main.cpp (in this way,generate execute file result;default a.out)
  2. ./a.out (‘/’use in linux & ‘'use in Win;to execute,we need to specify the current location:’./’)
  3. echo $?(to see status,e.g. return 0 or else)

iostream(Standard Library)

  1. #include to use iostream
  2. cin & cout define in the namespace std,so if without “using namespace std”,it will be std::cin、std:cout、std::endl
  3. cin»val cout«val,both of them return the lefthand,e.g. if input is an int,then cin»val succeed to read an int into val,then return true; so we can use while(cin»val) to keep record the input when input is an int.

comments

  1. single-line: // end with a new line
  2. paired: /* * / when span multiple lines, it is better to use * in every line
    note:this type can’t appear inside again.it will result in mysterious errors.

flow of control

1.for(i=0;i<100;i++), when you want to iterate by 2 not 1,use while in which i=i+2;

class

  1. every class defines a type,likewise structure.The type name is class name.eg,we define a class Sales_item, and Sales_item item,means we define item as a object of type Sales_item,just like the built-in types.
  2. some languages just allow to define types that specify only what data make up the type; c++ allow operator as well as data.

others

  1. sum += val e.g.sum=sum+val;
  2. Headers from Standard Library are enclosed in <>;Other headers(we difine) are enclosed in “”.