首页 > 其他分享 >实验1 类和对象(1)

实验1 类和对象(1)

时间:2022-10-29 17:56:24浏览次数:31  
标签:const cout v1 对象 Clock int 实验 include

task1:

代码:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 int main()
 5 {
 6 using namespace std;
 7 string s1; // 创建一个string对象
 8 string s2{"c plus plus"}; // 创建一个string对象,并初始化
 9 string s3{s2}; // 创建一个string对象,并用s2对其进行初始化
10 string s4 = s2; // 创建一个string对象,并用s2对其进行初始化
11 s1 = "oop";
12 1
13 2
14 3
15 4
16 5
17 6
18 7
19 8
20 9
21 10
22 11
23 12
24 13
25 14
26 测试结果:
27 说明: 
28 1. xx.size() 是容器类对象的方法,返回容器数据项个数。
29 2. xx.begin() , xx.end() , xx.rbegin() , xx.rend() 是序列类容器(container)对象的迭代器
30 (iterator)接口。某种程度上,是比指针更高层的抽象。
31 xx.begin() 指向容器内的第一个数据项, xx.end() 指向容器内最后一个数据项后面的位置;
32 xx.rbegin() 指向容器内的最后一个数据项, xx.rend() 指向容器内第一个数据项前面的位置;
33 迭代器指示的是位置,通过迭代器访问数据项要通过星号运算,即代码中的line30 *itetator 这样的方
34 式。
35 vector<string> v1; // 创建一个vector对象
36 v1.push_back(s1); // 向v1末尾添加数据项s1
37 v1.push_back(s2 + "1");
38 v1.push_back(s3 + "2");
39 v1.push_back(s4 + "3");
40 // 输出方式1:使用自动类型推导、范围for
41 cout << "output1: " << endl;
42 for(auto item: v1)
43 cout << item << endl;
44 // 输出方式2:使用自动类型推导、迭代器
45 cout << "ouput2: ";
46 for(auto p = v1.begin(); p != v1.end(); ++p)
47 cout << *p << endl;
48 // 输出方式3:使用自动类型推导、索引
49 cout << "output3: " << endl;
50 for(auto i = 0; i < v1.size(); ++i)
51 cout << v1[i] << endl;
52 vector<string> v2{v1.rbegin(), v1.rend()}; // 使用vector对象v1极其迭代器,
53 构造对象v2
54 cout << "v2: " << endl;
55 for(auto item: v2)
56 cout << item << endl;
57 }
 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <time.h>
 7 // 定义模板函数,用于输出vector容器对象元素的值
 8 template<typename T>
 9 void output(const T& obj) // 引用作为函数参数
10 {
11 for(auto item: obj)
12 std::cout << item << " ";
13 std::cout << std::endl;
14 }
15 int main()
16 {
17 using namespace std;
18 vector<int> v1{1, 9, 8, 4};
19 v1.insert(v1.begin(), 2022); // 在v1.begin()之前的位置插入
20 v1.insert(v1.end(), 2023); // 在v1.end()之前的位置插入
21 cout << "v1: ";
22 output(v1);
23 v1.pop_back(); // 从v1尾部删除数据项
24 v1.erase(v1.begin()); // 删除v1.begin()位置的数据项
25 cout << "v1: ";
26 output(v1);
27 vector<string> v2{"《1984》", "《动物农场》", "《美丽新世界》"};
28 cout << "v2: ";
29 output(v2);
30 }

 

测试结果:

 

 

 

 

 

 

task2:

代码:

 1 #include <iostream>
 2 using std::cout;
 3 using std::endl;
 4 // 定义Point类
 5 class Point {
 6 public:
 7 Point(int x0 = 0, int y0 = 0);
 8 Point(const Point&p );
 9 ~Point() = default;
10 int get_x() const { return x; } // 内联成员函数
11 int get_y() const { return y; } // 内联成员函数
12 void show() const;
13 private:
14 int x, y;
15 };
16 // Point类的实现
17 // 构造函数(带有默认形参值)
18 Point::Point(int x0, int y0): x{x0}, y{y0} {
19 cout << "constructor called." << endl;
20 }
21 // 复制构造函数
22 // 参数必须是自身对象的引用类型
23 Point::Point(const Point& p): x{p.x}, y{p.y} {
24 cout << "copy constructor called." << endl;
25 }

 

测试结果:

 

 

 

task3:

代码:

 1 #include <iostream>
 2 #include <iomanip>
 3 using std::cout;
 4 using std::endl;
 5 // 定义时钟类Clock
 6 class Clock {
 7 public:
 8 Clock(int h = 0, int m = 0, int s = 0);
 9 Clock(const Clock& t);
10 ~Clock() = default;
11 void set_time(int h, int m = 0, int s = 0);
12 void show_time() const;
13 private:
14 int hour, minute, second;
15 };
16 // 类Clock实现
17 Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s} {
18 cout << "constructor called" << endl;
19 }
20 Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute},
21 second{t.second} {
22 cout << "copy constructor called" << endl;
23 }
24 void Clock::set_time(int h, int m, int s) {
25 hour = h;
26 minute = m;
27 second = s;
28 }
29 void Clock::show_time() const {
30 using std::setw;
31 using std::setfill;
32 cout << setfill('0') << setw(2) << hour << ":"
33 << setw(2) << minute << ":"
34 << setw(2) << second << endl;
35 }
36 // 普通函数定义
37 Clock reset() {
38 return Clock(0, 0, 0); // 构造函数被调用
39 }
40 int main()
41 {
42 Clock c1(12, 0, 5); // 构造函数被调用
43 c1.show_time();
44 c1 = reset(); // 理论上:复制构造函数被调用
45 c1.show_time();
46 Clock c2(c1); // 复制构造函数被调用
47 c2.set_time(6);
48 c2.show_time();
49 }

 

测试结果:

 

 

 

task4:

代码:

 1 class X{
 2 public:
 3 X(); // 默认构造函数
 4 ~X(); // 析构函数
 5 X(int m); // 构造函数
 6 X(const X& obj); // 复制构造函数
 7 X(X&& obj) noexcept; // 移动构造函数
 8 void show() const; // 显示数据
 9 private:
10 int data;
11 };
12 X::X(): data{42} {
13 std::cout << "default constructor called.\n";
14 }
15 X::~X() {
16 std::cout << "destructor called.\n";
17 }
18 X::X(int m): data{m} {
19 std::cout << "constructor called.\n";
20 }
21 X::X(const X& obj): data{obj.data} {
22 std::cout << "copy constructor called.\n";
23 }
24 X::X(X&& obj) noexcept: data{obj.data} {
25 std::cout << "move constructor called.\n";
26 }
27 void X::show() const {
28 std::cout << data << std::endl;
29 }
30 int main() {
31 X x1; //默认构造函数被编译器自动调用
32 x1.show();
33 X x2{2049};
34 x2.show(); // 构造函数被编译器自动调用
35 X x3{x1}; // 复制构造函数被编译器自动调用
36 x3.show();
37 X x4{ std::move(x2) }; // 移动构造函数被编译器调用
38 x4.show();
39 }

 

测试结果:

 

 

 

task5:

代码:

 1 #include <iostream>
 2 #include <iomanip>
 3 // 矩形类Rectangle的定义和实现
 4 class Rectangle
 5 {
 6     public:
 7         Rectangle(int x=2.0,int y=1.0)
 8         {
 9             length=x;
10             width=y;
11         }
12         Rectangle(const Rectangle &a){
13             length=a.len();
14             width=a.wide();
15         }
16         ~Rectangle(){}
17         float len() const{
18             return length;
19         }
20         float wide() const{
21             return width;
22         }
23         float area() const{
24             return length*width;
25         }
26         float circumference() const
27         {
28             return 2*(length+width);
29         }
30         void resize(float times){
31             width*=times;
32             length*=times;
33         }
34         void resize(float l_times,float w_times)
35         {
36             width*=w_times;
37             length*=l_times;
38         }
39     private:
40         float length,width;
41  } ;
42 // 补足代码
43 // ×××
44 // 普通函数, 用于输出矩形信息
45 void output(const Rectangle &rect) {
46 using namespace std;
47 cout << "矩形信息: \n";
48 cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出、小数部分保留两位
49 printf("长:\t%.2f\n",rect.len());// 补足代码:分行输出矩形长、宽、面积、周长
50 printf("宽:\t%.2f\n",rect.wide());
51 printf("面积:\t%.2f\n",rect.area());
52 printf("周长:\t%.2f\n",rect.circumference()); 
53 // ×××
54 }
55 // 主函数,测试Rectangle类
56 int main() {
57 Rectangle rect1; // 默认构造函数被调用
58 output(rect1);
59 Rectangle rect2(10, 5); // 带有两个参数的构造函数被调用
60 output(rect2);
61 Rectangle rect3(rect1); // 复制构造函数被调用
62 rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
63 output(rect3);
64 rect3.resize(5, 2); // 矩形rect3的长缩放5倍, 宽缩放2倍
65 output(rect3);
66 }

 

测试结果:

 

标签:const,cout,v1,对象,Clock,int,实验,include
From: https://www.cnblogs.com/git-porn-hub/p/16839265.html

相关文章

  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、......
  • 实验一:决策树算法实现
    |20大数据三班| 首页-20级大数据3班机器学习-池州学院-班级博客-博客园(cnblogs.com)|学号:201613328|博客后台-博客园(cnblogs.com)实验目的理解决策树算......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验......
  • 使用delegate给方法传递参数,或返回delegate对象
    第一次在博客园写文章。最近遇到一个问题,用到了多线程,以前用的时候线程启动时不需要传递参数,可现在需要时却被难了一把。。还是先说说delegate吧delegate是C#中的一种......
  • 实验一:决策树算法实验
    【实验目的】理解决策树算法原理,掌握决策树算法框架;理解决策树学习算法的特征选择、树的生成和树的剪枝;能根据不同的数据类型,选择不同的决策树算法;针对特定应用场景及......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu22.04.1Desktopamd64三、实......
  • 前端项目实战68-数据处理之一个数组和一个对象
    exportfunctionmyContact(target:any,source:any){for(const[key,value]ofObject.entries(source)){const[name,index]=key.split('-')//要......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;下发指令删除s1上的流表数据。delet......
  • jsp 九大内置对象和四大域对象
    jsp九大内置对象我们打开翻译后的java文件。查看_jspService方法。 request对象请求对象,可以获取请求信息response对象响应对象。可以设置响应信息pageContext对......