编程语言
首页 > 编程语言> > 实验9 c++

实验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 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 }

 

标签:color,Light,教室,c++,int,实验,Chair,include
来源: https://www.cnblogs.com/pxxfxxxx/p/10938271.html