/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱: Student.cpp
* 作 者: 計114-4 張馨
* 完成日期: 2012年 3 月 28 日
* 版 本 號: V 1.0
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程序輸出:按要求輸出
* 程序頭部的註釋結束
*/
#include <iostream> using namespace std; class A { private: int a; public: A() {a = 0;} A(int aa){this->a=aa;} }; void main() { A *p; A p1; p = &p1; } #include <iostream> using namespace std; class A { private: int *a; int n; int MaxLen; public: A():a(0),n(0),MaxLen(0){} A::A(int *aa,int nn,int MM); ~A(); int GetValue(int i){return a[i];} }; A::A(int *aa,int nn,int MM) { n = nn; MaxLen = MM; if(n>MaxLen) exit(1); a = new int [MaxLen]; for (int i = 0; i < MaxLen; i++) { a[i] = *(aa + i); } } A::~A() { delete []a; } void main() { int b[10] = {1,2,3,4,5,6,7,8,9,10}; A r(b,10,10); int i,s = 0; for (i = 0;i < 10;++i) { s = s + r.GetValue(i); }//以i爲循環變量,把r對象的a數據成員中的每個元素值依次累加到s中 cout<<"s = "<<s<<endl; system("PAUSE"); }
上級感言:類、對象每種類型都有固定的套路,理解好了也就容易多了。