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

实验2 类和对象(2)

时间:2022-10-30 02:44:29浏览次数:40  
标签:real const cout get 对象 imag Complex 实验

task1

代码:

 1 #include <iostream>
 2 #include <complex>
 3 int main()
 4 {
 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() << endl;
12 cout << "c3.imag = " << c3.imag() << endl;
13 cout << "c1 + c2 = " << c1 + c2 << endl;
14 cout << "c1 - c2 = " << c1 - c2 << endl;
15 cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库中数学函数,对复数进行取模
16 cout << boolalpha; // 设置bool型值以true/false方式输出
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 }

 

测试结果:

 

 

task2

代码:

 1 ing>
 2 
 3 // 妯℃澘鍑芥暟
 4 // 瀵规弧瓒崇壒瀹氭潯浠剁殑搴忓垪绫诲瀷T锛屼娇鐢ㄨ寖鍥磃or杈撳嚭
 5 template<typename T>
 6 void output1(const T &obj) {
 7     for(auto i: obj)
 8         std::cout << i << ", ";
 9     std::cout << "\b\b \n";
10 }
11 
12 // 妯℃澘鍑芥暟
13 // 瀵规弧瓒崇壒瀹氭潯浠剁殑搴忓垪绫诲瀷T锛屼娇鐢ㄨ凯浠e櫒杈撳嚭
14 template<typename T>
15 void output2(const T &obj) {
16     for(auto p = obj.cbegin(); p != obj.cend(); ++p)
17         std::cout << *p << ", ";
18     std::cout << "\b\b \n";
19 }
20 
21 int main() {
22     using namespace std;
23 
24     array<int, 5> x1; // 鍒涘缓涓€涓猘rray瀵硅薄锛屽寘鍚?涓猧nt绫诲瀷鍏冪礌锛屾湭鍒濆鍖?
25     cout << "x1.size() = " << x1.size() << endl; // 杈撳嚭鍏冪礌涓暟
26     x1.fill(42); // 灏唜1鐨勬墍鏈夊厓绱犲€奸兘璧嬪€间负42
27     x1.at(0) = 999;  // 鎶婁笅鏍囦负0鐨勫厓绱犱慨鏀逛负999
28     x1.at(4) = -999; // 鎶婁笅鏍囦负4鐨勫厓绱犱慨鏀逛负-999
29     x1[1] = 777; // 鎶婁笅鏍囦负1鐨勫厓绱犱慨鏀逛负777
30     cout << "x1: ";
31     output1(x1);
32     cout << "x1: ";
33     output2(x1);
34 
35 
36     array<double, 10> x2{1.5, 2.2}; // 鍒涘缓瑕佺粰array瀵硅薄锛屽寘鍚?0涓猟ouble绫诲瀷鍏冪礌锛屽垵濮嬪寲閮ㄥ垎鍏冪礌
37     cout << "x2.size() = " << x2.size() << endl;
38     cout << "x2: ";
39     output1(x2);
40 
41     array<int, 5> x3{x1};
42     cout << boolalpha << (x1 == x3) << endl;
43     x3.fill(22);
44     cout << "x3: ";
45     output1(x3);
46 
47     swap(x1, x3); // 浜ゆ崲x1鍜寈3鐨勬暟鎹?
48     cout << "x1: ";
49     output1(x1);
50     cout << "x3: ";
51     output1(x3);
52 }

 

测试结果:

 

task3

代码:

 1 #include "Employee.hpp"
 2 #include <iostream>
 3 
 4 void test() {
 5     using std::cout;
 6     using std::endl;
 7 
 8     cout << Employee::doc << endl << endl;
 9 
10     Employee employee1;
11     employee1.set_info("Sam", 30000, 2015, 1, 6);
12     employee1.update_hire_date(2017, 6, 30);
13     employee1.update_salary(35000);
14     employee1.display_info();
15     cout << endl << endl;
16 
17     Employee employee2{"Tony", 20000, 2020, 3, 16};
18     employee2.raise_salary(15); // 鎻愭垚15%
19     employee2.display_info();
20     cout << endl << endl;
21 
22     Employee::display_count();
23 }
24 
25 int main() {
26     test();
27 }

 

测试结果:

 

 

task4

代码:

 1 #include<cmath>
 2 class Complex
 3 {
 4     public:
 5         Complex(){
 6             real=0;
 7             imag=0;
 8         }
 9         Complex(float x){
10             real=x;
11             imag=0;
12         }
13         Complex(float x,float y){
14             real=x;
15             imag=y;
16         }
17         Complex(const Complex &xx){
18             real=xx.get_real();
19             imag=xx.get_imag();
20         }
21         float get_real() const;
22         float get_imag() const;
23         void show() const;
24         friend Complex add (const Complex,const Complex);
25         friend bool is_equal (const Complex,const Complex);
26         friend float abs (const Complex);
27     private:
28         float real,imag;
29  } ;
30 Complex::float get_real() const{
31             return real;
32         }
33 Complex::float get_imag() const{
34             return imag;
35         }
36 Complex::void show() const{
37             printf("%f+%fi\n",real,imag);
38         }
39 Complex::void add(Complex &xx){
40             real+=xx.get_real();
41             imag+=xx.get_imag();
42         }
43 Complex add(Complex a,Complex b)
44 {
45     Complex c(a.get_real()+b.get_real(),a.get_imag()+b.get_imag());
46     return c;
47 }
48 bool is_equal(Complex a,Complex b)
49 {
50     if(a.get_imag()==b.get_imag()&&a.get_real()==b.get_real())
51     return true;
52     else return false;
53 }
54 float abs(Complex a)
55 {
56     return sqrt(a.get_real()*a.get_real()+a.get_imag()*a.get_imag());
57 }

 

测试结果:

 

task5

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 class User
 5 {
 6     public:
 7         User(string a,string b="111111",string c="")
 8         {
 9             name=a;
10             password=b;
11             email=c;
12             n++;
13         }
14         ~User(){
15             n--;
16         }
17         static int n;
18         void set_email()
19         {
20             printf("请输入邮箱:");
21             cin>>email;
22         }
23         void change_passwd(){
24             string f="";
25             int t=0;
26             while(f!=password&&t<3)
27             {
28             printf("输入旧密码:");
29             cin>>f;
30             t++;
31             }
32             if(t==3) printf("稍后再试");
33             else 
34             {
35                 printf("输入新密码:");
36                 cin>>f;
37                 password=f;
38              } 
39         }
40         void print_info(){
41             cout<<"用户名:"<<name<<endl;
42             cout<<"密码:";
43             for(int i=0;i<password.length();i++) cout<<'*';
44             cout<<endl;
45             cout<<"邮箱:"<<email; 
46         }
47         static void print_n(){
48             cout<<"n="<<n;
49         }
50     private:
51         string name,password,email;
52  } ;
53  int User::n=0;

 

测试结果:

 

标签:real,const,cout,get,对象,imag,Complex,实验
From: https://www.cnblogs.com/git-porn-hub/p/16840394.html

相关文章

  • HCIP-进阶实验01-ospf高级组网
    HCIP-进阶实验01-ospf高级组网1实验需求某企业网络环境如图所示、内部网络由多台路由器联合起来,以GateWay作为出口路由器连接到ISP网络各网段IP地址规划如图所示,其中R1......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境Ubuntu20.04Desktopamd64三、......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境下载虚拟机软件OracleVisualBo......
  • 面向对象基础回顾
    基本知识回顾:类与对象:成员变量,方法,构造器(初始化类的对象)内部块,代码块一个JAVA文件可以定义多个类,但是只有一个类用PUBLIC修饰,且该修饰的类名必须为JAVA代码文件名称 ......
  • 实验3:OpenFlow协议分析实践
    一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。二、实验环境Ubuntu20.0......
  • 【WPF】绘制可视化对象(mvvm模式)
    新建wpf解决方案如下:效果:     源代码下载: ......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验2:Open vSwitch虚拟交换机实践
    一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的O......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验......
  • 实验一:决策树算法实验
    实验一:决策树算法实验姓名:许珂学号:201613344【实验目的】理解决策树算法原理,掌握决策树算法框架;理解决策树学习算法的特征选择、树的生成和树的剪枝;能根据不同的数......