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

实验2 类和对象(2)

时间:2022-10-12 21:02:50浏览次数:27  
标签:real string 对象 double imag Complex 实验 cout

1.实验四   Complex类的实现

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4   class Complex
 5   {
 6        private:
 7            double real;
 8            double imag;
 9       public:
10           Complex(double r=0.0,double i=0.0)
11           {
12               real=r;
13               imag=i;
14           }
15         Complex(Complex &c)
16         {
17             real=c.real;
18             imag=c.imag;
19         }
20         double     get_real() const
21         {
22             return real; 
23         }
24         double get_imag() const
25         {
26             return imag;
27         }
28         void show() const
29         {
30             if(imag>0)
31              cout<<real<<"+"<<imag<<"i"<<endl;
32              else if(imag==0)
33                  cout<<real<<endl;
34              else
35                cout<<real<<imag<<"i"<<endl;
36         }
37         void add(const Complex &c1)
38         {
39             real=real+c1.real;
40             imag=imag+c1.imag;
41         }
42         friend Complex add(const Complex &c2,const Complex &c3)
43         {
44             Complex c4;
45             c4.real=c2.real+c3.real;
46             c4.imag=c2.imag+c3.imag;
47             return c4;
48         }
49         friend bool is_equal(const Complex &c5,const Complex &c6)
50         {
51              if(c5.real==c6.real&&c5.imag==c6.imag)
52              return true;
53              else
54              return false;
55         }
56         friend double abs(Complex c7)
57         {
58              double model;
59              model=sqrt(c7.imag*c7.imag+c7.real*c7.real);
60              return model;
61         }
62   };

运行结果:

 

 

 

2. 实验五  User类实现

 1 #include<iostream>
 2 #include<cmath>
 3 #include<string>
 4 using namespace std;
 5   class User
 6   {
 7          private:
 8                string name;
 9                string password;
10                string email;
11                static int count;
12                
13       public:
14           
15           User(string n,string p={"111111"},string e={"    "}):name{n},password{p},email{e}
16           {
17              count++;
18           }
19           ~User()
20           {
21               count--;
22           }
23          void set_email()
24         {
25             cout<<"Enter email address: ";
26              cin>>email;
27              cout<<"email is set successfully"<<endl;
28         }
29         void change_password()
30         {
31               string oldpassword;
32               int i=1;
33              cout<<"Enter old password: ";
34              cin>>oldpassword;
35              while(i)
36              {
37              if(oldpassword.compare(password)!=0)
38                 {
39                     i++;
40                     if(i>3)
41                     {
42                         cout<<"please try after a while"<<endl;
43                         break;
44                     }
45                     cout<<"password input error. Please re-enter again: ";
46                     cin>>oldpassword;
47                     continue;
48                     
49                 }
50                 else 
51                   {
52                         string newpassword;
53                         cout<<"Enter new password: ";
54                         cin>>newpassword;
55                         cout<<"password is set successfully"<<endl;
56                         password=newpassword;
57                         break;
58                   }
59              }
60         }
61         void print_info()
62         {
63              cout<<"name: "<<name<<endl;
64              string secret=password;
65              int i;
66              cout<<"password: ";
67              for(i=1;i<=secret.length();i++)
68              cout<<"*";
69              cout<<endl;
70              cout<<"email: "<<email<<endl;
71         }
72         static int  get_count()
73         {
74             cout<<count<<endl;
75              return count;
76         }
77   };
78   int User::count=0;

运行结果:

 

 

logo  

标签:real,string,对象,double,imag,Complex,实验,cout
From: https://www.cnblogs.com/lc114514/p/16785151.html

相关文章

  • 实验4:开源控制器实践——OpenDaylight
    一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。(二)进......
  • 实验4:开源控制器实践——OpenDaylight
    (一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;Mininet拓扑生成并连接控制器的结果Mininet中ping测试截图通过Postman工具调用OpenDaylight......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......
  • 改进ls实验
    改进要求排序:文件名读入数组,利用qsort排序分栏:文件名读入数组,计算列宽和行高.和..加入-a选项,没有-a,不显示隐藏文件-l:功能不同,单独实现首先我们来看一下ls-l的具体......
  • 实验1c语言开发环境使用和数据类型,运算符和表达式
    1.试验任务1(1)在垂直方向上打印两个字符小人的源代码,以及运行结果截图 \\在垂直方向上打印两个字符小人#include<stdio.h>intmain(){printf("o\n");pr......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ub......
  • 使用xstream 解析xml 到对象
    1、依赖包<dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.10</ver......
  • 实验2 类和对象(2)
    实验目的能够使用C++语法规则正确定义、实现、测试类。能够使用C++语法规则正确创建对象,并基于对象编程。知道类的static成员用于解决什么问题场景,会正确使用c++语法规......
  • json操作 - 对象和json字符串 互转 JavaScript
    json操作-对象和json字符串互转JavaScript?12345678910111213141516171819<script>    //json数据转换    vardata={        name:'小明',......