实验1 类与对象

实验任务3

//Complex.hpp

 1 #ifndef COMPLEX_HPP
 2 #define COMPLEX_HPP
 3 
 4 #include <iostream>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 class Complex
 9 {
10 public:
11     Complex() : real{0}, imag{0} {};
12     Complex(double real0) : real{real0} {};
13     Complex(double real0, double imag0) : real{real0}, imag{imag0} {};
14     Complex(const Complex &c) : real{c.real}, imag{c.imag} {};
15     ~Complex() = default;
16 
17     //成员函数
18     double get_real() const;
19     double get_imag() const;
20     void show() const;
21     void add(const Complex &cc);
22 
23     //友元函数
24     friend Complex add(const Complex &cc1, const Complex &cc2);
25     friend bool is_equal(const Complex &cc1, const Complex &cc2);
26     friend double abs(const Complex &cc1);
27 
28 private:
29     double real;
30     double imag;
31 };
32 
33 double Complex::get_real() const
34 {
35     return real;
36 }
37 
38 double Complex::get_imag() const
39 {
40     return imag;
41 }
42 
43 void Complex::show() const
44 {
45     if (imag < 0)
46         cout << real << " - " << 0 - imag << "i";
47     else if (imag == 0)
48         cout << real;
49     else
50         cout << real << " + " << imag << "i";
51 }
52 
53 void Complex::add(const Complex &cc)
54 {
55     real += cc.get_real();
56     imag += cc.get_imag();
57 }
58 
59 Complex add(const Complex &cc1, const Complex &cc2)
60 {
61     Complex c;
62     c.real = cc1.real + cc2.real;
63     c.imag = cc1.imag + cc2.imag;
64     return c;
65 }
66 
67 bool is_equal(const Complex &cc1, const Complex &cc2)
68 {
69     if (cc1.real == cc2.real && cc1.imag == cc2.imag)
70     {
71         return true;
72     }
73     else
74         return false;
75 }
76 
77 double abs(const Complex &cc1)
78 {
79     return sqrt(cc1.real * cc1.real + cc1.imag * cc1.imag);
80 }
81 #endif

//task3.cpp

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

测试结果:

 

实验任务4

//User.hpp

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

//task4.cpp

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

测试结果:

密码输入错误:

密码输入正确:

 

实验总结:

1、对于const型数据声明、定义的形式,使用场景还不太熟悉

posted @ 2021-10-23 17:03  dd摇摆  阅读(19)  评论(1)    收藏  举报