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

实验一 类与对象_基础编程1

时间:2023-10-17 16:12:31浏览次数:36  
标签:const cout 对象 double void 编程 int Complex 实验

task1.cpp

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<array>
 5 
 6 template<typename T>
 7 void output1(const T &obj){
 8     for(auto i:obj)
 9     std::cout<<i<<", ";
10     std::cout<<"\b\b \n";
11 }
12 
13 template<typename T>
14 void output2(const T &obj){
15     for(auto p=obj.begin();p!=obj.end(); ++p)
16     std::cout<<*p<<", ";
17     std::cout<<"\b\b \n";
18 }
19 
20 void test_array(){
21     using namespace std;
22     
23     array<int,5> x1;
24     cout<<"x1.size()="<<x1.size()<< endl;
25     x1.fill(42);
26     x1.at(0)=999;
27     x1[4]=-999;
28     cout<<"x1:";
29     output1(x1);
30     cout<<"x1:";
31     output2(x1);
32     
33     array<int,5> x2(x1);
34     cout<<boolalpha<<(x1==x2)<<endl;
35     x2.fill(22);
36     cout<<"x2:";
37     output1(x2);
38     swap(x1,x2);
39     cout<<"x1: ";
40     output1(x1);
41     cout<<"x2: ";
42     output1(x2);
43         
44 }
45 
46 void test_vector(){
47     using namespace std;
48     
49     vector<int> v1;
50     cout<<v1.size()<< endl;
51     cout<<v1.max_size()<<endl;
52     v1.push_back(55);
53     cout<<"v1: ";
54     output1(v1);
55     
56     vector<int> v2{1,0,5,2};
57     v2.pop_back();
58     v2.erase(v2.begin());
59     v2.insert(v2.begin(), 999);
60     v2.insert(v2.end(), -999);
61     cout << v2.size() << endl;
62     cout << "v2: ";
63     output2(v2);
64     
65     vector<int>v3(5,42);
66     cout<<"v3:";
67     output1(v3);
68     
69     vector<int> v4(v3.begin(),v3.end()-2);
70     cout<<"v4:";
71     output1(v4);
72 }
73 
74 void test_string(){
75     using namespace std;
76     
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

 

运行结果截图

 

task2.cpp

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

 

运行结果截图

 

task3.cpp

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

 

运行结果截图

 

task4.cpp

 1 #include <iostream>
 2 #include <string>
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 class Rect {
 7 private:
 8 double length;
 9 double width;
10 static int count; 
11 
12 public:
13 static string doc; 
14 Rect() : length(0), width(0) { 
15      ++count;
16  }
17 Rect(double l, double w) : length(l), width(w) { count++; }
18 Rect(const Rect& r) : length(r.length), width(r.width) { count++; }
19 ~Rect() { 
20     --count; 
21 }
22 double area() const { return length * width; }
23 double perimeter() const { return 2 * (length + width); }
24 void resize(double l, double w) { 
25    length = l; 
26    width = w; 
27 }
28 void resize(double s) { 
29 length = s;
30  width = s; 
31  }
32 friend void output(const Rect& r);
33 static int size_info() {
34  return count; 
35  }
36 };
37 
38 int Rect::count = 0;
39  string Rect::doc { "a simple rect class"};
40 
41 
42 void output(const Rect &r) {
43     cout << "矩形信息: " << endl;
44 cout << fixed << setprecision(2); 
45 cout << "长: " << r.length << endl;
46 cout << "宽: " << r.width << endl;
47 cout << "面积: " << r.area() << endl;
48 cout << "周长: " << r.perimeter() << endl;
49 
50 }
51 
52 void test() {
53 cout << "矩形类信息: " << Rect::doc << endl;
54 cout << "当前矩形对象数目: " << Rect::size_info() << endl;
55 Rect r1;
56 output(r1);
57 Rect r2(4, 3);
58 output(r2);
59 Rect r3(r2);
60 r3.resize(2);
61 output(r3);
62 r3.resize(5, 2);
63 output(r3);
64 cout << "当前矩形对象数目: " << Rect::size_info() << endl;
65 }
66 
67 int main() {
68 test();
69 cout << "当前矩形对象数目: " << Rect::size_info() << endl;
70 }
View Code

 

运行结果截图

 

task5.cpp

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

 

运行结果截图

 

标签:const,cout,对象,double,void,编程,int,Complex,实验
From: https://www.cnblogs.com/chenxiaolong202083290491/p/17769946.html

相关文章

  • 实验2
    任务一源代码 任务一运行结果 任务一问题回答 任务二源代码  任务二运行结果 任务三源代码  任务三运行结果 任务四源代码  任务四运行结果  任务五源代码  任务五运行结果  任务六源代码  任务六运行结果 ......
  • 极限编程
    极限编程(ExtremeProgramming,简称XP)是一种敏捷软件开发方法,旨在改善软件开发项目的质量和效率。XP强调迭代开发、持续反馈和高度协作,以便快速适应需求的变化。以下是XP的一些关键特点:用户故事(UserStories):XP使用用户故事来描述应用程序的功能,这有助于开发团队更好地理解客户需......
  • Java编程之道:巧妙解决Excel公式迭代计算难题
    本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。什么是迭代计算迭代计算其实是在Excel中,一种公式的循环引用,对于了解编程概念的同学,很容易会想到另一个词“递归”。简单的说,就是一段程序调用自己,......
  • 实验一
    #include<iostream>#include<string>#include<vector>#include<array>//函数模板//对满足特定条件的序列类型T对象,使用范围for输出template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";......
  • 实验2
    #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("2......
  • 金蝶云星空业务对象自定义按钮进行权限控制
     金蝶云星空业务对象自定义按钮进行权限控制一、背景说明金蝶标准:权限绑定操作,操作绑定按钮。实际需求:按钮不绑定操作,直接验权二、创建按钮2.1菜单集合  2.2列表菜单 三、创建权限项  四、添加权限项到权限对象 五创建公共权限服务usingKingdee.B......
  • 实验二
    task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13746#defineN24657intmain()8{9intnumber;10inti;11srand(time(0));//以当前系统时间作为随机种子12for(i=0;i<N;++i......
  • Java拾贝第四天——String和匿名对象
    Java拾贝不建议作为0基础学习,都是本人想到什么写什么复习突然发现String没写匿名对象只在堆内存中开辟空间,栈内存中没有对其进行引用的一种对象。(会等待被GC清除)publicclassTest4{publicstaticvoidmain(String[]args){newNoname("匿名对象");}}......
  • 实验2
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13746#defineN24657intmain()8{9intnumber;10inti;11srand(time(0));//以当前系统时间作为随机种子12for(i=0;i<N;++i){......
  • 【Java 并发编程】synchronized
    synchronized关键字synchronized是Java中的一个关键字,翻译成中文是同步的意思,主要解决的是多个线程之间访问资源的同步性,可以保证被它修饰的方法或者代码块在任意时刻只能有一个线程执行。使用方法修饰实例方法给当前对象实例加锁,进入同步代码前要获得当前对象实例的锁......