实验二 类和对象

实验四

.cpp

#include "Complex.hpp"
#include <iostream>
void test() {
    using namespace std;

    Complex c1(7, -8);
    const Complex c2(7.8);
    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();
}
.hpp
#pragma
once #include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(double a = 0, double b = 0) :real{ a }, imag{ b } {}; Complex(const Complex& p) :real{ p.real }, imag{ p.imag } {}; void show()const; double get_imag()const; double get_real()const; void add(const Complex& p); friend bool is_equal(const Complex& a, const Complex& b); friend double abs(const Complex& p); friend Complex add(const Complex& a, const Complex& b); private: double real;double imag; }; void Complex::show()const { if (real == 0 && imag == 0) { cout << 0; } else if (real == 0 && imag != 0) { cout << imag << "i"; } else if (real != 0 && imag == 0) { cout << real; } else if (real != 0 && imag != 0) { if (imag < 0) { cout << real << imag << "i"; } else { cout << real << "+" << imag << "i"; } } }; double Complex::get_imag()const { return imag; } double Complex::get_real()const { return real; } double abs(const Complex& p) { return sqrt(p.get_real() * p.get_real() + p.get_imag() * p.get_imag()); } Complex add(const Complex& a, const Complex& b) { Complex c(a.get_real() + b.get_real(), a.get_imag() + b.get_imag()); return c; } void Complex::add(const Complex& p) { real += p.get_real(); imag += p.get_imag(); } bool is_equal(const Complex& a, const Complex& b) { bool i; if (a.get_real() == b.get_real() && a.get_imag() == b.get_imag()) { i = 1; } else { i = 0; } return i; }

实验五

.cpp

#include "User.hpp"
#include <iostream>
#include <string>
using namespace std;
void test() {
    using std::cout;
    using std::endl;
    using std::string;

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

.hpp

#pragma once
#include <iostream>
#include <string>
using namespace std;
class User 
{
public:
    User(string n, string p = "111111", string e = "") :
        name{ n }, passwd{ p }, email{ e } 
        {
            num ++;
        }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() 
    {
        cout << "there are " << num << " users.";
    }
private:
    string name, passwd, email;
    static int num;
};
int User::num = 0;
void User::set_email() 
{
    cout << "Enter email address: " ;
    string email_;
    cin >> email_;
    email = email_;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() 
{
    string o, n;
    int count = 1;
    cout << "Enter old password: ";
    cin >> o;
    while(o!= passwd) 
    {
        cout << "password input error. Please re-enter again: ";
        cin >> o;
        count++;
        if(count == 3)
         {
            cout << "password input error. Please try after a while. " << endl;
            return;
        }
    }
    cout << "Enter new password: ";
    cin >> n;
    passwd = n;
    cout << "new passwd is set successfully..." << endl;
}
void User::print_info() 
{
    string password(passwd.length(), '*');
    cout << "name:\t" << name << endl;
    cout << "passwd:\t" << password << endl;
    cout << "email:\t" << email << endl;
}
View Code

 

posted @ 2022-10-18 14:18  旅、程  阅读(38)  评论(0)    收藏  举报