C++语言程序设计实验一 类与对象

 

 

   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;
}

  运行测试结果截图:

 

 

  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", "112@hotmail.com");
    user1.print_info();
    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
}

  运行测试结果截图:

 

 

 

 

 

 

 

posted @ 2021-10-20 22:33  miilue  阅读(92)  评论(5编辑  收藏  举报