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

实验1 类和对象

时间:2023-10-13 13:33:10浏览次数:28  
标签:const cout 对象 double void int 实验 include

实验任务1

task1.cpp源码

 1 // 标准库string, vector, array基础用法
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <array>
 6 
 7 // 函数模板
 8 // 对满足特定条件的序列类型T对象,使用范围for输出
 9 template<typename T>
10 void output1(const T &obj) {
11     for (auto i : obj)
12         std::cout << i << ", ";
13     std::cout << "\b\b \n";
14 }
15 
16 // 函数模板
17 // 对满足特定条件的序列类型T对象,使用迭代器输出
18 template<typename T>
19 void output2(const T &obj) {
20     for (auto p = obj.begin(); p != obj.end(); ++p)
21         std::cout << *p << ", ";
22     std::cout << "\b\b \n";
23 }
24 
25 // array模板类基础用法
26 void test_array() {
27     using namespace std;
28     array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化
29     cout << "x1.size() = " << x1.size() << endl; // 输出元素个数
30     x1.fill(42); // 把x1的所有元素都用42填充
31     x1.at(0) = 999; // 把下标为0的元素值修改为999
32     x1[4] = -999; // 把下表为4的元素值修改为-999
33     cout << "x1: ";
34     output1(x1); // 调用模板函数output1输出x1
35     cout << "x1: ";
36     output2(x1); // 调用模板函数output1输出x1
37     array<int, 5> x2{x1};
38     cout << boolalpha << (x1 == x2) << endl;
39     x2.fill(22);
40     cout << "x2: ";
41     output1(x2);
42     swap(x1, x2); // 交换array对象x1, x2
43     cout << "x1: ";
44     output1(x1);
45     cout << "x2: ";
46     output1(x2);
47 }
48 
49 // vector模板类基础用法
50 void test_vector() {
51     using namespace std;
52     vector<int> v1;
53     cout << v1.size() << endl; // 输出目前元素个数
54     cout << v1.max_size() << endl; // 输出元素个数之最大可能个数
55     v1.push_back(55); // 在v1末尾插入元素
56     cout << "v1: ";
57     output1(v1);
58     vector<int> v2 {1, 0, 5, 2};
59     v2.pop_back(); // 从v2末尾弹出一个元素
60     v2.erase(v2.begin()); // 删除v2.begin()位置的数据项
61     v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入
62     v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入
63     cout << v2.size() << endl;
64     cout << "v2: ";
65     output2(v2);
66     vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是42
67     cout << "v3: ";
68     output1(v3);
69     vector<int> v4(v3.begin(), v3.end() - 2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值
70     cout << "v4: ";
71     output1(v4);
72 }
73 
74 // string类基础用法
75 void test_string() {
76     using namespace std;
77     string s1{"oop"};
78     cout << s1.size() << endl;
79     for (auto &i : s1)
80         i -= 32;
81     s1 += "2023";
82     s1.append(", hello");
83     cout << s1 << endl;
84 }
85 
86 int main() {
87     using namespace std;
88     cout << "===========测试1: array模板类基础用法===========" << endl;
89     test_array();
90     cout << "\n===========测试2: vector模板类基础用法===========" << endl;
91     test_vector();
92     cout << "\n===========测试3: string类基础用法===========" << endl;
93     test_string();
94 }
View Code

运行测试结果

 

 

实验任务2

task2.cpp源码

 1 #include <iostream>
 2 #include <complex>
 3 
 4 // 测试标准库提供的复数类模板complex
 5 void test_std_complex() {
 6     using namespace std;
 7     complex<double> c1{3, 4}, c2{4.5};
 8     const complex<double> c3{c2};
 9     cout << "c1 = " << c1 << endl;
10     cout << "c2 = " << c2 << endl;
11     cout << "c3 = " << c3 << endl;
12     cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag()
13          << endl;
14     cout << "c1 + c2 = " << c1 + c2 << endl;
15     cout << "c1 - c2 = " << c1 - c2 << endl;
16     cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库数学函数,对复数取模
17     cout << boolalpha; // 设置bool型值以true/false方式输出
18     cout << "c1 == c2: " << (c1 == c2) << endl;
19     cout << "c3 == c2: " << (c3 == c2) << endl;
20     complex<double> c4 = 2;
21     cout << "c4 = " << c4 << endl;
22     c4 += c1;
23     cout << "c4 = " << c4 << endl;
24 }
25 
26 int main() {
27     test_std_complex();
28 }
View Code

运行测试结果

 

实验任务3

task3.cpp源码

  1 // 一个简单的类T:定义、使用
  2 #include <iostream>
  3 #include <string>
  4 using namespace std;
  5 
  6 // 类T的声明
  7 class T {
  8     public:
  9         T(int x = 0, int y = 0); // 带有默认形值的构造函数
 10         T(const T &t); // 复制构造函数
 11         T(T &&t); // 移动构造函数
 12         ~T(); // 析构函数
 13         void set_m1(int x); // 设置T类对象的数据成员m1
 14         int get_m1() const; // 获取T类对象的数据成员m1
 15         int get_m2() const; // 获取T类对象的数据成员m2
 16         void display() const; // 显示T类对象的信息
 17         friend void func(); // 声明func()为T类友元函数
 18     private:
 19         int m1, m2;
 20     public:
 21         static void disply_count(); // 类方法,显示当前T类对象数目
 22     public:
 23         static const string doc; // 类属性,用于描述T类
 24         static const int max_count; // 类属性,用于描述T类对象的上限
 25     private:
 26         static int count; // 类属性,用于描述当前T类对象数目
 27 };
 28 
 29 // 类的static数据成员:类外初始化
 30 const string T::doc{"a simple class"};
 31 const int T::max_count = 99;
 32 int T::count = 0;
 33 
 34 // 类T的实现
 35 T::T(int x, int y): m1{x}, m2{y} {
 36     ++count;
 37     cout << "constructor called.\n";
 38 }
 39 T::T(const T &t): m1{t.m1}, m2{t.m2} {
 40     ++count;
 41     cout << "copy constructor called.\n";
 42 }
 43 T::T(T &&t): m1{t.m1}, m2{t.m2} {
 44     ++count;
 45     cout << "move constructor called.\n";
 46 }
 47 T::~T() {
 48     --count;
 49     cout << "destructor called.\n";
 50 }
 51 void T::set_m1(int x) {
 52     m1 = x;
 53 }
 54 int T::get_m1() const {
 55     return m1;
 56 }
 57 int T::get_m2() const {
 58     return m2;
 59 }
 60 void T::display() const {
 61     cout << m1 << ", " << m2 << endl;
 62 }
 63 
 64 // 类方法
 65 void T::disply_count() {
 66     cout << "T objects: " << count << endl;
 67 }
 68 
 69 // 友元函数func():实现
 70 void func() {
 71     T t1;
 72     t1.set_m1(55);
 73     t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问
 74     t1.display();
 75 }
 76 
 77 // 测试
 78 void test() {
 79     cout << "T class info: " << T::doc << endl;
 80     cout << "T objects max_count: " << T::max_count << endl;
 81     T::disply_count();
 82     T t1;
 83     t1.display();
 84     t1.set_m1(42);
 85     T t2{t1};
 86     t2.display();
 87     T t3{std::move(t1)};
 88     t3.display();
 89     t1.display();
 90     T::disply_count();
 91 }
 92 
 93 // 主函数
 94 int main() {
 95     cout << "============测试类T============" << endl;
 96     test();
 97     cout << endl;
 98     cout << "============测试友元函数func()============" << endl;
 99     func();
100 }
View Code 运行测试结果

 

实验任务4

task4.cpp源码

 1 #include <iostream>
 2 #include <string>
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 // 矩形类Rect的定义
 7 class Rect {
 8     private:
 9         static int size;
10     public:
11         static const string doc;
12         static int size_info() {
13             return size;
14         }
15         friend void test();
16     private:
17         double length, width;
18     public:
19         Rect(double l = 2.0, double w = 1.0) : length{l}, width{w} {
20             size++;
21         }
22         Rect(const Rect &obj) : length{obj.length}, width{obj.width} {
23             size++;
24         }
25         ~Rect() {
26             size--;
27         }
28         double len() const {
29             return length;
30         }
31         double wide() const {
32             return width;
33         }
34         double area() const {
35             return length * width;
36         }
37         double circumference() const {
38             return 2 * (length + width);
39         }
40         void resize(double times) {
41             length *= times;
42             width *= times;
43         }
44         void resize(double l_times, double w_times) {
45             length *= l_times;
46             width *= w_times;
47         }
48 };
49 int Rect::size = 0;
50 const string Rect::doc{"a simple Rect class"};
51 
52 // 普通函数:输出矩形信息
53 void output(const Rect &r) {
54     cout << "矩形信息: " << endl;
55     cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出,小数部分保留两位
56     cout << "长: \t" << r.len() << endl;
57     cout << "宽: \t" << r.wide() << endl;
58     cout << "面积: \t" << r.area() << endl;
59     cout << "周长: \t" << r.circumference() << endl;
60 }
61 
62 // 测试代码
63 void test() {
64     cout << "矩形类信息: " << Rect::doc << endl;
65     cout << "当前矩形对象数目: " << Rect::size_info() << endl;
66     Rect r1;
67     output(r1);
68     Rect r2(4, 3);
69     output(r2);
70     Rect r3(r2);
71     r3.resize(2);
72     output(r3);
73     r3.resize(5, 2);
74     output(r3);
75     cout << "当前矩形对象数目: " << Rect::size_info() << endl;
76 }
77 
78 // 主函数
79 int main() {
80     test();
81     cout << "当前矩形对象数目: " << Rect::size_info() << endl;
82 }
View Code

运行测试结果

 

实验任务5

task5.cpp源码

 1 #include <iostream>
 2 #include <cmath>
 3 
 4 // 复数类Complex:定义
 5 class Complex {
 6     private:
 7         double real, imag;
 8     public:
 9         Complex(double r = 0, double i = 0) : real{r}, imag{i} {};
10         Complex(const Complex &c) :real{c.real},imag{c.imag} {};
11         double get_real() const {
12             return real;
13         }
14         double get_imag() const {
15             return imag;
16         }
17         void add(const Complex &c) {
18             real += c.get_real();
19             imag += c.get_imag();
20         }
21         void show() {
22             std::cout << real;
23             if (imag>0) std::cout << " + " << abs(imag) << 'i';
24             if (imag<0) std::cout << " - " << fabs(imag) << 'i';
25         }
26         void show() const {
27             std::cout << real;
28             if (imag>0) std::cout << " + " << abs(imag) << 'i';
29             if (imag<0) std::cout << " - " << fabs(imag) << 'i';
30         }
31     public:
32         friend add(), is_equal(), abs();
33 };
34 Complex add(const Complex &c1, const Complex &c2) {
35     double r, i;
36     r = c1.get_real() + c2.get_real();
37     i = c1.get_imag() + c2.get_imag();
38     Complex c(r, i);
39     return c;
40 }
41 bool is_equal(const Complex &c1, const Complex &c2) {
42     bool r, i;
43     r = c1.get_real() == c2.get_real();
44     i = c1.get_imag() == c2.get_imag();
45     return r && i;
46 }
47 double abs(const Complex &c) {
48     return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
49 }
50 
51 // 复数类Complex: 测试
52 void test() {
53     using namespace std;
54     Complex c1(3, -4);
55     const Complex c2(4.5);
56     Complex c3(c1);
57     cout << "c1 = ";
58     c1.show();
59     cout << endl;
60     cout << "c2 = ";
61     c2.show();
62     cout << endl;
63     cout << "c2.imag = " << c2.get_imag() << endl;
64     cout << "c3 = ";
65     c3.show();
66     cout << endl;
67     cout << "abs(c1) = ";
68     cout << abs(c1) << endl;
69     cout << boolalpha;
70     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
71     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
72     Complex c4;
73     c4 = add(c1, c2);
74     cout << "c4 = c1 + c2 = ";
75     c4.show();
76     cout << endl;
77     c1.add(c2);
78     cout << "c1 += c2, " << "c1 = ";
79     c1.show();
80     cout << endl;
81 }
82 
83 int main() {
84     test();
85 }
View Code

 运行测试结果

标签:const,cout,对象,double,void,int,实验,include
From: https://www.cnblogs.com/ffhfAdjFH7Vr/p/17761465.html

相关文章

  • 实验1 类和对象
    //标准库string,vector,array基础用法#include<iostream>#include<string>#include<vector>#include<array>//函数模板//对满足特定条件的序列类型T对象,使用范围for输出template<typenameT>voidoutput1(constT&obj){for(autoi:obj)......
  • vue el-select/el-cascader获取选中的对象label值
    1.el-select获取选中对象label值<el-form-itemlabel="车辆配置"prop="sales_name"><el-selectv-if="!showSaleNameInput"v-model="form.sales_name"clearableref="itemSelect"......
  • 5、SpringMVC之域对象共享数据
    5.1、域对象简介请求域(request):一次请求范围内有效会话域(session):一次会话范围内有效应用域(application):整个应用范围内有效5.2、环境搭建5.2.1、右击project创建新module5.2.2、选择maven5.2.3、设置module名称和路径5.2.4、module初始状态5.2.5、配置打包方......
  • 在java中将InputStream对象转换为File对象(不生成本地文件)
    importorg.apache.commons.io.IOUtils;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;publicclassStreamUtil{staticfinalStringPREFIX="stream2file";//前缀字符串定义文件名;必须至少三个字符......
  • 实验楼操作系统学习记录
    链接哈尔滨工业大学李治军老师操作系统课程实验楼配套实验课实验1熟悉实验环境课程说明主要平台和工具介绍实验环境的工作模式使用方法这一章节主要是一些对于课程以及实验楼环境的说明,没什么可说的实验2操作系统的引导评分标准实验提示实验2的第一个任务是完......
  • 实验报告
    一、总体思路利用Java,继承JFrame类实现图形用户页面框架及UI设计布局,继承了ActionListener接口实现按钮事件监听用户按钮输入结束后获取等号前的字符串,采用双栈法将中缀表达式转化为后缀表达式后计算后缀表达式(单独写出牛顿迭代法计算开方运算)结果并输出在文本框中二、UI设计......
  • C#学习笔记--面向对象三大特征
    C#核心面向对象--封装用程序来抽象现实世界,(万物皆对象)来编程实现功能。三大特性:封装、继承、多态。类与对象声明位置:namespace中样式:class类名{}命名:帕斯卡命名法(首字母大写)实例化对象:根据类来新建一个对象。Personp=newPerson();成员变量声明在类语句块中用来描述......
  • 面向对象测试
    1.下列说法正确的是(B)A、在类方法中可用this来调用本类的类方法B、在类方法中调用本类的类方法时可直接调用C、在类方法中只能调用本类中的类方法D、在类方法中绝对不能调用实例方法A,this是个局部变量,静态类方法中不存在,也没办法调用答案是:A、在类方法中可用this来调用本......
  • 【算法】游戏中的学习,使用c#面向对象特性控制游戏角色移动
    最近,小悦的生活像是一首繁忙的交响曲,每天忙得团团转,虽然她的日程安排得满满当当,但她并未感到充实。相反,她很少有时间陪伴家人,这让她感到有些遗憾。在周五的午后,小悦的哥哥突然打来电话,他的声音里充满了焦虑。“小悦,我有个事情想拜托你。”哥哥的声音传来。小悦不禁有些疑惑,哥哥......
  • 在JavaScript中,最高效的方法来深度克隆一个对象是什么?
    内容来自DOChttps://q.houxu6.top/?s=在JavaScript中,最高效的方法来深度克隆一个对象是什么?将JavaScript对象进行深度克隆的最有效方法是什么?我见过使用obj=eval(uneval(o));,但这是非标准的做法,仅被Firefox支持。我曾尝试过obj=JSON.parse(JSON.stringify(o));,但对效率......