最近好忙,一來要在店裡看店,二來朋友辦結婚酒,搞的我這幾天好疲憊啊···博客又有好幾天沒提筆了。
下午簡單看了下書,看到了類的部分,自己動手練習了一下
筆記:1.類是數據類型 / 它的變童就是對象
格式:
class 類名
{
public:
//公有的...成員
private:
//私有的...成員
};
一個名為Myclass類的代碼:
1 /*
2 一個簡單的類例子
3 */
4 #pragma hdrstop
5 #include <tchar.h>
6 #include <iostream>
7 #pragma argsused
8 using namespace std;
9
10 class Myclass
11 {
12 public: //關鍵字public 表示公開的屬性和方法, 外界可以直接訪問或者調用。
13 int Add(int x,int y) //在類內聲明函數
14 {
15 return x + y; //兩數相加
16 }
17
18
19 } ; //需注意類定義結束部分的分號不能省略。
20
21 int _tmain(int argc, _TCHAR* argv[])
22 {
23 Myclass M; //用定義好的類創建一個對象 點M
24 int i,n,sum = 0;
25 cin >> i ; //輸入
26 cin >> n ;
27 sum = M.Add(i,n) ; //調用類M下的 Add函數 實現加法運算 把函數返回值賦給sum變量
28 cout << "運算結果:" << sum << endl ; //輸出結果
29 system("pause");
30 return 0;
31
32 }
33 //---------------------------------------------------------------------------
圖解:
