1. 程式人生 > 其它 >實驗一 類與物件

實驗一 類與物件

實驗結論

實驗任務3

Complex.hpp原始碼

#ifndef Complex_hpp
#define Complex_hpp
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
public:
    Complex(double X = 0.0, double Y = 0.0) : real{X}, imag{Y} {}
    Complex(const Complex &now) : real{now.real}, imag{now.imag} {}
    ~Complex() = default;
    double get_real() const { return real; }
    double get_imag() const { return imag; }
    void show() const
    {
        cout << real;
        if (imag > 0)
            cout << " + ";
        else if (imag < 0)
        {
            cout << " - ";
        }
        else
        {
            return;
        }
        cout << abs(imag) << "i";
    }
    void add(Complex x) { real = real + x.real, imag = imag + x.imag; }
    friend Complex add(Complex a, Complex b);
    friend bool is_equal(Complex a, Complex b);
    friend double abs(Complex a);

private:
    double real, imag;
};
Complex add(Complex a, Complex b)
{
    Complex ans;
    ans.real = a.real + b.real;
    ans.imag = a.imag + b.imag;
    return ans;
}
bool is_equal(Complex a, Complex b)
{
    if (a.real == b.real && a.imag == b.imag)
        return true;
    return false;
}
double abs(Complex a)
{
    return sqrt(a.imag * a.imag + a.real * a.real);
}
#endif

task3.cpp原始碼

#include <iostream>
#include "Complex.hpp"
using namespace std;
int main()
{
    Complex c1(6, -4);
    const Complex c2(4.5);
    Complex c3(c1);
    cout << "c1 = ";
    c1.show();
    cout << endl;
    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;
    cout << "c3 = ";
    c3.show();
    cout << endl;
    cout << "abs(c1) = ";
    cout << abs(c1) << endl;
    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    Complex c4;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;
    c1.add(c2);
    cout << "c1 += c2, "
         << "c1 = ";
    c1.show();
    cout << endl;
}

執行截圖

實驗任務4

User.hpp

#ifndef User_hpp
#define User_hpp
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class User
{
public:
    User(string a,string b="111111",string c=""):name{a},password{b},email{c}{n++;}
    ~User()=default;
    void print_info()const{
        cout<<"name:   "<<name<<"\n";
        cout<<"passwd: "<<"******"<<"\n";
        cout<<"email:  "<<email<<"\n";
        return;
    }
    static void print_n(){
        cout<<"there are "<<n<<" users.\n";
        return;
    }
    void set_email(){
        cout<<"Enter email address: ";
        string now;
        cin>>now;
        email=now;
        cout<<"email is set successfully...\n";
        return;
    }
    void change_passwd(){
        string now;
        int error_times=0;
        bool flag=false;
        cout<<"Enter old password: ";
        while(1){
            cin>>now;
            if(now==password){
                flag=true;
                break;
            }
            else{
                error_times++;
                if(error_times==3) break;
                cout<<"password input error. Please re-enter again: ";
            }
        }
        if(!flag){
            cout<<"password input error. Please try after a while.\n";
            return;
        }
        else{
            cout<<"Enter new passwd: ";
            cin>>now;
            password=now;
            cout<<"new passwd is set successfully..."<<"\n";
            return;
        }
    }


private:
    string name,password,email;
    static int n;
};
int User::n=0;
#endif

task4.cpp

#include "User.hpp"
#include <iostream>
using namespace std;
int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("wzy", "112233", "[email protected]");
    user1.print_info();
    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Wzy");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
    return 0;
}

執行截圖

實驗總結

這次的實驗讓我較為深刻的感受到了類的使用,以前寫程式碼時我更多的選擇用struct儲存需要操作型別並寫普通函式對其進行操作,雖然簡單明確但也有缺陷————某種模板我很難直接去複製,因為可能會出現各種形如:函式名命名衝突,變數命名衝突的問題,使用類並呼叫友元函式雖然程式碼總量上相對更為繁瑣,但確實保證了程式碼的可讀性和更好的拓展性,是很好的寫模板方式。

對於課程的User類的一些完善豐富,我感覺比較瑣碎就在這裡聊聊我感覺可以增加的地方與實現方法。
1.使用者名稱衝突及弱密碼問題,為避免使用者設定弱密碼或多使用者設定相同的使用者名稱,可以選擇對弱密碼,使用者名稱進行雜湊,出現衝突則要求使用者重新設定。
2.郵箱格式線性處理即可,如果需要進行敏感詞遮蔽可以搭建敏感詞庫並以使用者名稱為模式串進行AC自動機操作,之所以不使用kmp是因為kmp複雜度取決於匹配串個數,詞庫較大時複雜度較劣。

以上便是我對本次實驗的一些想法與觀點,如果您認為我所寫哪裡有錯誤或歧義請下方評論或聯絡我,謝謝。