实验一 类与对象

实验任务3 Complex类的自我实现

​ 简要说明:不使用C++标准库,自行设计并实现一个复数类Complex,并满足简单的使用需求。

​ 程序包括 Complex.hpp与 task3.cpp 。

​ Complex.hpp:

 1 #ifndef COMPLEX_HPP
 2 #define COMPLEX_HPP
 3 #include<iostream>
 4 #include <math.h>
 5 
 6 
 7 class Complex
 8 
 9 {
10 public:
11     Complex() {};
12 
13     Complex(double a0, double b0 = 0) :a(a0), b(b0) {}
14 
15     Complex(const Complex& p) :a(p.a), b(p.b) {}
16 
17     ~Complex() = default;
18 
19     double get_real() const { return a; }
20     double get_imag() const { return b; }
21 
22     void show() const {
23         std::cout << a;
24         if (b > 0) { std::cout << "+" << b << "i"; }
25         else if(b<0) std::cout << b << "i" << std::endl;
26 
27     }
28 
29     void add(const Complex& p) { a += p.a; b += p.b;  }
30 
31     friend Complex add (const Complex& p1, const Complex& p2);//用于实现两个复数相加,返回复数。
32     friend bool is_equal (const Complex& p1,const  Complex& p2);//用于判断两个复数是否相等,返回true/false。
33     friend double abs  (const Complex& p);//用于对复数进行取模运算。
34 
35 private:
36     double a, b;
37 };
38 
39 Complex add(const Complex& p1, const Complex& p2) {
40 
41     Complex kk;
42     kk.a = p1.a + p2.a;
43     kk.b = p1.b + p2.b;
44 
45     return kk;
46 }
47 
48 bool is_equal(const Complex& p1, const Complex& p2) {
49 
50     if (p1.a == p1.a && p1.b == p2.b) return true; else return false;
51 
52 }
53 
54 double abs(const Complex& p) {
55 
56     double m = p.a, n = p.b;
57 
58     return sqrt(m * m + n * n);
59 
60 }
61 #endif

task3.cpp:

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

更改数据后的测试结果:

实验一 类与对象

 实验任务4 设计并实现一个用户类User。

 程序包括 User.hpp与 task4.cpp 。

User.hpp:

 1 #ifndef USER_HPP
 2 #define USER_HPP
 3 
 4 // Employee类的定义
 5 #include <iostream>
 6 #include <string>
 7 using std::cin;
 8 using std::cout;
 9 using std::endl;
10 using std::string;
11 class User {
12 public:
13     User(string name0, string passwd0 = "111111", string email0 = "") :name(name0), passwd(passwd0), email(email0) { n++; };
14     ~User() {};
15     void set_email();
16     void change_passwd();
17     void print_info();
18     static void  print_n(){ cout << "there are" << n << " users."; }
19 private:
20     string name, passwd, email;
21     static int n;
22 
23 };
24 int User::n = 0;
25 void User::set_email() {
26     cout << "Enter email address: ";
27     cin >> email;
28     size_t position;
29     position = email.find("@");
30     while(position == string::npos){          //检测邮箱地址是否包含@
31         cout << "The email should inculde '@'." << endl;
32         cout << "Please re-enter again: ";
33         cin >> email;
34         position = email.find("@");
35     } 
36     cout << "email is set successfully..." << endl;
37 }
38 void User::change_passwd() {
39     int i = 0;
40     string newpasswd;
41     cout << "Enter old password: ";
42     while (i < 3) {
43         cin >> newpasswd;
44         if (passwd == newpasswd) {
45             cout << "Enter new passwd: ";
46             int a;
47             do {                           //保证密码长度必须>=6
48 
49                 cin >> passwd;
50                 a = passwd.length();
51                 if (a < 6) {
52                     cout << "password lengh must be more than 5 numbers." << endl;
53                     cout << "Please re-enter again: ";
54                 }
55             } while (a < 6);
56             cout << "new passwd is set succeddfully..." << endl;
57             break;
58         }
59         else {
60             i++;
61             if (i < 3)
62                 cout << "password input error. Please re-enter again: ";
63             else if (i == 3)cout << "password input error. Please try after a while." << endl;
64 
65         }
66 
67     }
68 }
69 void User::print_info() {
70     cout << "name:  " << name << endl;
71     cout << "passwd: ******" << endl;
72     cout << "email:  " << email << endl;
73 }
74 #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", "123456", "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 }

实验一 类与对象

 

实验一 类与对象

 

实验总结:

1.hpp和cpp文件要保存在相同的路径,才能编译成功。

2.class的定义时结尾处要有结束符“;”。

3.对于减少using namespace std影响的几个办法:

    a.直接在函数名前加上std::

    b.

    直接把using的作用域限制到一个函数中,例如:

    void temp()
   {
      using namespace std;
       cout<<"kk"<<endl;
    }

     c.在开头写出using std::+需要用的函数名;

4.string类中find函数返回的值npos因为是无符号整型,所以相对方便的办法是直接和npos作比较。

 

 

上一篇:实验1 类与对象


下一篇:实验一 类与对象