实验一 类与对象

task3 Complex.hpp

 

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

class Complex {
private:
    double real;
    double imag;
public:
    Complex(double x = 0, double y = 0) :real(x), imag(y) {}
    Complex(Complex& p);
        double get_real() const;
        double get_imag() const;
        void show()const;
        void add(const Complex& p);
        friend Complex add(const Complex& a, const Complex& b);
        friend bool is_equal(const Complex& a, const Complex& b);
        friend double abs(Complex& a);
};

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

double Complex::get_real()const {
    return real;
}

double Complex::get_imag()const {
    return imag;
}

void Complex::show()const {
    cout << real;
    if (imag < 0)
        cout << imag << 'i';
    else if (imag > 0)
        cout << ' + ' << imag << 'i' ;
}

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

Complex add(const Complex& a, const Complex& b) {
    Complex c;
    c.real = a.real+ b.real;
    c.imag = a.imag+ b.imag;
    return c;
}

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

double abs(Complex& a) {
    return sqrt(a.real * a.real + a.imag * a.imag);
}

task3 主函数 : 

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

#include<cmath>

int main()
{
    using namespace std;

    Complex c1(8, -3);
    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;
}

结果截图:

 

实验一 类与对象

 

 

 

task4 User.hpp

#ifndef USER_HPP
#define USER_HPP

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

class User {
private:
    string name;
    string password;
    string email;
    static int count;
public:
    User(string name0, string password0 = "111111", string email0 = "******") :name{ name0 }, password{ password0 }, email{ email0 }{++count; }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
};

int User::count = 0;

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

void User::change_passwd() {
    string oldp, newp;
    int i = 0;
    cout << "please enter old password:";
    while (i < 3) {
        cin >> oldp;
        if (oldp == password) {
            cout << "please enter new passwrod:";
            cin >> newp;
            password = newp;
            cout <<"new passwd is successfully..." <<endl;
            break;
        }
        else {
            cout << "password input error.Please re-enter again:";
            i++;
        }
    }
    if(i>=3) {
        cout << "password input error.Please try after a while." << endl;
    }
}

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

void User::print_n() {
    cout << "there are " << count << " users." << endl;
}
#endif // !USER_HPP

 

task4 主函数:

#include "User.hpp"
#include <iostream>
#include "../../CPP_tasks/task4/task4/User.hpp"

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Hershel", "020312", "1780304254@qq.com");
    user1.print_info();

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

    User::print_n();
}

 

结果截图:

实验一 类与对象

 

实验一 类与对象

 

 

 

 

 

 

上一篇:Git submodule 特性


下一篇:10.20