实验二 类和对象

实验任务4

task4.cpp

#include "Complex.hpp"
#include <iostream>
// 类测试

void test() {
    using namespace std;
    Complex c1(1, -8);
    const Complex c2(6.4);
    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();
}

Complex.hpp

#pragma once
#include <iostream>
#include <cmath>
using namespace std;
    
//Complex类的声明
class Complex {
public:
    Complex();
    Complex(double a, double b);
    Complex(double a);
    Complex(const Complex& obj);
    ~Complex() = default;

    //成员函数
    double get_real() const{ return real; }
    double get_imag() const{ return imag; }

    void show() const;
    void add(Complex c2);


    //友元函数
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);

    friend double abs(Complex c1);
private:
    double real;
    double imag;
};

Complex::Complex() {
    real = 0;
    imag = 0;
}
Complex::Complex(double a, double b) {
    real = a;
    imag = b;
}
Complex::Complex(double a) {
    real = a;
    imag = 0;
}
Complex::Complex(const Complex& obj):real{obj.real},imag{obj.imag}{}

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

void Complex::add(Complex c2){
    real += c2.real;
    imag += c2.imag;
}

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

bool is_equal(Complex c1, Complex c2) {
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    else
        return false;
}
 
double abs(Complex c1) {
   return sqrt(c1.real * c1.real + c1.imag * c1.imag);
}

使用的测试数据:

 c1=(1, -8) ,  c2=(6.4)

 实验任务5

task5.cpp

#include "User.hpp"
#include <iostream>
// 测试User类
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();
}

User.hpp

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

class User {
public:
    User(string mz);
    User(string mz, string mm, string yx);
    ~User() = default;

    //成员函数
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();

private:
    string name;
    string passwd;
    string email;
    static int count;       //静态数据成员声明
};

int User::count = 0;        //静态数据成员定义和初始化,使用类名限定

void User::print_n() { cout << "There are " << count << " Users." << endl; }
 
User::User(string mz) :name{ mz }, passwd{ "111111" }, email{""} { count++; }

User::User(string mz, string mm, string yx) :name{ mz }, passwd{ mm }, email{ yx } { count++; }

void User::set_email() {
    string address;
    cout << "enter email address:";
    cin >> address;
    email = address;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() {
    string old_passwd;
    string new_passwd;
    int t = 1;
    cout << "enter old passwd:";
    cin >> old_passwd;
    do {
        if (passwd == old_passwd) {
            cout << "enter new passwd:";
            cin >> new_passwd;
            passwd = new_passwd;
            break;
        }
        else
        {
            t++;
            cout << "passwd input error.Please re-enter again:";
            cin >> old_passwd;
        }
    } while (t < 3);
    if (t == 3) {
        cout << "passwd enter error.Please try after a while." << l;
    }
}
void User::print_info() {
    int length = passwd.length();
    cout << "name:   " << name << endl;
    cout << "passwd: ";
        while (length--) {
            cout << "*";
        }
        cout << endl;
    cout << "email:  " << email << endl;
}

测试数据1:

测试数据2:

 

posted @ 2022-10-19 00:44  Molancci  阅读(36)  评论(0)    收藏  举报