第1章 預備知識 && 第2章 開始學習C++

目錄

1.1 C++簡介

1.2 C++簡史

1.2.1 C語言

1.2.2 C語言的編譯原理

1.2.3 面向對象編程

1.2.4 C++和範型編程

1.2.5 C++的起源

1.3 可移植性和標準

1.3.1 C++的發展

1.3.2 本書遵循的C++標準

1.4 程序創建的技巧

1.4.1 創建源代碼文件

1.4.2 編譯和鏈接

1.5 總結

 

2.1 進入C++

2.1.1 main()函數

2.1.2 C++註釋

2.1.3 C++預處理器和iostream文件

2.1.4 頭文件名

2.1.5 名稱空間

2.1.6 使用cout進行C++輸出

2.1.7 C++源代碼的格式化

2.2 C++語句

2.2.1 聲明語句和變量

2.2.2 賦值語句

2.2.3 cout的新花樣

2.3 其他C++語句

2.3.1 使用cin

2.3.2 使用cout進行拼接

2.3.3 類簡介

2.4 函數

2.4.1 使用有返回值的函數

2.4.2 函數變體

2.4.3 用戶定義的函數

2.4.4 用戶定義的有返回值的函數

2.4.5 在多函數程序中使用using編譯指令

2.5 總結


C語言和C++的發展歷史和基本原理

過程性編程和麪向對象編程

C++是如何在C語言的基礎上添加面向對象概念的

C++是如何在C語言的基礎上添加泛型編程概念的

編程語言標準

創建程序的技巧

1.1 C++簡介

C語言代表的過程性語言
C++在C語言基礎上添加的類代表的面嚮對象語言
C++模版支持的泛型編程

1.2 C++簡史

1.2.1 C語言

彙編語言依賴特定計算機處理器的內部機器語言,要將彙編程序移植到另一種計算機上,必須使用不同的彙編語言重新編寫程序。

針對不同硬件,一種被稱爲編譯器的特殊程序將高級語言翻譯成特定計算機的內部語言。

將低級語言的效率、硬件訪問能力和高級語言的通用性、可移植性融合在一起,開發了C語言。

1.2.2 C語言的編譯原理

過程性語言。強調算法方面,即菜譜指定廚師做蛋糕時遵循的步驟
結構性編程。良好的結構分支,如for、while、if else..
自頂向下。開發函數來表示各個任務模塊

1.2.3 面向對象編程

OOP試圖語言來滿足問題的要求,其理念是設計與問題的本質特性相對應的數據格式。

OOP將數據和方法合併爲類定義。類是規範,規定了可使用哪些數據來表示對象以及可以對這些數據執行哪些操作。對象是根據這種規範構造的特定數據結構。

1.2.4 C++和泛型編程

重用代碼和抽象通用概念的技術更簡單,OOP強調的是編程的數據方面,泛型強調獨立於特定數據類型。

1.2.5 C++的起源

1.3 可移植性和標準

兩個障礙:
1.硬件特定的程序是不可移植的
2.語言上的差異

ANSI
C++98表示C++98/C++2003
C++11

1.3.1 C++的發展

1.3.2 本書遵循的C++標準

1.4 程序創建的技巧

1.4.1 創建源代碼文件

1.4.2 編譯和鏈接

1.UNIX編譯和鏈接
    CC
2.Linux編譯和鏈接
    g++
3.Windows命令行編輯器
    Cygwin MinGW
4.Windows編譯器
5.Macintosh上的C++

1.5 總結

 

創建C++程序

C++程序的一般格式

#include編譯指令

main()函數

使用cout對象進行輸出

在C++程序中加入註釋

何時以及如何使用endl

聲明和使用變量

使用cin對象進行輸入

定義和使用簡單函數

2.1 進入C++

// myfirst.cpp--displays a message

#include <iostream>                           // a PREPROCESSOR directive
int main()                                    // function header
{                                             // start of function body
    using namespace std;                      // make definitions visible
    cout << "Come up and C++ me some time.";  // message
    cout << endl;                             // start a new line
    cout << "You won't regret it!" << endl;   // more output
// If the output window closes before you can read it,
// add the following code:
    // cout << "Press any key to continue." <<endl;
	// cin.get();                                                   
    return 0;                                 // terminate main()
}           
[wlsh@wlsh-MacbookPro] chapter_2$ g++ myfirst.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
Come up and C++ me some time.
You won't regret it!

2.1.1 main()函數

C++句法要求main()函數的定義以函數頭int main()開始

特殊的是main()被啓動代碼調用,而啓動代碼是由編譯器添到程序中的,是程序和操作系統之間的橋樑。事實上,該函數頭描述的是main()和操作系統之間的接口。

C++標準格式

2.1.2 C++註釋

2.1.3 C++預處理器和iostream文件

#include <iostream>
using namespace std;
目的:簡化程序
預處理器將iostream文件內容添加到程序中。

2.1.4 頭文件名

C++對老式的頭文件保留了擴展名,而C++頭文件則沒有擴展名

2.1.5 名稱空間

C++特性之名稱空間,讓廠商能夠將將其產品封裝在一個叫做名稱空間的單元中,用名稱空間的名稱來指出想使用哪個廠商的產品。如Microflp::wanda()、Pisscine::wanda()

名稱空間std,爲了不必麻煩,using編譯指令,可以使用std名稱空間中定義的名稱,而不必使用std::前綴
using namespace std

進一步更好的做法
using std::cout;

2.1.6 使用cout進行C++輸出

cout << "lalaplus.";

"" 雙引號括起的是字符串
<< 該語句將把這個字符串發給cout,該符號指出信息流動的路徑
cout是預定義的對象,優點不用瞭解對象的內部情況,就可以使用它,只需要知道它的接口,,即如何使用它

編譯器通過上下文識別運算符重載

控制符(manipulator)
cout << “lalalaplus.\n”;
cout << “lalalaplus." << endl;

老式的換行風格。
end:重起一行,在輸出流中插入endl將導致屏幕光標移到下一行還頭。

差別:endl確保程序繼續運行前刷新輸出(將其立即顯示在屏幕上)而“\n”不能保證

2.1.7 C++源代碼的格式化

在C和C++中,不能把空格、製表符或回車放在元素中間,也不能把回車放在字符串中間。

不可分割的元素叫標記(token),通常使用空格、製表符將兩個標記分開。
return0; 錯誤
return(0);
intmain();錯誤
int main ();

2.2 C++語句

// carrots.cpp -- food processing program
// uses and displays a variable

#include <iostream>

int main()
{
    using namespace std;
    
    int carrots;            // declare an integer variable
    
    carrots = 25;            // assign a value to the variable
    cout << "I have ";
    cout << carrots;        // display the value of the variable
    cout << " carrots.";
    cout << endl;
    carrots = carrots - 1;  // modify the variable
    cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
    // cin.get();
	return 0;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ carrots.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
I have 25 carrots.
Crunch, crunch. Now I have 24 carrots.

2.2.1 聲明語句和變量

int carrots;
信息:需要的內存及內存單元的名稱。編譯器負責分配和標記內存的細節。
即carrots來標識存儲在該內存單元中的值。爲變量,值可以修改

C++所有變量都必須聲明。在首次使用變量前聲明就可以了。

2.2.2 賦值語句

2.2.3 cout的新花樣

cout << carrots
cout將carrots替換爲其當前值23,然後把值轉換爲合適的輸出字符。
整數25存儲爲二進制數,字符串25存儲的是字符2和字符5的編碼,這是cout的智能之處
源自C++的面向對象特性,插入運算符(<<)將根據其後的數據類型相應地調整行爲,這是一個運算符重載的例子。


與C語言的複雜性不通,必須用特殊代碼指出是打印字符串還是整數,假如想打印字符串,而又提供整數,printf()不夠精密,發現不了錯誤。
print("Printing a string: %s\n","25");
print("Printing a integer: %d\n",25);

2.3 其他C++語句

// getinfo.cpp -- input and output
#include <iostream>

int main()
{
    using namespace std;
    
    int carrots;
    
    cout << "How many carrots do you have?" << endl;
    cin >> carrots;                // C++ input
    cout << "Here are two more. ";
    carrots = carrots + 2;
// the next line concatenates output
    cout << "Now you have " << carrots << " carrots." << endl;
	// cin.get();
	// cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ getinfo.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
How many carrots do you have?
12
Here are two more. Now you have 14 carrots.

2.3.1 使用cin

cin.get()在您輸入數字並按Enter鍵時讀取輸入。

信息流從cin流向carrots,C++將輸出看作是流出程序的字符流一樣,它將輸入看作是流入程序的字符流。

iostream將cin定義爲一個表示流的對象,在運算符右側插入一個變量,在接受抽取的信息用符號(<<>>)指示信息流的方向。

2.3.2 使用cout進行拼接

cout << "Now you have " << carrots << " carrots." << endl;
cout語句的輸出緊跟在前一條cout語句的輸出後面,即使兩條cout語句之前有其他語句。

這些對象都很智能化,cin知道如何將輸入流中的信息轉化爲double類型,cout知道如何將double類型的插入到輸出流中。

2.3.3 類簡介

類是用戶定義的數據類型。
類描述的是數據格式及其用法,而對象是根據數據格式規範創建的實體。
類之於對象 = 類型之於變量。

cout是一個ostream類對象
cin是一個istream類對象
這些類定義位於iostream文件中,沒有內置到編譯器中。

總之,類描述了一種數據類型的全部屬性(它執行的操作),對象是根據這些描述創建的實體。

2.4 函數

2.4.1 使用有返回值的函數

在使用函數之前,C++編譯器必須知道函數的參數類型和返回值類型。
函數原型之於函數就像變量聲明之於變量--指出涉及的類型。

double sqrt(double);
分號表明它是一條語句,這使得它是一個原型,而不是函數頭。

提供原型的兩種方法:
1.源代碼文件中輸入函數原型
2.包含頭文件cmath中定義了原型。

函數原型 不等於 函數定義。 原型只是描述接口,描述的是發送給函數的信息和返回的信息;而定義包含函數的代碼。

另外,C++允許在程序的任何地方聲明新變量,還允許在創建變量時對它進行復制,這叫初始化(initialization)
double side = sqrt(area);
// sqrt.cpp -- using the sqrt() function

#include <iostream>
#include <cmath>    // or math.h

int main()
{
    using namespace std;
   
    double area;
    cout << "Enter the floor area, in square feet, of your home: ";
    cin >> area;
    double side;
    side = sqrt(area);
    cout << "That's the equivalent of a square " << side 
         << " feet to the side." << endl;
    cout << "How fascinating!" << endl;
	// cin.get();
	// cin.get();
    return 0;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ getinfo.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
How many carrots do you have?
12
Here are two more. Now you have 14 carrots.
[wlsh@wlsh-MacbookPro] chapter_2$ g++ sqrt.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
Enter the floor area, in square feet, of your home: 10
That's the equivalent of a square 3.16228 feet to the side.
How fascinating!
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
Enter the floor area, in square feet, of your home: 16
That's the equivalent of a square 4 feet to the side.
How fascinating!

2.4.2 函數變體

double pow(double , double);
answer = pow(5.0 , 8.0);

2.4.3 用戶定義的函數

// ourfunc.cpp -- defining your own function
#include <iostream>
void simon(int);    // function prototype for simon()

int main()
{
    using namespace std;
    simon(3);       // call the simon() function
    cout << "Pick an integer: ";
    int count;
    cin >> count;
    simon(count);   // call it again
    cout << "Done!" << endl;
	// cin.get();
    // cin.get();
    return 0;
}

void simon(int n)   // define the simon() function
{
    using namespace std;

    cout << "Simon says touch your toes " << n << " times." << endl;
}                   // void functions don't need return statements
[wlsh@wlsh-MacbookPro] chapter_2$ g++ ourfunc.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
Simon says touch your toes 3 times.
Pick an integer: 512
Simon says touch your toes 512 times.
Done!

2.4.4 用戶定義的有返回值的函數

// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int);     // function prototype
int main()
{
    using namespace std;
    int stone;
    cout << "Enter the weight in stone: ";
    cin >> stone;
    int pounds = stonetolb(stone);
    cout << stone << " stone = ";
    cout << pounds << " pounds." << endl;
	// cin.get();
    // cin.get();
    return 0;
}

int stonetolb(int sts)
{
     return 14 * sts;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ convert.cpp 
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out 
Enter the weight in stone: 15
15 stone = 210 pounds.
將函數比作一個由出入她們的信息所指定的黑盒子(block boxes)

2.4.5 在多函數程序中使用using編譯指令

2.5 總結

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章