第六周實驗報告(二)

來源:互聯網
上載者:User
03.#include<iostream>  04.using namespace std;  05.class A  06.{  07.private:  08.    int a;  09.public:  10.    A(){a = 0;}           11.    A(int aa):a(aa){}     //定義建構函式,用參數aa初始化資料成員a  12.};  13.  14.void main()  15.{  16.    A *p;                 //定義類A的指標對象p    17.    p = new A(100);       //用p指向動態對象並初始化為整數   18.    system("pause");  19.}  20.  21.//b.用指標類型作為成員函數的參數  22.  23.#include<iostream>  24.using namespace std;  25.class A  26.{  27.private:  28.    int *a;  29.    int n;  30.    int Maxlen;  31.      32.public:  33.    A(): a(0), n(0), Maxlen(0){}  34.    A(int *aa, int nn, int MM);  35.    ~A();  36.    int GetValue(int i){return a[i];}  37.};  38.  39.A::A(int *aa, int nn, int MM)  40.{  41.    n = nn;  42.    Maxlen = MM;  43.      44.    if(n > Maxlen)  45.    {  46.        exit(1);  47.    }  48.      49.    a = new int[Maxlen];  50.      51.    for(int i = 0; i < n; i++)    //以i為迴圈變數把aa數組中每個元素值傳送給a數組的對應元素中  52.    {  53.        a[i] = aa[i];  54.    }  55.}  56.  57.A::~A()                          //解構函式的類外定義,釋放指標型資料a所指向的空間  58.{  59.    delete []a;  60.}  61.  62.void main()  63.{  64.    int b[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};      65.    A r(b, 10, 10);   66.    int i, s = 0;     67.    for(i = 0; i < 10; i++)     //以i為迴圈變數,把r對象的a資料成員中的每個元素值依次累加到s中  68.    {  69.        s += r.GetValue(i);  70.    }     71.    cout << "s = " << s << endl;  72.}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.