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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/569366.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

迭代器协议、斐波那契数列

f1.__iter__iter(f1)  把一个对象变成可迭代对象 __init__  构造函数 for循环遵循迭代器协议&#xff0c;要求对象有next、iter方法&#xff0c;有iter方法&#xff0c;for循环跟着对象对触发对象的方法&#xff08;协议规定&#xff09; 对象内部要有next方法 斐波那契数列…

[java基础问题] Exception 和 Error

Exception 和 Error Exception 和 Error 都是继承了 Throwable 类, 在 java 中, 只有继承了 Throwable 类才可以使用 throw 抛出, 或者 cath 捕获;Exception 意为 java 运行时可能发生的不合理的情况, 出现时并不会时程序异常退出。其中 Exception 又分为可检查异常, 非可检查异…

关于Kernel的思考

学习播客_KLDA&#xff08;推导得很通俗&#xff0c;下面的推导就是源于此篇博客&#xff09; 第一部分&#xff1a;按照自己的理解&#xff0c;模仿抄&#xff01;学习播客来完成一下KLDA的推导。 第二部分&#xff1a;对于Kernel的思考 KLDA&#xff1a;顾名思义&#xff0c;…

在elementUI中使用 el-autocomplete 实现远程搜索的下拉框

1. 在template加入如下标签 <el-form-item label"文章库" :label-width"formLabelWidth" ><el-autocompletev-model"addTopic.name":fetch-suggestions"querySearchAsync"placeholder"请输入文章标题"select"…

如何用一个例子彻底解释白盒测试中语句覆盖、判定覆盖、条件覆盖、条件判定覆盖、条件组合覆盖?

白盒测试 白盒测试把测试对象看作一个打开的盒子&#xff0c;测试人员依据程序内部逻辑结构相关信息&#xff0c;设计或选择测试用例&#xff0c;对程序所有逻辑路径进行测试&#xff0c;通过在不同点检查程序的状态&#xff0c;确定实际的状态是否与预期的状态一致。 语句覆…

thinkPHP5.0数据查询表达式生成技巧

thinkPHP的查询表达式大揭秘 主要使用where(条件表达式)方法 语法一&#xff1a;where(字段,条件,值)&#xff1b; 等于&#xff1a;EQ 解析为"" 不等于&#xff1a;NEQ 解析为"<>" 小于&#xff1a;LT 解析为"<" 小于等于&#xff1a;…

简易的实现对象内存池

简易的实现对象内存池MemoryNode结构体是保存每一个申请的内存节点&#xff0c;然后构成一个单链表。MemoryNodeList 结构体是保存的是每一块内存&#xff0c;当上一个内存块用完时&#xff0c;再次创建一个内存块。numofMemoryNode&#xff1a;一个内存块的内存节点数量 numof…

连接符合赋值运算符

字符串运算符 以.的形式来连接如&#xff1a;$a.$b; $aacb; $a.efg $aabcefg .相当与$a$a.efg mt_rand()&#xff1b;随机函数 <?php // mt_rand(int $min,int $max) 产生随机数函数 $min ,$max $str"请输入验证码&#xff1a;"; $str.<span style"…

Linux关机操作

转载于:https://www.cnblogs.com/hlc-123/p/10958108.html

JAVA:贪吃蛇源代码

嘿嘿 新手来 表现了&#xff01;&#xff01;&#xff01; 2019年结束 游戏截图&#xff1a; 文件布局&#xff1a; 代码分享 用到的一些全局变量 //全局变量 public class Global {public static final int RECT_WIDTH20;//每个小方格的长宽public static final int WIDTH…

结构

结构 值类型中除了枚举类型都是结构类型的派生类型 由于结构是值类型&#xff0c;并且直接存储数据&#xff0c;因此&#xff0c;在一个对象的主要成员为数据且数据量不大的情况下&#xff0c;使用结构会带来更好的性能。 public struct Address {public string Name;public st…

10 字符串相关操作

# ### 字符串的相关操作 #(1)字符串的拼接 str1 "我爱你" str2 "美丽的祖国" res str1 str2 print(res) str1 str2 # str1 str1 str2 print(str1)# (2)字符串的重复 * str1 "重要的事情说三遍" res str1 * 3 print(res)# (3)字符串跨…

hbase-1.3.2安装

上传安装包hbase-1.3.2-bin.tar.gz解压安装包tar -zxvf /root/hbase-1.3.2-bin.tar.gz -C /usr/local/ 修改配置文件修改hbase-env.shvi hbase-env.sh 修改为以下内容&#xff1a; export JAVA_HOME/usr/local/jdk1.8.0_102 export HBASE_MANAGES_ZKfalse 表示不引用 hbase 自带…

深拷贝的三种方式

JSON方法 var obj2 JSON.parse&#xff08;JSON.stringify(obj1)&#xff09;//深拷贝递归(自己调用自己)方法 判断第一层属性的类型&#xff0c;多层拷贝对象的属性 var obj1 { name: zs, age: 18, dog: { name: 金毛, age: 2 }, friends: [ww, lil] } var obj2 {}; functio…

AppiumForWin安装

尝试安装Windows版本的Appium参考&#xff1a;http://www.cnblogs.com/fnng/p/4540731.html第一步&#xff1a;安装nodehttps://nodejs.org/en/安装成功后使用&#xff1a;node -v&#xff0c;进行验证第二步&#xff1a;安装Appium下面的方法失败&#xff1a;原因下载不成功&a…

剑指offer-二进制中1的个数

题目描述 输入一个整数&#xff0c;输出该数二进制表示中1的个数。其中负数用补码表示。1 class Solution {2 public:3 int NumberOf1(int n) {4 int count 0;5 while(n)6 {7 count ;8 n (n - 1) & n;9 …

activiti5第一弹-----基本的activiti示例

建立一个普通的javaSE工程&#xff0c;工程的目录结构如下&#xff1a;需要的jar包如下&#xff1a; 在config文件夹下创建log4j.properties用来输入日志&#xff0c;内容如下&#xff1a; log4j.rootLoggerINFO, CA # ConsoleAppender log4j.appender.CAorg.apache.log4j.Cons…

activiti5第二弹----使用activiti5提供的测试类进行测试

在前文的基础上改变测试方式 package activiti_001;import org.activiti.engine.impl.test.PluggableActivitiTestCase; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.activiti.engine.test.Deployment; import org…

Android在布局XML中的空格转义符(占位符)

普通的英文半角空格 &#xA0; no-break space &#xff08;普通的英文半角空格但不换行&#xff09; 中文全角空格 &#xff08;一个中文宽度&#xff09; &ensp; en空格 &#xff08;半个中文宽度&#xff09; &emsp; em空格 &#xff08;一个中文宽…

activiti5第三弹-----------脚本任务

首先是流程定义图&#xff1a; 重点关注一下流程节点中 Script Task 节点的 properties中的Main config 这里可以选择不同的脚本语言&#xff0c;由于其他的部怎么会就用javascript来吧。 从图中可以看出只是定义了一个变量x&#xff0c;它的值为 a; 很多人都说这样定义了这个…