实验1 类与对象

 

实验结论:

实验任务3:

Complex.h源码

#ifndef COMPLEX_HPP
#define COMPLEX_HPP

#include<cmath>
#include<iostream>
using namespace std;
class Complex {
public:
    Complex() {};
    Complex(double x, double y = 0) :real{ x }, imag{ y }{};
    Complex(const Complex& p)
    {
        real = p.real;
        imag = p.imag;
    }
    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(Complex c1) {
        real = real + c1.real;
        imag = imag + c1.imag;
    }
    friend Complex add(Complex c1, Complex c2)
    {
        Complex c3(c1.real + c2.real, c1.imag + c2.imag);
        return c3;
    }
    friend bool is_equal(Complex c1,Complex c2);
    friend double abs(Complex c1);
private:
    double real;
    double imag;
};

double Complex::get_real() const
{
    return real;
}
double Complex::get_imag() const
{
    return imag;
}
void Complex::show() const
{
    if (imag >= 0)
        cout << real << " " << "+" << " " << imag << "i" ;
    else
        cout << real << " " << "-" << " " << fabs(imag) << "i";
}
bool is_equal(Complex c1, Complex c2)
{
    if ((c1.imag == c2.imag) && (c1.real == c2.real)) return true;
    else return false;
}
double abs(Complex c1)
{
    return sqrt((c1.real * c1.real)+ (c1.imag * c1.imag));
}
#endif

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

实验截图

实验1 类与对象

添加一组数据

 

Complex c5(2, 2);
    Complex c6(c1);
    c5.add(c6);
    cout << "c5= ";
    c5.show();
    cout << endl;

结果

实验1 类与对象

 

实验任务4:

user.h源码

 

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

class User {
public:
    User(string a, string b = "111111", string c = "") :name{ a }, password{ b }, email{ c }{count++; };
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
private:
    string name;
    string password;
    string email;
    static int count;
};
int  User::count = 0;
void User::set_email()
{
    cout << "please enter new email: ";
    string a;
    cin >> a;
    bool fl =false;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] == '@') fl = true;
    }
    if (!fl)
    {
        cout << "invalid email"<<endl;
        return;
    }
    else
    {
        email = a;
        cout << "email set successfully..." << endl;
    }
}
void User::change_passwd()
{
    cout << "Enter old password: ";
    string a;
    int cnt = 1;
    bool fl = false;
    while (cnt <= 3)
    {
        if (fl) break;
        cin >> a;
        if (a != password && cnt < 3)
        {
            cout << "password input error please retry: ";
        }
        else if (a == password)
        {
            cout << "enter new password: ";
            cin >> a;
            cout << "new password set successfully"<<endl;
            password = a;
            fl = true;
        }
        cnt++;
    }
    if (!fl)
    {
        cout << "your have entered 3 wrong password! please try later." << endl;
        return;
    }
}
void User::print_info()
{
    cout << "name: " << " " << name << endl;
    cout << "password: " << " ******" << endl;
    cout << "email: " << " " << email << endl;
}
void User::print_n()
{
    cout << "there are" << " " << count << " " << "users" << endl;
}

 

 

 

 

 

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

运行截图

实验1 类与对象

 

实验1 类与对象

 更换测试数据

实验1 类与对象

 

 实验总结:

在这次的实验中,我学会创建项目将程序分成类头文件和执行文件。在编写类的过程中,我熟练运用了构造函数,常函数,友元等知识。当然还有一些尚存的问题,就是有关task4的邮箱和密码的扩展运用,我的逻辑判断可能太繁琐,可以用另外的函数简化。

 

上一篇:Collider Collision 区别


下一篇:【.NET Core项目实战-统一认证平台】第七章 网关篇-自定义客户端限流