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

实验二 类与对象(2)

时间:2022-10-16 23:24:15浏览次数:61  
标签:const cout 对象 void Complex 实验 imag string

任务4:

  Complex.hpp

 1 #pragma once
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 class Complex{
 5     public:
 6         Complex (double r=0,double i=0):real{r},imag{i}{} ;
 7         Complex (const Complex &p):real{p.real},imag{p.imag}{} ;
 8         double get_real() const{return real;}
 9         double get_imag() const{return imag;}
10         void show() const;
11         void add(const Complex &p) ;
12         friend Complex add(const Complex &p1,const Complex &p2) ;
13         friend bool is_equal(const Complex &p1,const Complex &p2) ;
14         friend double abs(Complex &p) ;
15         private:
16             double real,imag;
17 };
18 void Complex::show()const {
19     if (imag > 0)
20         cout << real << "+" << imag << "i";
21     if (imag < 0)
22         cout << real << imag << "i"  ;
23     if (imag == 0)
24         cout << real ;
25 }
26 void Complex::add(const Complex &p){
27     real=real+p.real;
28     imag=imag+p.imag;
29 }
30 Complex add(const Complex& p1, const  Complex& p2) {
31     Complex p;
32     p.real =p1.real + p2.real;
33     p.imag = p1.imag +p2.imag;
34     return p;
35 }
36 bool is_equal(const Complex &p1,const Complex &p2){
37     if(p1.real==p2.real&&p1.imag==p2.imag){
38         return true;
39     }
40     else return false;
41 }
42 double abs(Complex &p){
43      return sqrt(p.real*p.real+p.imag*p.imag);
44 }

  main.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(4, -5);
 9     const Complex c2(2.7);
10     Complex c3(c1);
11 
12     cout << "c1 = ";
13     c1.show();
14     cout << endl;
15 
16     cout << "c2 = ";
17     c2.show();
18     cout << endl;
19     cout << "c2.imag = " << c2.get_imag() << endl;
20 
21     cout << "c3 = ";
22     c3.show();
23     cout << endl;
24 
25     cout << "abs(c1) = ";
26     cout << abs(c1) << endl;
27 
28     cout << boolalpha;
29     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
30     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
31 
32     Complex c4;
33     c4 = add(c1, c2);
34     cout << "c4 = c1 + c2 = ";
35     c4.show();
36     cout << endl;
37 
38     c1.add(c2);
39     cout << "c1 += c2, " << "c1 = ";
40     c1.show();
41     cout << endl;
42 }
43 
44 int main() {
45     test();
46 }

 

任务5:

  

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 class User{
 6     public:
 7     User(string n0,string p0="111111",string e0 ="");
 8      void set_email();
 9      void change_passwd();
10      void print_info() const;
11      static void print_n();
12      
13      private:
14      string name,passwd,email;
15      static int n;
16 };
17 int User::n = 0;
18 User::User(string n0,string p0,string e0):name{n0},passwd{p0},email{e0}{
19     n++;
20 }
21 
22 void User::set_email(){
23     cout << "Enter emal address: ";
24     cin >> email;
25     cout << "email is set successfully..." << endl;
26 }
27 void User::change_passwd(){
28     string old_password;
29     int count =1;
30     cout << "Enter old password: ";
31     while(count<=3){
32         cin >> old_password;
33         if(old_password==passwd){
34         cout << "Enter new passwd: ";
35         cin >> passwd;
36         cout << "new passwd is set successfully..." << endl;
37         break;
38         }
39         else{
40             if(count!=3){
41             cout << "password input error.Please re-enter again: ";
42         }
43             else{
44             cout << "password input error.Please try after a while" << endl;
45         } 
46             count++;
47         }
48     }
49     
50 }
51 void User::print_info() const{
52     cout << "name: " << name << endl;
53     cout << "passwd: ";
54     for (int i = 0; i < passwd.length(); i++)
55          cout << "*";
56          cout << endl;
57          cout << "eamil: " << email << endl;
58 }
59 void User::print_n(){
60     cout << "there are " << n << " users.";
61 }

  task5.cpp

 1 void test() {
 2     using std::cout;
 3     using std::endl;
 4 
 5     cout << "testing 1......\n";
 6     User user1("Jonny", "92197", "[email protected]");
 7     user1.print_info();
 8 
 9     cout << endl
10          << "testing 2......\n\n";
11          
12     User user2("Leonard");
13     user2.change_passwd();
14     user2.set_email();
15     user2.print_info();
16 
17     cout << endl;
18     User::print_n();
19 }
20 
21 int main() {
22     test();
23 }

 

标签:const,cout,对象,void,Complex,实验,imag,string
From: https://www.cnblogs.com/ljhakuna/p/16797559.html

相关文章

  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 爱斯曼德测评(Aspiring Minds),是目前*的职业能力测评及认证公司,由美国麻省理工学院人工
    爱斯曼德测评https://m.jobui.com/company/12939222/https://amcatcampus.aspiringminds.com/https://www.shl.com/en-in/......
  • 实验四
    1、利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 实验4:开源控制器实践——OpenDaylight
    基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。进......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验一
    任务1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("I......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 内置函数,可迭代对象
    一、重要内置函数1.zip()将对不同列表中对应的元素打包成一个个元组,然后返回由这些元组组成的对象.  用list()转换后打印出结果,可以看到输出结果为一个列表,列表中的......
  • 操作系统实验:同步机制及应用编程实现与比较——银行转账问题
    1.实验内容及要求针对所谓的银行账户转账同步问题,分析、设计和利用C语言编程实现基于Peterson算法的同步解决方案,以及基于Windows(或Linux)操作系统同步机制的相应解......