实验9 c++

Posted pxxfxxxx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验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 }

 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类型实数。

技术图片

代码示例:

 

以上是关于实验9 c++的主要内容,如果未能解决你的问题,请参考以下文章

这些 C++ 代码片段有啥作用?

有趣的 C++ 代码片段,有啥解释吗? [复制]

以下代码片段 C++ 的说明

C++ 代码片段执行

此 Canon SDK C++ 代码片段的等效 C# 代码是啥?

C++ 代码片段(积累)