实验2 类和对象(2)

实验任务4

Complex.h

#pragma once
#include<iostream>
#include<cmath>

using namespace std;

class Complex {
public:
    //Complex();
    Complex(double real2, double imag2);
    Complex(const Complex &obj);

public:
    double get_real() const{ return real; }
    double get_imag() const { return imag; }
    void show() const {    
        if (imag == 0) {
            cout << real;
        }
        else if (imag > 0) {
            cout << real << " + " << imag << "i";
        }
        else
            cout << real << " - " << abs(imag) << "i";
         ;}
    void add(const Complex &obj);

    friend bool is_equal(const Complex &obj1,const Complex &obj2);
    friend double abs(const Complex &obj);
    friend Complex add(const Complex &obj1, const Complex &obj2);

private:
    double real, imag;
};

//Complex::Complex():real{0},imag{0}{}
Complex::Complex(double real=0.0,double imag=0.0):real{real},imag{imag}{}
Complex::Complex(const Complex& obj) {
    real = obj.real;
    imag = obj.imag;
}
void Complex::add(const Complex &obj){
    real += obj.real;
    imag += obj.imag;
}
bool is_equal(const Complex& obj1, const Complex& obj2){
    if (obj1.real == obj2.real && obj1.imag == obj2.imag) {
        return true;
    }
    else
        return false;
}
double abs(const Complex& obj) {
    return sqrt( obj.real * obj.real + obj.imag * obj.imag);
}
Complex add(const Complex &obj1,const Complex &obj2) {
    return Complex(obj1.real + obj2.real, obj1.imag + obj2.imag);
}

task4.cpp

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

void test() {
    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;
}

int main()
{
    test();
}

运行测试结果:

 

 更换数据后:

实验任务5

 User.h

#pragma once
#include<iostream>
#include<string>

using namespace std;
using std::to_string;

class User {
public:
    User(string name0, string passwd0 = "111111", string email0 = "") :name{name0}, passwd{passwd0}, email{email0} { count++; }
    void set_email() ;
    void change_passwd() ;
    void print_info() ;
    static void print_n() { cout << "there are " << count << " users" << endl; }

public:
    static int count;

private:
    string name;
    string passwd;
    string email;
};
int User::count = 0;

void User::set_email()  {
    string email1;
    cout << "Enter email address: ";
    cin >> email1;
    email = email1;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd()  {
    string passwd1, passwd2;
    cout << "enter old password: ";
    cin >> passwd1;
    if (passwd1.compare(passwd) == 0) {
        cout << "enter new passwd: ";
        cin >> passwd2;
        passwd = passwd2;
        cout << "new passwd is set successfully..." << endl;
    }
    else{
        cout << "password input error.please re-enter again:";
        cin >> passwd1;
        if (passwd1.compare(passwd)==0) {
            cout << "enter new passwd: ";
            cin >> passwd2;
            passwd = passwd2;
            cout << "new passwd is set successfully..." << endl;
        }
        else {
            cout << "password input error.please re-enter again:";
            cin >> passwd1;
            if (passwd1.compare(passwd) == 0) {
                cout << "enter new passwd: ";
                cin >> passwd2;
                passwd = passwd2;
                cout << "new passwd is set successfully..." << endl;
            }
            else {
                cout << "password input error.";
                cout << "please try after a while." << endl;

            }
        }
    }
}
void User::print_info()  {
    cout << "name: " << name << endl;
    cout << "passwd: ";
    string s1(passwd.length(), '*');
    cout << s1 << endl;
    cout << "email: " << email << endl;
}

task5.cpp

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

void test() {
    using std::cout;
    using std::endl;
    
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
        << "testing 2......\n\n";
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main() {
    test();
}

运行测试结果;

测试2:

 

posted @ 2022-10-16 20:56  kierborn  阅读(19)  评论(0)    收藏  举报