日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

实验9 c++

發(fā)布時間:2025/3/15 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实验9 c++ 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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類型的參數(shù),表示燈的瓦數(shù);定義Chair類,只有一個字符串類型的參數(shù),表示椅子的顏色。定義ClassRoom類,包括四個屬性:兩個int類型的屬性,分別為燈的個數(shù)和椅子的個數(shù)。一個Light類的對象和一個Chair類的對象,分別為教室中燈的種類和椅子的種類。

input:

輸入有6行,第1行是一個正整數(shù),表示燈的瓦數(shù)。第2行是一個不含空白符的字符串,表示椅子的顏色。第3、4行表示教室中燈的個數(shù)和椅子的數(shù)量。第5行是一個正整數(shù),表示教室中燈的瓦數(shù),第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:? 是否回文數(shù)?

description :

定義Data類,有一個int類型的屬性。定義其構造函數(shù)、setValue函數(shù)和isPalindrome函數(shù),其中setValue函數(shù)用于設置屬性值,isPalindrome用于判斷該屬性值是否為回文數(shù)。判斷回文數(shù)時,不考慮數(shù)的符號。

輸入:

若干個int類型范圍內的整數(shù)

輸出:

每個輸入對應一行輸出,如果對應的輸入是回文數(shù),則輸出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類型的屬性。定義它們的構造函數(shù)和析構函數(shù),輸出信息如樣例所示。

input;

輸入2個整數(shù)。

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,設模板參數(shù)為T,則Sample類只有一個T類型的屬性。定義其構造函數(shù)、拷貝構造函數(shù),輸出與樣例類似的信息。定義show函數(shù),用于顯示屬性值(只輸出屬性值)。定義add函數(shù),將當前對象與Sample類的另一個對象的屬性值相加,和仍存入當前對象。

input:

輸入2個int類型整數(shù)、2個double類型實數(shù)。

代碼示例:

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

總結

以上是生活随笔為你收集整理的实验9 c++的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。