code:
// 第二章 变量和基本类型 // 1、基本内置类型 ---------------------------------------------- // 类型字节长 #include <iostream> using namespace std; int main() { cout << sizeof(bool) << endl; cout << sizeof(char) << endl; cout << sizeof(wchar_t) << endl; cout << sizeof(short) << endl; cout << sizeof(int) << endl; cout << sizeof(long) << endl; cout << sizeof(long long) << endl; cout << sizeof(float) << endl; cout << sizeof(double) << endl; cout << sizeof(long double) << endl; return 0; } // 2、字面值常量 ------------------------------------------------- // 字面值常量 #include <iostream> using namespace std; int main() { int i; i=20; //decimal cout << i << endl; i=024; //octal cout << i << endl; i=0x14; // hexadecimal cout << i << endl; return 0; } // concatenated long string literal #include <iostream> using namespace std; int main() { std::cout << "a multi-line " "string literal " "using concatenation" << std::endl; return 0; } // Multi-Line Literals #include <iostream> using namespace std; int main() { // ok: A before a newline ignores the line break /* std::cou t << "Hi" << std::e ndl; */// 这样行不通,通不过编译 // std::cout << "Hi" << std::endl; // multiline string literal // 在字符串中加,可以的 std::cout << "a multi-line string literal using a backslash" << std::endl; return 0; } // 3、变量 ----------------------------------------------------- #include <iostream> int main() { // a first, not very good, solution std::cout << "2 raised to the power of 10: "; std::cout << 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2; std::cout << std::endl; return 0; } #include <iostream> int main() { // local objects of type int int value = 2; int pow = 10; int result = 1; // repeat calculation of result until cnt is equal to pow for(int cnt = 0; cnt != pow; ++cnt) result *= value; // result = result * value; std::cout << value << " raised to the power of " << pow << ": " << result << std::endl; return 0; } #include <iostream> #include <string> using namespace std; int main() { // alternative ways to initialize string from a character string literal string titleA = "C++ Primer, 4th Ed."; string titleB("C++ Primer, 4th Ed."); cout << titleA << endl; cout << titleB << endl; string all_nines(30,'9'); cout << all_nines << endl; return 0; } #include <iostream> #include <string> using namespace std; int main() { // ok: salary defined and initialized before it is used to initialize wage double salary = 9999.99, wage(salary + 0.01); // ok: mix of initialized and uninitialized int interval, month = 8, day = 7, year = 1955; // ok: both forms of initialization syntax used string title("C++ Primer, 4th Ed."), publisher = "A-W"; return 0; } #include <iostream> int main() { int sum = 0; // sum values from 1 up to 10 inclusive for(int val = 1; val <= 10; ++val) sum += val; // equivalent to sum = sum + val std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; } #include <iostream> #include <string> /* Program for illustration purposes only: * It is bad style for a function to use a global variable and then * define a local variable with the same name */ std::string s1 = "hello"; // s1 has global scope int main() { std::string s2 = "world"; // s2 has local scope // uses global s1; prints "hello world" std::cout << s1 << " " << s2 << std::endl; int s1 = 42; // s1 is local and hides global s1 // uses local s1;prints "42 world" std::cout << s1 << " " << s2 << std::endl; return 0; } // 4、const限定符 ------------------------------------------------------ #include <iostream> using namespace std; int main() { const int bufSize = 512; // input buffer size //bufSize = 0; // error: attempt to write to const object const std::string hi = "hello!"; // ok: initialized //const int i, j = 0; // error: i is uninitialized const return 0; } // 5、引用 ------------------------------------------------------------- #include <iostream> int main() { const int ival=1024; const int &refVal=ival; std::cout<<refVal; // int i=42; const int &r=42; const int &r2=r+i; std::cout<<' '<<r2<<std::endl; return 0; } // 6、typedef名字 ------------------------------------------------------- #include <iostream> int main() { typedef double wages; // wages is a synonym for double typedef int exam_score; // exam_score is a synonym for int typedef wages salary; // indirect synonym for double wages hourly, weekly; // double hourly, weekly; exam_score test_result; // int test_result; return 0; } // 7、枚举 -------------------------------------------------------------- #include <iostream> using namespace std; int main() { // shape is 1, sphere is 2, cylinder is 3, polygon is 4 enum Forms { shape = 1, sphere, cylinder, polygon }; // point2d is 2, point2w is 3, point3d is 3, point3w is 4 enum Points { point2d = 2, point2w, point3d = 3, point3w }; Points pt3d = point3d; // ok: point3d is a Points enumerator //Points pt2w = 3; // error: pt2w initialized with int //pt2w = polygon; // error: polygon is not a Points enumerator Points pt2w; pt2w = pt3d; // ok: both are objects of Points enum type // cout << pt3d << endl; cout << pt2w << endl; return 0; } // 8、类类型 ------------------------------------------------------------- // 类的定义 #include <iostream> using namespace std; int main() { class Sales_item { public: // operations on Sales_item objects will go here private: std::string isbn; unsigned units_sold; double revenue; }; struct Sales_item2 { // no need for public label, members are public by default // operations on Sales_item objects private: std::string isbn; unsigned units_sold; double revenue; }; return 0; } // 9、编写自己的头文件 ----------------------------------------------- #include <iostream> using namespace std; int main() { #ifndef SALESITEM_H #define SALESITEM_H // Definition of Sales_itemclass and related functions goes here #endif return 0; }