实验一 类与对象

实验任务3

//task3.cpp
#include "Complex.h"
#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;
}

  

//Complex.h
#ifndef LAB1_COMPLEX_H
#define LAB1_COMPLEX_H

#include "iostream"

using namespace std;

class Complex {
public:
    Complex(double xx = 0, double yy = 0) {
        x = xx;
        y = yy;
    }

    ~Complex() {}

    Complex(const Complex &c) {
        x = c.x;
        y = c.y;
    }

    double get_real() const { return x; }

    double get_imag() const { return y; }

    void show() const {
        if (y >= 0) {
            cout << x << '+' << y << 'i' << endl;
        } else {
            cout << x << y << 'i' << endl;
        }
    }

    void add(Complex c1) {
        x = x + c1.x;
        y = y + c1.y;
    }

    friend Complex add(Complex c1, Complex c2);

    friend string is_equal(Complex c1,Complex c2);

    friend double abs(Complex c);


private:
    double x;
    double y;
};


#endif //LAB1_COMPLEX_H

  

//Complex.cpp
#include "Complex.h"
#include "cmath"
#include "string"

Complex add(Complex c1, Complex c2) {
    double x = c1.get_real() + c2.get_real();
    double y = c1.get_imag() + c2.get_imag();
    Complex c(x, y);
    return c;
}

string is_equal(Complex c1, Complex c2) {
    if (c1.get_real() == c2.get_real() && c1.get_imag() == c2.get_imag()) {
        return "ture";
    } else {
        return "false";
    }
}

double abs(Complex c) {
    double x = c.get_real();
    double y = c.get_imag();
    return sqrt(x * x + y * y);
}

 实验一 类与对象

 

 更换数据后:

实验一 类与对象

 

实验任务4

//task4.cpp
#include "User.h"
#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();
}

 

//User.h
#ifndef LAB2_USER_H
#define LAB2_USER_H

#include "iostream"
#include "string"

using namespace std;

class User {
public:
    User(string n = "", string p = "111111", string e = "") {
        name = n;
        password = p;
        email = e;
        count++;
    }

    ~User() {};

    void set_email() {
        string temp;
        cout << "please enter email adress:";
        cin >> temp;
        email = temp;
    }

    void change_passwd() {
        cout << "please enter old password:";
        int flag = 0;
        string temp;
        while (1) {
            if (flag == 3) {
                cout << "please try again later..." << endl;
                return;
            }
            cin >> temp;
            if (temp == password) { break; }
            else {
                cout << "error! try again" << endl;
                flag++;
            }
        }
        cout << "please enter new password:";
        cin >> temp;
        email = temp;
        cout << "set successful!" << endl;
    }

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

    static void print_n();

private:
    string name;
    string password;
    string email;
    static int count;
};


#endif //LAB2_USER_H
//User.cpp
#include "User.h"

int User::count = 0;

void User::print_n() {
    cout << "there are " << count << " users" << endl;
}

实验一 类与对象

 

实验一 类与对象

 

上一篇:实验一 类与对象


下一篇:实验一 类和对象