1. 程式人生 > 其它 >C++語言程式設計實驗一 類與物件

C++語言程式設計實驗一 類與物件

四、實驗結論

1. 實驗任務1-2

  驗證性實驗,自行練習。

2. 實驗任務3

  Complex.hpp檔案原始碼:

#include <iostream>

using namespace std;

class Complex {
public:
    Complex(float r = 0, float i = 0) : real{ r }, imag{ i }{ }
    Complex(const Complex& obj) : real{ obj.real }, imag{ obj.imag }{ }
    float get_real() const
{ return real; } float get_imag() const { return imag; } void show() const { if (imag == 0) { cout << real; } else if(imag < 0){ cout << real << " - " << std::abs(imag) << "i"; } else { cout
<< real << " + " << imag << "i"; } } void add(const Complex c2) { real += c2.real; imag += c2.imag; } friend Complex add(Complex c1, Complex c2) { Complex c3; c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag;
return c3; } friend bool is_equal(Complex c1, Complex c2) { bool equal = false; if (c1.real == c2.real && c1.imag == c2.imag) { equal = true; } return equal; } friend float abs(Complex c1) { float m = 0; m = std::sqrt(c1.real * c1.real + c1.imag * c1.imag); return m; } private: float real; float imag; };

  task3.cpp原始碼:

#include "Complex.hpp"
#include <iostream>
int main()
{
    using namespace std;
    Complex c1(4, -1);
    const Complex c2(-2);
    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;
}

  執行測試結果截圖:

3. 實驗任務4

  myUser.hpp檔案原始碼 :(由於我的電腦內可能存在User.hpp檔案,故使用myUser命名該檔案)

#include <iostream>
#include <iomanip>

using namespace std;

class User {
public:
    User(string namestr, string passwdstr = "111111", string emailstr = " ") : name{ namestr }, passwd{ passwdstr }, email{ emailstr }{ n++; }
    void set_email() {
        string new_email;
        cout << "Enter email address: ";
        cin >> new_email;
        email = new_email;
        cout << "email is set successfully\n";
    }
    void change_passwd() {
        string old_passwd, new_passwd;
        int count = 0;
        cout << "Enter old password: ";
        cin >> old_passwd;
        while (count < 2) {
            if (passwd != old_passwd) {
                cout << "password input error. Please re-enter again: ";
                cin >> old_passwd;
            }
            else {
                cout << "Enter new password: ";
                cin >> new_passwd;
                passwd = new_passwd;
                cout << "new password is set successfully...\n";
                return;
            }
            count++;
        }
        if (passwd != old_passwd) {
            cout << "password input error. Please try after a while.\n";
        }
        else {
            cout << "Enter new password: ";
            cin >> new_passwd;
            passwd = new_passwd;
            cout << "new password is set successfully...\n";
        }
    }
    void print_info() {
        cout << left << setw(8) << "name:" << name << endl;
        cout << setw(8) << "passwd:" << "******" << endl;
        cout << setw(8) << "email:" << email << endl;
    }
    void static print_n() {
        cout << "there are " << n << " users.";
    }

private:
    string name;
    string passwd;
    string email;
    static int n;
};

  task4.cpp原始碼:

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

int User::n = 0;

int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("Luna", "46713", "[email protected]");
    user1.print_info();
    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
}

  執行測試結果截圖: