类和对象(2)

实验4:

Complex.hpp文件源码

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

task.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 }

task4运行截图

 

 

 

task5:

User.hpp文件源码

 1 #pragma once
 2 #include <iostream>
 3 
 4 // User类的定义
 5 
 6 using namespace std;
 7 
 8 // User类的声明
 9 class User {
10 public: 
11     User(string name0, string passwd0 = "111111", string email0 = " ") :name { name0 },
12     passwd{ passwd0 }, email{ email0 } { n++; }
13     ~User() { n--; }
14 
15     void set_email();
16     void change_passwd();
17     void print_info();
18     static void print_n();   // 静态成员函数
19 
20 private:
21     string name, passwd, email;
22     static int n;       // 静态数据成员声明,用于记录用户的个数
23 };
24 
25 int User::n = 0;    // 静态数据成员定义和初始化,使用类名限定
26 
27 // 函数定义
28 void User::set_email() {
29     cout << "Enter email address: ";
30     cin >> email;
31     cout << "email is set successfully...\n";
32 }
33 
34 void User::change_passwd() {
35     int i = 1;
36     cout << "Enter old passwd: ";
37     while (i <= 3) {
38         string s1;
39         cin >> s1;
40         if (s1 == passwd) {
41             cout << "Enter new passwd: ";
42             cin >> s1;
43             passwd = s1;
44             break;
45         }
46         else {
47             cout << "passwd input error. ";
48             if (i == 3)
49                 cout << "Please try after a while." << endl;
50             else
51                 cout << "Please re-enter again: ";
52             i++;
53         }
54     }
55 }
56 
57 void User::print_info() {
58     int len = passwd.size();
59     string s2( len, '*' );
60     // passwd.assign(passwd.size(), '*');     // 重新赋值
61     cout << "name:   " << name << endl;
62     cout << "passwd: " << s2 << endl;
63     cout << "email:  " << email << endl;
64 }
65 
66 void User::print_n() {
67     cout << "there are " << n << " users.";
68     cout << endl;
69 }

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", "xyz@hotmail.com");
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 }

task5运行截图

 

 

 

 

 

实验总结:

1、在Complex.hpp的创建过程中,直观感受到了const关键字对于成员函数的限制性,以及在调用参数时产生的一些报错,对于const的理解与运用更加熟练。

2、在User.hpp的创建过程中,主要是对于在密码加密输出那几行代码琢磨了很久,对于string类的相关函数的使用还是不够熟练。

3、task5的选做部分,emmm,还没有太清楚的思路和想法,后续再看。

posted @ 2022-10-18 22:43  小时不时  阅读(76)  评论(0)    收藏  举报