实验9 c++
problem :A 第一個類
?
#include <iostream> #include <iomanip> #include <cstring> #include <cmath> using namespace std;class Thing { private:string name; public:Thing(){}Thing(string n):name(n) {cout << "" << name << endl;}~Thing(){ cout << "Destroy a thing" << " " << name << endl;}};int main() {Thing A("Car");string str;cin>>str;Thing B(str);return 0; }?problem b:建造一間教室
description:
一間教室包括很多物品。這里只考慮燈(Light)和椅子(Chair)。定義Light類,只有一個int類型的參數,表示燈的瓦數;定義Chair類,只有一個字符串類型的參數,表示椅子的顏色。定義ClassRoom類,包括四個屬性:兩個int類型的屬性,分別為燈的個數和椅子的個數。一個Light類的對象和一個Chair類的對象,分別為教室中燈的種類和椅子的種類。
input:
輸入有6行,第1行是一個正整數,表示燈的瓦數。第2行是一個不含空白符的字符串,表示椅子的顏色。第3、4行表示教室中燈的個數和椅子的數量。第5行是一個正整數,表示教室中燈的瓦數,第6行是一個不含空白符的字符串,表示教室中椅子的顏色。
sample input:
20
blue
16
100
25
red
sample output:
代碼示例:
1 #include <iostream> 2 #include <iomanip> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 class Light 7 { 8 private: 9 int w; 10 public: 11 Light(){ } 12 Light(int ww):w(ww){ cout << "A " << w << "w light is created." << endl; } 13 ~Light(){ cout << "A " << w << "w light is erased." << endl;} 14 int getw() const { return w; } 15 }; 16 class Chair 17 { 18 private: 19 string cl; 20 public: 21 Chair(){ } 22 Chair(string c):cl(c){ cout << "A " << cl << " chair is created." << endl; } 23 ~Chair(){ cout << "A " << cl << " chair is created." << endl;} 24 string getcl() const { return cl; } 25 }; 26 27 class ClassRoom 28 { 29 private: 30 int lnum, cnum; 31 Light lig; 32 Chair cha; 33 public: 34 ClassRoom(){ } 35 ClassRoom(int ln,int cn,int w,string c ): lig(w), cha(c),lnum(ln), cnum(cn) 36 { 37 cout << "A classroom having " << lnum << " lights and " << cnum << " chairs is created." << endl; 38 } 39 ~ClassRoom(){ cout << "A classroom having " << lnum << " lights and " << cnum << " chairs is erased." << endl;} 40 }; 41 42 int main() 43 { 44 int nl, nc; 45 int w; 46 string color; 47 cin>>w>>color; 48 Light light(w); 49 Chair chair(color); 50 cin>>nl>>nc; 51 cin>>w>>color; 52 ClassRoom room(nl, nc, w, color); 53 return 0; 54 }problem c:? 是否回文數?
description :
定義Data類,有一個int類型的屬性。定義其構造函數、setValue函數和isPalindrome函數,其中setValue函數用于設置屬性值,isPalindrome用于判斷該屬性值是否為回文數。判斷回文數時,不考慮數的符號。
輸入:
若干個int類型范圍內的整數
輸出:
每個輸入對應一行輸出,如果對應的輸入是回文數,則輸出Yes,否則輸出No。
代碼演繹:
1 #include <iostream> 2 #include <iomanip> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 class Data 7 { 8 private: 9 int data; 10 public: 11 Data(int d = 0) : data(d) { } 12 ~Data(){ } 13 public: 14 void setValue(int v) { data = v; } 15 bool isPalindrome() const 16 { 17 int temp; 18 temp = data; 19 if(data < 0 ) temp = -temp; 20 int tt[100]; 21 int i = 0; 22 while(temp != 0) 23 { 24 tt[i] = temp % 10; 25 temp /= 10; 26 i++; 27 } 28 for(int j = 0, k = i-1; j < i;j++,k-- ) 29 if(tt[j] != tt[k]) 30 return false; 31 return true; 32 } 33 }; 34 int main() 35 { 36 Data data; 37 int v; 38 while (cin>>v) 39 { 40 data.setValue(v); 41 if (data.isPalindrome()) 42 cout<<"Yes"<<endl; 43 else 44 cout<<"No"<<endl; 45 } 46 return 0; 47 }?
?problem d:Base 與 Derived
description:
定義Base和Derived類,Derived類是Base類的子類,兩個類都只有1個int類型的屬性。定義它們的構造函數和析構函數,輸出信息如樣例所示。
input;
輸入2個整數。
output:
見樣例
代碼樣例:
1 #include <iostream> 2 #include <iomanip> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 class Base 7 { 8 private: 9 int bs; 10 public: 11 Base(int b = 0):bs(b) { cout << "Base " << bs << " is created." << endl; } 12 ~Base() { cout << "Base " << bs << " is created." << endl;} 13 }; 14 class Derived:public Base 15 { 16 private: 17 int de; 18 public: 19 Derived(int x, int y):Base(x),de(y) { cout << "Derived " << de << " is created." << endl; } 20 ~Derived(){ cout << "Derived " << de << " is created." << endl; } 21 }; 22 int main() 23 { 24 int a, b; 25 cin>>a>>b; 26 Base base(a); 27 Derived derived(a, b); 28 return 0; 29 }problem e: 類模板sample
description:
定義類模板Sample,設模板參數為T,則Sample類只有一個T類型的屬性。定義其構造函數、拷貝構造函數,輸出與樣例類似的信息。定義show函數,用于顯示屬性值(只輸出屬性值)。定義add函數,將當前對象與Sample類的另一個對象的屬性值相加,和仍存入當前對象。
input:
輸入2個int類型整數、2個double類型實數。
代碼示例:
1 #include <iostream> 2 #include <iomanip> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 template <class T> 7 class Sample 8 { 9 private: 10 T t; 11 public: 12 Sample(T tt) : t(tt){ cout << "Sample " << t << " is created." << endl; } 13 Sample(const Sample & s):t(s.t) { cout << "Sample " << t << " is copied." << endl;} 14 ~Sample() { } 15 public: 16 void show()const{ cout << t << endl; } 17 void add(Sample s){ t += s.t; } 18 }; 19 20 21 int main() 22 { 23 int a, b; 24 double c, d; 25 cin>>a>>b>>c>>d; 26 Sample<int> s1(a), s2(b), s3(s1); 27 Sample<double> s4(c), s5(d), s6(s5); 28 s1.add(s2); 29 s1.show(); 30 s5.add(s4); 31 s5.show(); 32 return 0; 33 }?
轉載于:https://www.cnblogs.com/pxxfxxxx/p/10938271.html
總結
- 上一篇: Jmeter之性能测试类型
- 下一篇: 如何提高写前端的效率?干货,快进!