实验一 类与对象

实验一 类与对象

Complex.hpp:

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

Complex.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 int main()
 5 {
 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 }

实验一 类与对象

 实验一 类与对象

User.hpp:

 1 #ifndef USER_HPP
 2 #define USER_HPP
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 class User
 7 {
 8     static int number;
 9 public:
10     User(string name, string passwd = "111111", string email = "") :name(name), password(passwd), email(email) 
11     { number++; }
12     ~User() = default;
13     void set_email()
14     {
15         cout << "Enter email address: ";
16         cin >> email;
17         cout << "email is set successfully..." << endl;
18     }
19     void change_passwd()
20     {
21         cout << "Enter old password: ";
22         string old;
23         int ret = 3;
24         while (ret--)
25         {
26             cin >> old;
27             if (old == password)
28             {
29                 cout << "new passwd is set successfully..." << endl;
30                 break;
31             }
32             else if (!ret)
33             {
34                 cout << "password input error. Please try after a while." << endl;
35             }
36             else
37             cout << "password input error. Please re-enter again: ";
38         }
39     }
40     void print_info()
41     {
42         cout << "name: " << name << endl;
43         cout << "passwd:  ******" << endl;
44         cout << "email: " << email << endl;
45     }
46      static void print_n()
47     {
48         cout << "there are " << number << " users." << endl;
49     }
50 private:
51     string name;
52     string password;
53     string email;
54 };
55 int User::number = 0;
56 #endif

task4.cpp:

#include "User.hpp"
#include <iostream>

int main()
{
using namespace std;

cout << "testing 1......" << endl;
User user1("Nigel", "88488848", "wkc@123.com");
user1.print_info();

cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Nicholas");
user2.change_passwd();
user2.set_email();
user2.print_info();

User::print_n();
}

运行结果截图:

实验一 类与对象

 实验总结:

按照指定函数编写类需要留意的地方有很多,成员函数和友元函数的定义要准确,每个变量的作用域和存放地点要弄清楚。

 

上一篇:使用树莓派GPIO控制继电器


下一篇:字符串可以这样加索引,你知吗?《死磕MySQL系列 七》