【实验1】 类与对象

实验结论:

  • 实验任务3:

Complex.h

 1 #ifndef COMPLEX_HPP
 2 #define COMPLEX_HPP
 3 
 4 // Complex类的定义
 5 #include <iostream>
 6 #include <iomanip>
 7 #include <string>
 8 #include <cmath>
 9 
10 using namespace std;
11 
12 class Complex{
13     public:
14         Complex() = default;
15         Complex(double a): real{a} {}
16         Complex(double a, double b): real{a}, imag{b} {}
17         Complex(const Complex &a): real{a.real}, imag{a.imag} {};
18         double get_real()const {
19             return real;
20         }
21         double get_imag()const {
22             return imag;
23         }
24         void show() const{
25             if(imag < 0)
26                 cout << real << " - " << -imag << "i";
27             else if(imag == 0)
28                 cout << real;
29             else
30                 cout << real << " + " << imag << "i";
31         }
32         void add(const Complex &a) {
33             real += a.real;
34             imag += a.imag;
35         }
36     private:
37         double real=0.0, imag=0.0;
38 };
39 
40 Complex add(Complex a, Complex b) {
41     double a_real = a.get_real();
42     double b_real = b.get_real();
43     double a_imag = a.get_imag();
44     double b_imag = b.get_imag();
45     Complex c(a_real+b_real, a_imag+b_imag);
46     return c;
47 }
48 
49 bool is_equal(Complex a, Complex b) {
50     if(a.get_real() == b.get_real() && a.get_imag() == b.get_imag())
51         return true;
52     else return false;
53 }
54 
55 double abs(Complex a) {
56     double a_real = a.get_real();
57     double a_imag = a.get_imag();
58     return sqrt(a_real*a_real + a_imag*a_imag);
59 }
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(2, -1);
 9     const Complex c2(6.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 // User类的定义
 5 #include <iostream>
 6 #include <iomanip>
 7 #include <string>
 8 #include <cmath>
 9 
10 using namespace std;
11 
12 class User{
13     public:
14         User(string a, string b = "111111", string c = ""): name{a}, passwd{b}, email{c} {count++;}
15         void set_email() {
16             string newone = "";
17             while(newone.find('@') == -1) {
18                 cout << "Enter email address: ";
19                 cin >> newone;
20                 email = newone;
21             }
22         }
23         void change_passwd() {
24             cout << "Enter old password: ";
25             string front;
26             cin >> front;
27             if(front == passwd) {
28                 cout << "Enter new password: ";
29                 string newone;
30                 cin >> newone;
31                 while(newone.length()!=6) {
32                     cout << "Enter new password: ";
33                     cin >> newone;
34                     if(newone.length() == 6) {
35                         passwd = newone;
36                         cout << "new passwd is set successfully..." << endl;
37                     }
38                 }
39             
40             }
41         }
42         void print_info() {
43             cout << "name: " << name << endl;
44             cout << "password: ******" << endl;
45             cout << "email: " << email << endl;
46         }
47         static void print_n() {
48             cout << "there are " << count << " users." << endl;
49         }
50     private:
51         string name, passwd, email;
52         static int count;
53 };
54 
55 int User::count = 0;
56 
57 #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("Katachi", "494350", "katachi@gmail.com");
10     user1.print_info();
11 
12     cout << endl
13          << "testing 2......" << endl
14          << endl;
15     User user2("Richard");
16     user2.change_passwd();
17     user2.set_email();
18     user2.print_info();
19 
20     User::print_n();
21 }

运行结果:

 

 

实验总结:

  本次实验让我更加了解了面向对象程序设计的结构感和创造过程,特别是实验4让我深感体会面向对象程序设计是一个把数据和算法结合在一起的聚合物,我很喜欢做成成果的感觉。

原文地址:https://www.cnblogs.com/yinjx/p/15429849.html