实验一 类与对象

试验任务3

complex.hpp文件源码

 1 #ifndef COMPLEX_HPP
 2 #define COMPLEX_HPP
 3 
 4 // complex类的定义
 5 #include <iostream>
 6 #include <iomanip>
 7 #include <cmath>
 8 
 9 using namespace std;
10 
11 // Complex类的声明
12 class Complex
13 {
14 public:
15     Complex();
16     Complex(double a, double b = 0);
17     Complex(const Complex &c);
18 
19     void show() const;          //用于输出复数
20     double get_imag() const;    //返回复数虚部
21     double get_real() const;    //返回复数实部
22     void add(const Complex &c); //用于把一个复数加到自身
23 
24     friend double abs(const Complex &c);                        //求复数的模
25     friend bool is_equal(const Complex &c1, const Complex &c2); //用于判断两个复数是否相等
26     friend Complex add(const Complex &c1, const Complex &c2);   //用于实现两个复数相加,返回复数
27 
28 private:
29     double real; //实部
30     double imag; //虚部
31 };
32 
33 // 默认构造函数
34 Complex::Complex() {}
35 
36 // 带参数的构造函数
37 Complex::Complex(double a, double b) : real{a}, imag{b} {}
38 
39 // 复制构造函数
40 Complex::Complex(const Complex &c) : real{c.real}, imag{c.imag} {}
41 
42 // 用于输出复数
43 void Complex::show() const
44 {
45     if (imag == 0)
46         cout << real;
47     else if (imag < 0)
48         cout << real << " - " << -imag << "i";
49     else
50         cout << real << " + " << imag << "i";
51 }
52 
53 // 返回复数实部
54 double Complex::get_real() const
55 {
56     return real;
57 }
58 
59 // 返回复数虚部
60 double Complex::get_imag() const
61 {
62     return imag;
63 }
64 
65 //用于把一个复数加到自身
66 void Complex::add(const Complex &c)
67 {
68     real += c.real;
69     imag += c.imag;
70 }
71 
72 //求复数的模
73 double abs(const Complex &c)
74 {
75     return sqrt(c.real * c.real + c.imag * c.imag);
76 }
77 
78 //用于判断两个复数是否相等
79 bool is_equal(const Complex &c1, const Complex &c2)
80 {
81     if (c1.imag == c2.imag && c1.real == c2.real)
82         return true;
83     else
84         return false;
85 }
86 
87 //  用于实现两个复数相加,返回复数
88 Complex add(const Complex &c1, const Complex &c2)
89 {
90     Complex c;
91     c.real = c1.real + c2.real;
92     c.imag = c1.imag + c2.imag;
93     return c;
94 }
95 
96 #endif

task3.cpp源码

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

实验结果截图


 

 实验任务4

User.hpp源码

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

实验结果截图

 

 


 

实验总结

 

posted @ 2021-10-20 21:07  请去看诡秘之主  阅读(70)  评论(3编辑  收藏  举报