实验一 类与对象

Complexs.hpp代码:

#include<iostream>
#include<cmath> 
using namespace std;
    class Complex
    {
    public:
        Complex();
        Complex(double r = 0, double m = 0) : real(r), imag(m) {};
        Complex(Complex& c) : real(c.real), imag(c.imag) {};
        double get_real() const
        {
            return real;
        }
        double get_imag() const
        {
            return imag;
        }
        void show() const
        {
            cout << real << " + " << imag << "i" << endl;
        }
        void add(Complex const& c)
        {
            real += c.real;
            imag += c.imag;
        }
        friend Complex add(Complex const& c1, Complex const& c2);
        friend bool is_equal(Complex const& c1, Complex const& c2);
        friend double abs(Complex const& c);
    private:
        double real;
        double imag;
    };

    Complex add(Complex const& c1, Complex const& c2)
    {
        Complex c(0);
        c.real = c1.real + c2.real;
        c.imag = c1.imag + c2.imag;
        return c;
    }

    bool is_equal(Complex const& c1, Complex const& c2)
    {
        if (c1.real == c2.real && c1.imag == c2.imag)
            return true;
        return false;
    }

    double abs(Complex const& c)
    {
        return sqrt(c.real * c.real + c.imag * c.imag);
    }
#include "Complex.hpp"
#include <iostream>
int main()
{

        using namespace std;
    Complex c1(3, -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;
}

 -------------------------------------------------------------------------------------------------------------------------------------------------

User.hpp代码:

#include<iostream>
using namespace std;

class User
{
public:
    User(string a, string p = "111111", string e = "") : name(a), password(p), email(e) { n++; }
    User(User& u) : name(u.name), password(u.password), email(u.email) { n++; }
    void set_email()
    {
        cout << "Enter email address:";
        cin >> email;
        cout << "email is set sucessfully..." << endl;
    }
    void change_passwd();
    void print_info()
    {
        cout << "name: " << name << endl;
        cout << "passwd:******" << endl;
        cout << "email :" << email << endl;
    }
    static void print_n()
    {
        cout << "there are " << n << " users." << endl;
    }
    ~User() { n--; }
private:
    string name;
    string password;
    string email;
    static int n;
};

int User::n = 0;

void User::change_passwd()
{
    int i = 0;
    string temp;
    while (i != 3)
    {
        if (i == 0)
            cout << "Enter old password:";
        else
            cout << "Please re-enter again:";
        cin >> temp;
        if (temp == password)
        {
            cout << "Enter new password:";
            cin >> password;
            while (password.length() != 6 || password.find('@') == -1)
            {
                cout << "The new password is wrong.";
                cout << "Please input again:";
                cin >> password;
            }
            cout << "new password is set sucessfully..." << endl;
            return;
        }
        else
        {
            cout << "password input error.";
            i++;
        }
    }
    if (i == 3)
        cout << "Please try after a while." << endl;
}

 

 task4:

#include "User.hpp"
#include <iostream>
int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@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();
}

 

实验总结:

编辑时错误的将头文件放到了源文件里以至于一直运行错误

发现通过实验三通过对complex赋一个初值可以避免重名错误

 

posted @ 2021-10-20 17:00  吔瓜群众  阅读(33)  评论(3编辑  收藏  举报