首页 > 编程语言 >实验1 类和对象_基础编程

实验1 类和对象_基础编程

时间:2023-10-16 22:33:24浏览次数:43  
标签:const 对象 double void 编程 int 实验 include Rect

实验任务1

1.代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<array>
 5 
 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 // 对满足特定条件的序列类型T对象,使用迭代器输出
17 template<typename T>
18 void output2(const T &obj) {
19 for(auto p = obj.begin(); p != obj.end(); ++p)
20 std::cout << *p << ", ";
21 std::cout << "\b\b \n";
22 }
23 // array模板类基础用法
24 void test_array() {
25 using namespace std;
26 
27 array<int,5> x1;
28 cout<<"x1.size()="<< x1.size() << endl; 
29 x1.fill(42);
30 x1.at(0)=999;
31 x1[4]=-999;
32 cout<<"x1: ";
33 output1(x1); 
34 cout << "x1: ";
35 output2(x1); 
36 
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);
43 cout<<"x1: ";
44 output1(x1);
45 cout<<"x2: ";
46 output1(x2);
47 }
48 
49 void test_vector()
50 {
51 using namespace std;
52     
53 vector<int> v1;
54 cout<<v1.size()<<endl;
55 cout<<v1.max_size()<<endl;
56 v1.push_back(55);
57 cout<<"v1: ";
58 output1(v1);
59     
60 vector<int> v2{1,0,5,2};
61 v2.pop_back();
62 v2.erase(v2.begin());
63 v2.insert(v2.begin(),999);
64 v2.insert(v2.end(),-999);
65 cout<<v2.size()<<endl;
66 cout<<"v2: ";
67 output2(v2);
68     
69 vector<int> v3(5, 42);
70 cout << "v3: ";
71 output1(v3);
72     
73 vector<int> v4(v3.begin(), v3.end()-2); 
74 cout << "v4: ";
75 output1(v4);
76 }
77 
78 void test_string() {
79 using namespace std;
80 string s1{"oop"};
81 cout << s1.size() << endl;
82 for(auto &i: s1)
83 i -= 32;
84 s1 += "2023";
85 s1.append(", hello");
86 cout << s1 << endl;
87 }
88 
89 int main() {
90 using namespace std;
91 cout << "===========测试1: array模板类基础用法===========" << endl;
92 test_array();
93 cout << "\n===========测试2: vector模板类基础用法===========" << endl;
94 test_vector();
95 cout << "\n===========测试3: string类基础用法===========" << endl;
96 test_string();
97 }
View Code

2.图片:

 

实验任务2

1.代码:

 1 #include<iostream>
 2 #include<complex>
 3 
 4 void test_std_complex()
 5 {
 6     using namespace std;
 7     complex<double> c1{3,4},c2{4.5};
 8     const complex<double> c3{c2};
 9     
10     cout << "c1 = " << c1 << endl;
11     cout << "c2 = " << c2 << endl;
12     cout << "c3 = " << c3 << endl;
13     cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag()
14 << endl;
15 
16 cout << "c1 + c2 = " << c1 + c2 << endl;
17 cout << "c1 - c2 = " << c1 - c2 << endl;
18 cout << "abs(c1) = " << abs(c1) << endl; 
19 
20 }
21 
22 int main()
23 {
24     test_std_complex();
25 }
View Code

2.图片:

 

实验任务3

 1.代码:

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 class T
  6 {
  7     public:
  8         T(int x=0,int y=0);
  9         T(const T &t);
 10         T(T &&t);
 11         ~T();
 12         
 13         void set_m1(int x); 
 14         int get_m1() const; 
 15         int get_m2() const; 
 16         void display() const; 
 17         friend void func();
 18         
 19     private:
 20         int m1,m2;
 21         
 22     public:
 23         static void display_count();
 24         
 25     public:
 26         static const string doc;
 27         static const int max_count;
 28         
 29     private:
 30         static int count;
 31 };
 32 
 33 const string T::doc{"a simple class"};
 34 const int T::max_count = 99;
 35 int T::count = 0;
 36 
 37 T::T(int x,int y):m1{x},m2{y}
 38 {
 39     ++count;
 40     cout << "constructor called.\n";
 41 }
 42 
 43 T::T(const T &t):m1{t.m1},m2{t.m2}
 44 {
 45     ++count;
 46     cout << "copy constructor called.\n";
 47 }
 48 
 49 T::T(T &&t):m1{t.m1},m2{t.m2}
 50 {
 51     ++count;
 52     cout << "move constructor called.\n";
 53 }
 54 
 55 T::~T() {
 56     --count;
 57     cout << "destructor called.\n";
 58 }
 59 
 60 void T::set_m1(int x)
 61 {
 62     m1=x;
 63 }
 64 int T::get_m1() const {
 65     return m1;
 66 }
 67 int T::get_m2() const {
 68     return m2;
 69 }
 70 void T::display() const {
 71     cout << m1 << ", " << m2 << endl;
 72 }
 73 void T::display_count() {
 74 cout << "T objects: " << count << endl;
 75 }
 76 
 77 
 78 void func()
 79 {
 80     T t1; 
 81     t1.set_m1(55);
 82     t1.m2=77;
 83     t1.display(); 
 84 }
 85 
 86 
 87 void test() {
 88     cout << "T class info: " << T::doc << endl;
 89     cout << "T objects max_count: " << T::max_count << endl;
 90     T::display_count();
 91     T t1;
 92     t1.display();
 93     t1.set_m1(42);
 94     T t2{t1};
 95     t2.display();
 96     T t3{std::move(t1)};
 97     t3.display();
 98     t1.display();
 99     T::display_count();
100 }
101 
102 int main() {
103 cout << "============测试类T============" << endl;
104 test();
105 cout << endl;
106 cout << "============测试友元函数func()============" << endl;
107 func();
108 }
View Code

2.图片:

 

实验任务4

1.代码:

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

2.图片

 

 

实验任务5

1.代码

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

2.图片

 

标签:const,对象,double,void,编程,int,实验,include,Rect
From: https://www.cnblogs.com/yili123/p/17761759.html

相关文章

  • 实验2
    实验2.1实验2.1代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=1;i<N;++i){number=rand()%(N2-N1+1)+......
  • 【linux编程】backtrace获取堆栈信息
      参考资料1. 使用backtrace获取堆栈信息2. Howtoprintastacktracewheneveracertainfunctioniscalled......
  • 如何以编程方式关闭/隐藏Android软键盘?
    内容来自DOChttps://q.houxu6.top/?s=如何以编程方式关闭/隐藏Android软键盘?我在我的布局中有一个EditText和一个Button。在编辑字段中写入内容并点击Button后,我希望在触摸键盘外部时隐藏虚拟键盘。我认为这是一段简单的代码,但是我在哪里可以找到它的示例?为了澄清这个疯狂......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;//计算num的值printf("......
  • 如何将没有复制或移动构造函数的对象放入vector容器
    正文直接说答案,这个问题无法实现。原因是因为std::vector容器的插入一定会调用类对象的构造函数或者移动构造函数。说一下为什么会有这个问题,因为不想用指针,我想直接通过类对象本身的RAII机制来实现的资源的控制,智能指针是一个解决方案,不过智能指针是写起来很繁琐,终究比不上值类......
  • 实验2
    task1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain(){10intnumber,i;1112srand(time(0));1314for(i=0;i<N;++i......
  • 实验二 c语言分支与循环基础应用编程
    实验一源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){ intnumber; inti; srand(time(0)); for(i=0;i<N;i++) { number=rand()%(N2-N1+1)+N1; printf("20238329%04......
  • 03 K8S API资源对象介绍02(Deployment Service DaemonSet StatefulSet)
    一、API资源对象DeploymentDeploymentYANL示例vimnginx-deploy.yamlapiVersion:apps/v1kind:Deploymentmetadata:labels:app:myngname:ng-deployspec:replicas:2##副本数selector:matchLabels:app:myngtemplate:metadata:......
  • 实验二
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+......
  • 实验2
    实验任务1 #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){ system("colorE5");intnumber;inti;srand(time(0));//以当前系统时间作为随机种子for(i=0;i&......