实验任务3

Complex.hpp源代码:

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

task3.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     Complex c1(6, -8);
 9     const Complex c2(2.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 }

运行结果:

 

 

 实验任务4

User.hpp

 1 #ifndef USER_HPP
 2 #define USER_HPP
 3 
 4 #include<iostream>
 5 #include<string>
 6 #include<iomanip>
 7 
 8 using namespace std;
 9 
10 class User {
11 
12 private:
13     string name;
14     string password;
15     string email;
16     static int n;
17 
18 public:
19     User(string name0);
20     User(string name0, string password0, string email0) :name{ name0 }, password{ password0 }, email{ email0 }{n++; }
21     ~User() { n--; }
22 
23     void set_email();
24     void change_passwd();
25     void print_info()const;
26     static void print_n();
27 
28 };
29 int User::n = 0;
30 
31 User::User(string name0) :name{ name0 }, password{ "111111" }, email{ "" }{n++; }
32 
33 void User::set_email() {
34     cout << "Enter email address:";
35     cin >> email;
36 }
37 void User::change_passwd() {
38     string password0;
39     cout << "Enter old password:";
40     cin >> password0;
41     if (password == password0) {
42         cout << "Enter new password:";
43         cin >> password0;
44         password = password0;
45         cout << endl;
46         cout << "new password is set successfully." << endl;
47     }
48     else {
49         int i = 0;
50         while (i < 2) {
51             cout << "password input error.please re-enter again:";
52             cin >> password0;
53             if (password0 != password) i++;
54             else break;
55         }
56         if (i == 2) cout << "password input error.please try after a while." << endl;
57         else {
58             password = password0;
59             cout << "new password is set successfully." << endl;
60         }
61     }
62 }
63 void User::print_info()const {
64     cout << "name:\t" << name << endl;
65     cout << "password:******" << endl;
66     cout << "email:\t" << email << endl;
67 }
68 void User::print_n() {
69     if (n > 1)
70         cout << "there are " << n << " users." << endl;
71     else
72         cout << "there is " << n << " user." << endl;
73 }
74 #endif

task4.cpp

 1 #include "User.hpp"
 2 #include <iostream>
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     cout << "testing 1......" << endl;
 9     User user1("Jonny", "92197", "xyz@hotmail.com");
10     user1.print_info();
11 
12     cout << endl
13         << "testing 2......" << endl
14         << endl;
15     User user2("Leonard");
16     user2.change_passwd();
17     user2.set_email();
18     user2.print_info();
19 
20     User::print_n();
21 }

运行结果:

 

 

 

 实验总结:

1.本次实验让我学会了自己构造复数类以及更加了解到const的用法;

2.本次实验中我也遇到了很多的问题并且对于'\t'用法有了更加深刻的印象;

3.同时,我对于构造函数的理解也更加深刻了。