A:Apple
描述
程序填空,使其輸出4 5 1
#include <iostream> using namespace std; class Apple { // 在此處補充你的代碼 static void PrintTotal() { cout << nTotalNumber << endl; } }; int Apple::nTotalNumber = 0; Apple Fun(const Apple & a) { a.PrintTotal(); return a; } int main() { Apple * p = new Apple[4]; Fun(p[2]); Apple p1,p2; Apple::PrintTotal (); delete [] p; p1.PrintTotal (); return 0; }
輸入無輸出
4
5
1
樣例輸入
None
樣例輸出
4
5
1
來源Guo Wei

1 #include <iostream> 2 using namespace std; 3 class Apple { 4 static int nTotalNumber; 5 public: 6 Apple(){ 7 nTotalNumber++; 8 } 9 ~Apple(){ 10 nTotalNumber--; 11 } 12 13 static void PrintTotal() { 14 cout << nTotalNumber << endl; 15 } 16 17 }; 18 int Apple::nTotalNumber = 0; 19 Apple Fun(const Apple & a) { 20 a.PrintTotal(); 21 return a; 22 } 23 int main() 24 { 25 Apple * p = new Apple[4]; 26 Fun(p[2]); 27 Apple p1,p2; 28 Apple::PrintTotal (); 29 delete [] p; 30 p1.PrintTotal (); 31 return 0; 32 }
B:奇怪的類復制
描述
程序填空,使其輸出9 22 5
#include <iostream> using namespace std; class Sample { public: int v; // 在此處補充你的代碼 }; void PrintAndDouble(Sample o) { cout << o.v; cout << endl; } int main() { Sample a(5); Sample b = a; PrintAndDouble(b); Sample c = 20; PrintAndDouble(c); Sample d; d = a; cout << d.v; return 0; }
輸入無輸出
9
22
5
樣例輸入
None
樣例輸出
9
22
5
來源Guo Wei

1 #include <iostream> 2 using namespace std; 3 class Sample { 4 public: 5 int v; 6 Sample(const Sample&a){ 7 v=a.v+2; 8 } 9 Sample(int i=0){ 10 v=i; 11 } 12 }; 13 void PrintAndDouble(Sample o) 14 { 15 cout << o.v; 16 cout << endl; 17 } 18 int main() 19 { 20 Sample a(5); 21 Sample b = a; 22 PrintAndDouble(b); 23 Sample c = 20; 24 PrintAndDouble(c); 25 Sample d; 26 d = a; 27 cout << d.v; 28 return 0; 29 }
const很重要
C:返回什么才好呢
描述
程序填空,使其按要求輸出
#include <iostream> using namespace std; class A { public: int val; A(int // 在此處補充你的代碼 }; int main() { int m,n; A a; cout << a.val << endl; while(cin >> m >> n) { a.GetObj() = m; cout << a.val << endl; a.GetObj() = A(n); cout << a.val<< endl; } return 0; }
輸入
多組數據,每組一行,是整數 m 和 n輸出先輸出一行:
123
然后,對每組數據,輸出兩行,第一行是m,第二行是n樣例輸入
2 3
4 5
樣例輸出
123
2
3
4
5
來源Guo Wei

1 c=123){ 2 val=c; 3 } 4 A& GetObj(){ 5 return *this; 6 }
我……不知道有this指針的存在……(弱死)
D:第四周程序填空題1
描述
下面程序的輸出是:
3+4i
5+6i
請補足Complex類的成員函數。不能加成員變量。
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; } // 在此處補充你的代碼 }; int main() { Complex a; a = "3+4i"; a.Print(); a = "5+6i"; a.Print(); return 0; }
輸入
無
輸出
3+4i
5+6
i樣例輸入
None
樣例輸出
3+4i
5+6i

1 Complex(char a[]="0"){ 2 r=a[0]-'0'; 3 i=a[2]-'0'; 4 }
emmm……還沒學重載不知道發生了啥,一開始一直RE重載了一下賦值運算符然后突然過了??
E:MyString
描述
補足MyString類,使程序輸出指定結果
#include <iostream> #include <string> #include <cstring> using namespace std; class MyString { char * p; public: MyString(const char * s) { if( s) { p = new char[strlen(s) + 1]; strcpy(p,s); } else p = NULL; } ~MyString() { if(p) delete [] p; } // 在此處補充你的代碼 }; int main() { char w1[200],w2[100]; while( cin >> w1 >> w2) { MyString s1(w1),s2 = s1; MyString s3(NULL); s3.Copy(w1); cout << s1 << "," << s2 << "," << s3 << endl; s2 = w2; s3 = s2; s1 = s3; cout << s1 << "," << s2 << "," << s3 << endl; } }
輸入
多組數據,每組一行,是兩個不帶空格的字符串
輸出
對每組數據,先輸出一行,打印輸入中的第一個字符串三次
然后再輸出一行,打印輸入中的第二個字符串三次樣例輸入
abc def
123 456
樣例輸出
abc,abc,abc
def,def,def
123,123,123
456,456,456
來源Guo Wei

1 ~MyString() { if(p) delete [] p; } 2 MyString(const MyString&s) { 3 p = new char[strlen(s.p) + 1]; 4 strcpy(p, s.p); 5 } 6 void Copy(const char*s) { 7 p = new char[strlen(s) + 1]; 8 strcpy(p, s); 9 } 10 friend ostream & operator << (ostream &output, const MyString &s) { 11 output << s.p; 12 return output; 13 } 14 MyString& operator=(const char*s) { 15 delete[]p; 16 p = new char[strlen(s) + 1]; 17 strcpy(p, s); 18 return *this; 19 } 20 void operator=(MyString& a) { 21 strcpy(p,a.p); 22 }
F:Big & Base 封閉類問題
描述
程序填空,輸出指定結果
#include <iostream> #include <string> using namespace std; class Base { public: int k; Base(int n):k(n) { } }; class Big { public: int v; Base b; // 在此處補充你的代碼 }; int main() { int n; while(cin >>n) { Big a1(n); Big a2 = a1; cout << a1.v << "," << a1.b.k << endl; cout << a2.v << "," << a2.b.k << endl; } }
輸入
多組數據,每組一行,是一個整數
輸出
對每組數據,輸出兩行,每行把輸入的整數打印兩遍樣例輸入
3
4
樣例輸出
3,3
3,3
4,4
4,4
來源Guo Wei

1 Big(int n):v(n),b(n){ 2 }