其他分享
首页 > 其他分享> > 实验一 类与对象

实验一 类与对象

作者:互联网

目录

实验任务三

Complex.hpp
#ifndef _Complex_hpp
#define _Complex_hpp
#include <iostream>
#include <cmath>
using namespace std;

class Complex {
public:
    Complex(double a, double b = 0) : real { a }, imag { b }{}
    Complex(Complex const& comp) : real { comp.real }, imag { comp.imag }{}
    Complex() = default;
    double get_real()const
    {
        return real;
    }
    double get_imag()const
    {
        return imag;
    }
    void show()const
    {
        if ( imag == 0 ) {
            cout << real;
        }
        else if ( imag != 1 && imag != -1 ) {
            cout << real << ((imag < 0) ? " - " : " + ") << abs(imag) << "i";
        }
    }
    void add(Complex const& comp)
    {
        real += comp.real;
        imag += comp.imag;
    }
    friend Complex add(Complex const& comp1, Complex const& comp2);
    friend bool is_equal(Complex const& comp1, Complex const& comp2);
    friend double abs(Complex const& comp);
private:
    double real;
    double imag;
};

Complex add(Complex const& comp1, Complex const& comp2)
{
    Complex ret(0, 0);
    ret.real = comp1.real + comp2.real;
    ret.imag = comp1.imag + comp2.imag;
    return ret;
}

bool is_equal(Complex const& comp1, Complex const& comp2)
{
    if ( comp1.real == comp2.real && comp1.imag == comp2.imag ) {
        return 1;
    }
    return 0;
}

double abs(Complex const& comp)
{
    double ret = sqrt(comp.real * comp.real + comp.imag * comp.imag);
    return ret;
}

#endif
Complex.cpp
#include "Complex.hpp"
#include <iostream>

int main()
{
    using namespace std;

    Complex c1(2, -5);
    const Complex c2(1.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;
}
运行输出

实验任务四

User.hpp
#ifndef User_hpp
#define User_hpp
#include <iostream>
#include <string>
using namespace std;

class User {
public:
    User(string na, string p = "111111", string e = "") :name { na }, passwd { p }, email { e }
    {
        n++;
    }
    void set_email()
    {
        cout << "Enter email address: ";
        cin >> email;
    }
    void change_passwd()
    {
        string temp;
        int cnt = 0;
        cout << "Enter old password: ";
        cin >> temp;
        while (1) {
            if ( temp == passwd ) {
                cout << "Enter new passwd: ";
                cin >> temp;
                cout << "new passwd is set successfully..." << endl;
                passwd = temp;
                break;
            }
            else {
                cnt++;
                if ( cnt == 3 ) {
                    cout << "password input error. Please try after a while." << endl;
                    break;
                }
                cout << "password input error. Please re-enter again: ";
                cin >> temp;
            }
        }
    }
    void print_info()
    {
        cout << "name:   " << name << endl;
        cout << "passwd: ******" << endl;
        cout << "email:  " << email << endl;
    }
    static void print_n()
    {
        cout << "there are " << n << " users." << endl;
    }
private:
    string name;
    string passwd;
    string email;
    static int n;
};
int User::n = 0;

#endif
User.cpp
#include "User.hpp"
#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("ptyyyy", "998244353", "ptyyyy@gmail.com");
    user1.print_info();

    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("cannot follow the ranklist");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}
运行输出

标签:cout,对象,imag,hpp,Complex,实验,User,include
来源: https://www.cnblogs.com/ptyyyy/p/15450255.html