实验一 类与对象

#ifndef COMPLEX_HPP
#define COMPLEX_HPP

#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;

class Complex
{
public:
    Complex(){};
    Complex(double a);
    Complex(double a, double b);
    Complex(const 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(Complex a, Complex b);
    friend double abs(Complex a, Complex b);

private:
    double real;
    double imag;
};

Complex::Complex(double a) : real{a}, imag{0}
{
}
Complex::Complex(double a, double b) : real{a}, imag{b} {}
Complex::Complex(const 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
{
    if (get_imag() < 0)
    {
        cout << real << " - " << 0 - imag << "i" << endl;
    }
    else
    {
        cout << real << " + " << imag << "i" << endl;
    }
}
void Complex::add(const Complex &p)
{
    real = real + p.real;
    imag = imag + p.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;
}

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

double abs(const Complex &p)
{
    return sqrt(p.get_real() * p.get_real() + p.get_imag() * p.get_imag());
}

#endif
#include "Complex.hpp"
#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;
}

实验一 类与对象

 

#ifndef USER_HPP
#define USER_HPP

#include <iostream>
#include <string>
#include <string.h>
#include <iomanip>
#include <math.h>

using namespace std;

class User
{
public:
    User();
    User(string a);
    User(string a, string b, string c);
    void set_email();
    void change_passwd();
    void print_info() const;
    static void print_n();

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

int User::count = 0;
User::User() { count++; }
User::User(string a) : name{a}, passwd{"111111"}, email{"\0"} { count++; }
User::User(string a, string b, string c) : name{a}, passwd{b}, email{c} { count++; }

void User::set_email()
{
    cout << "请输入邮箱: ";
    cin >> email;
    cout << "邮箱修改成功!" << endl;
}

void User::change_passwd()
{
    cout << "请输入旧密码:";
    string old_passwd;
    for (int i = 1; i <= 3; i++)
    {
        cin >> old_passwd;
        if (old_passwd == passwd)
        {
            cout << "请输入新密码:";
            cin >> passwd;
            cout << "密码修改成功!" << endl;
            return;
        }
        else if (i < 3)
        {
            cout << "密码输入错误,请重试:";
        }
    }

    cout << "密码输入错误次数过多,请稍后再试." << endl;
}

void User::print_info() const
{
    cout << "用户名:" << name << endl;
    cout << "密码:******" << endl;
    cout << "邮箱:" << email << endl;
}

void User::print_n()
{
    cout << "现在共有" << count << "位员工" << endl;
}
#endif
#include "User.hpp"
#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();
}

实验一 类与对象

 

实验一 类与对象

 

 

上一篇:python-代码复用(函数)


下一篇:Python3 tkinter基础 Entry validate isdigit 只能输入数字的输入框