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

实验二 类与对象(2)

时间:2022-10-19 09:44:17浏览次数:41  
标签:std const cout 对象 Complex 实验 User using

 

Task4:

Complex.hpp

 

 1 #pragma once
 2 
 3 //Complex类的定义
 4 #include<iostream>
 5 #include<string>
 6 #include<math.h>
 7 
 8 using std::cout;
 9 using std::endl;
10 
11 //Complex类的声明
12 class Complex {
13 public:
14     Complex(double r = 0, double i = 0);
15     Complex(const Complex& c);
16     ~Complex();
17     double get_real() const { return real; }
18     double get_imag() const { return imag; }
19     void show() const;
20     void add(const Complex& c);
21     friend Complex add(const Complex& c1, const Complex& c2);
22     friend bool is_equal(const Complex& c1, const Complex& c2);
23     friend double abs(const Complex& c);
24 
25 private:
26     double real, imag;
27 };
28 
29 
30 //构造函数
31 Complex::Complex(double r, double i) :real{ r }, imag{ i } {}
32 
33 //复制构造函数
34 Complex::Complex(const Complex& c) :real{ c.real }, imag{ c.imag } {}
35 
36 //析构函数
37 Complex::~Complex() {}
38 
39 
40 //非成员函数
41 void Complex::show()const {
42     if (imag > 0)
43         cout << real << " + " << imag << "i";
44     else if (imag == 0)
45         cout << real;
46     else
47         cout << real << " - " << -imag << "i";
48 }
49 
50 void Complex::add(const Complex& c)
51 {
52     real += c.real;
53     imag += c.imag;
54 }
55 
56 //友元函数
57 Complex add(const Complex& c1, const Complex& c2) {
58     Complex c3;
59     c3.add(c1);
60     c3.add(c2);
61     return c3;
62 }
63 
64 bool is_equal(const Complex& c1, const Complex& c2) {
65     if (c1.real == c2.real && c1.imag == c2.imag)
66         return true;
67     else
68         return false;
69 }
70 
71 double abs(const Complex& c) {
72     return sqrt(c.real * c.real + c.imag * c.imag);
73 }

 

task4.cpp

 

 1 #include"Complex.hpp"
 2 #include<iostream>
 3 
 4 //类测试
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(3, -4);
 9     const Complex c2(4.5);
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 }

 

结果截图:

(1)原测试结果

 

 (2)更换数据:

 

 

 

Task5

User.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<iomanip>
 5 
 6 using std::cout;
 7 using std::cin;
 8 using std::endl;
 9 using std::string;
10 using std::left;
11 using std::setw;
12 
13 class User {
14 public:
15     User(string na, string pa = "111111", string em = "");
16     ~User();
17     void set_email();
18     void change_passwd();
19     void print_info();
20     static void print_n();
21 
22 private:
23     string name, passwd, email;
24     static int n;
25 };
26 
27 User::User(string na, string pa, string em) {
28     name = na;
29     passwd = pa;
30     email = em;
31     n++; }
32 User::~User() {
33     n--;
34 }
35 void User::set_email() {
36     cout << "Enter email address: ";
37     cin >> email;
38     cout << "email is set successfully..." << endl;
39 }
40 void User::change_passwd() {
41     int num = 0;
42     cout << "Enter old password: ";
43     string input_passwd;
44     while (1) {
45         cin >> input_passwd;
46         if(input_passwd == passwd)
47         {
48             cout << "Enter new password: ";
49             cin >> passwd;
50             cout << "new password is set successfully..." << endl;
51             break;
52         }
53         else {
54             num++;
55             if (num < 3) {
56                 cout << "password input error. Please re-enter again: ";
57                 continue;
58             }
59             else {
60                 cout << "password input error. Please try after a while. " << endl;
61                 break;
62             }
63         }
64     }
65 }
66 void User::print_info() {
67     cout << left
68         << setw(8) << "name: " << name << endl
69         << setw(8) << "passwd: ";
70     for (int i = 0; i < passwd.size(); i++) {
71         cout << "*";
72     }
73     cout << endl
74         << setw(8) << "email: " << email << endl;
75 }
76 int User::n = 0;
77 void User::print_n() {
78     if (n == 1)
79         cout << "there is 1 user." << endl;
80     else {
81         cout << "there are " << n << " users. " << endl;
82     }
83 }

 

task5.cpp

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

 

结果截图:

测试一:

 

 测试二:

 

 测试三:

 

标签:std,const,cout,对象,Complex,实验,User,using
From: https://www.cnblogs.com/lwhhhh/p/16805103.html

相关文章

  • 实验2 类和对象(2)
    4.实验任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal,imag;public:Complex();~Com......
  • 实验6:开源控制器实践——RYU
    (一)基本要求搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。阅读Ryu文档的TheFirstApplication一节,运行当中的L2Switch,h1......
  • 实验2 类与对象(2)
    2022.10.12OOP实验课实验2类与对象(2)任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal;double......
  • 实验二
    Task4hpp文件如下#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0,doubleb=0):re......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基本......
  • 实验二 类和对象
    实验任务4task4.cpp#include"Complex.hpp"#include<iostream>//类测试voidtest(){usingnamespacestd;Complexc1(1,-8);constComplexc2(6......
  • 实验6:开源控制器实践——RYU
    一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、实验环境(一)基本要求1.搭建......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、......
  • 实验6:开源控制器实践——RYU
    1.建立拓扑并连接RYU控制器2.通过Ryu的图形界面查看网络拓扑3.(1)创建L2Switch.py文件点击查看代码fromryu.baseimportapp_managerfromryu.controllerimporto......
  • 实验6:开源控制器实践——RYU
    实验要求(一)基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器。建立拓扑并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑2.阅读Ryu文档的TheFirst......