实验一 类与对象

实验任务3

 

"Complex.hpp":

#include<iostream>
#include<cmath>
using namespace std;

class Complex {
    
public:
    Complex (double a = 0.0,double b = 0.0): real{a}, imag{b} {}
    Complex (const Complex &c1);
    double get_real () const { return real; }
    double get_imag () const { return imag; }
    void show () const;
    void add (Complex const &c1);
    friend Complex add (Complex const &c1, Complex const &c2);
    friend bool is_equal (Complex const &c1, Complex const &c2);
    friend double abs (Complex const &c1); 
    
private:
    double real, imag;
};

Complex::Complex (const Complex &c1) {
    real = c1.real;
    imag = c1.imag;
}

void Complex::show () const {
    if (imag == 0)
        cout << real ;
    else if (imag > 0)
        cout << real << " + " << imag << "i" << endl;
    else
        cout << real << " - " << fabs(imag) << "i" << endl;
}

void Complex::add (Complex const &c1) {
    real += c1.real;
    imag += c1.imag;
}


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

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

double abs (Complex const &c1) {
    return sqrt(pow(c1.get_real(), 2) + pow(c1.get_imag(), 2));
}

 

"task3.cpp":

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

int main(){
    using namespace std;

    Complex c1(6, -2);
    const Complex c2(3.7);
    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":

#include <iostream>
#include <string>
using namespace std;

class User {
    
public:
    User (string na, string p = "111111", string e = ""): name(na), password(p), email(e) { n++; }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
    
private:
    string name, password, email;
    static int n; 
}; 

int User::n = 0;

void User::set_email() {
    cout << "Enter email adress: ";
    cin >> email;
    cout << "email is set successfully..." << endl;
}

void User::change_passwd() {
    string temp1, temp2;
    int count = 0;
    cout << "Enter old password: ";
    while (temp1 != password && count <= 2) {
        cin >> temp1;
        if (temp1 == password) {
            cout << "Enter new password: ";
            cin >> temp2;
            password = temp2;
            cout << "new password is set successfully..." << endl;
            return;
        }
        else
            cout << "password input error. Please re-enter again: ";
        count++;
    } 
    cout << "password input error. Please try after a while." << endl;
    return;
}

void User::print_info() {
    cout << "name:    " << name << endl;
    cout << "passord: " << "******" <<endl;
    cout << "email:   " << email << endl;
}

void User::print_n() {
    if (n == 0)
        cout << "there are no user." << endl;
    else if (n == 1)
        cout << "there is 1 user." << endl;
    else
        cout << "there are " << n << "users." << endl;
}

 

"task4.cpp":

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

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Bill Gates", "114514", "appleissobad@outlook.com");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Tim Cook");
    user2.change_passwd();
    user2.set_email();
    cout << endl;
    user2.print_info();

    User::print_n();
}

 

运行结果:

实验一 类与对象

 实验一 类与对象

 

上一篇:【转】如何快速定位JVM中消耗CPU最多的线程?


下一篇:Kafka分区分配策略-RangeAssignor、RoundRobinAssignor、StickyAssignor