C++学习笔记1

C++ Prime Plus

Setting out to C++

  • C++ standard function header: int main() void mian();
  • Header Filenames: C++ header files have no .h extension. For example, the C++ version of math.h is the cmath header file;
  • Function form:
    Functions prototypes: void simon(int); double taxes(double); Function: void simon(int n) {...}; double taxes(double t) {...};

Dealing with Data

  • Integer Types:
    A short integer is at least 16 bits wide;
    A int integer is at least as big as short;
    A long integer is at least 32 bits wide and at least as big as int;
    A long long integer is at least 64 bits wide and at least as big as long;
  • Some integer limits
#include 
#include  // use limits.h for older systems
int main()
{
    using namespace std;
    int n_int = INT_MAX; // initialize n_int to max int value
    short n_short = SHRT_MAX; // symbols defined in climits file
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;
    // sizeof operator yields size of type or of variable
    cout << "int is " << sizeof(int) << " bytes." << endl;
    cout << "short is " << sizeof n_short << " bytes." << endl;
    cout << "long is " << sizeof n_long << " bytes." << endl;
    cout << "long long is " << sizeof n_llong << " bytes." << endl;
    cout << endl;
    cout << "Maximum values:" << endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong << endl << endl;
    cout << "Minimum int value = " << INT_MIN << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;
    cin.get();
    return 0;
}

Output:

int is 4 bytes.
short is 2 bytes.
long is 4 bytes.
long long is 8 bytes.

Maximum values:
int: 2147483647
short: 32767
long: 2147483647
long long: 9223372036854775807

Minimum int value = -2147483648
Bits per byte = 8

  • Initialization with C++11
int rheas = {12}; // set rheas to 12
int emus{7}; // set emus to 7`
int rocs = {}; // set rocs to 0`
int psychics{}; // set psychics to 0
  • Display values in hex and octal
#include 
using namespace std;
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
cout << "Monsieur cuts a striking figure!" << endl;
cout << "chest = " << chest << " (decimal for 42)" << endl;
cout << hex; // manipulator for changing number base
cout << "waist = " << waist << " (hexadecimal for 42)" << endl;
cout << oct; // manipulator for changing number base
cout << "inseam = " << inseam << " (octal for 42)" << endl;
return 0;
}
  • C++ Escape Sequence Codes
Character Name ASCII Symbol C++ Code
Newline NL \n
Horizontal tab HT \t
Vertical tab VT \v
Backspace BS \b
Carriage return CR \r
Alert BEL \a
Backslash \ \\
Question mark ? \?
Single quote ' \'
Double quote '' \''
  • The const Qualifier
    The general form for creating a constant is this: const type name = value;
    If your background is in C,you might feel that the #define statement,which is discussed earlier,already does the job adequately. But const is better. For one thing, it lets you specify the type explicitly. Second, you can use C++’s scoping rules to limit the definition to particular functions or files. Third, you can use const with more elaborate types, such as arrays and structures.

你可能感兴趣的:(C++学习笔记1)