实验一 类与对象

task 3:

Complex.hpp源码:

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

class Complex {
private:
    double real;
    double imag;
public:
    Complex()
    {
        real = 0;
        imag = 0;
    };
    Complex(double x)
    {
        real = x;
        imag = 0;
    }
    Complex(double x, double y)
    {
        real = x;
        imag = y;
    }
    Complex(const Complex& c);
    double get_real() const;
    double get_imag() const;
    void show()const;
    void add(Complex c);
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs(Complex c);
};
Complex::Complex(const Complex& c)
{
    real = c.real;
    imag = c.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 if (imag < 0)
    {
        cout << real << " - " << -imag << "i";
    }
    else
    {
        cout << real;
    }
};
void Complex::add(Complex c)
{
    real += c.real;
    imag += c.imag;
};
Complex add(Complex c1, Complex c2)
{
    Complex c3;
    c3.real = c1.real + c2.real;
    c3.imag = c1.imag + c2.imag;
    return c3;
}
bool is_equal(Complex c1, Complex c2)
{
    if ((c1.real == c2.real) && (c1.imag == c2.imag))
    {
        return true;
    }
    else
    {
        return false;
    }
}
double abs(Complex c)
{
    double a = pow(c.real, 2) + pow(c.imag, 2);
    return pow(a, 0.5);
}

Complex.cpp源码:

#include "Complex.hpp"
#include <iostream>
int main()
{    
    using namespace std;
    Complex c1(6, -8);
    const Complex c2(4.2);
    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;
}

运行结果:

 

 ..........................................................................................

task 4:

User.hpp源码:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int N = 100;

class User {
private:
    char name[N];
    char password[N];
    char email[N];
    static int n;
public:
    User(const char na[],const char p[] = "111111",const char e[] = "")
    {
        strcpy(name, na);
        strcpy(password, p);
        strcpy(email, e);
        n++;
    }
    void set_email();
    void change_password();
    void print_info()const;
    static void print_n();
};
void User::set_email()
{
    cout << "请输入邮箱地址:";
    cin >> email;
    cout << "邮箱设置成功!" << endl;
}
void User::change_password()
{
    char a[N];
    cout << "请输入旧密码:";
    cin >> a;
    int cnt = 0;
    if (!strcmp(a, password))
    {
        cout << "请输入新密码:";
        cin >> password;
        cout << "新密码设置成功!" << endl;
        return;
    }
    while (strcmp(a, password))
    {
        cnt++;
        if (cnt == 3)
        {
            cout << "密码错误!请稍后再试。" << endl;
            return;
        }
        else if (!strcmp(a, password))
        {
            cout << "请输入新密码:";
            cin >> password;
            cout << "新密码设置成功!" << endl;
            return;
        }
        cout << "密码错误!请再此输入:";
        cin >> a;
    }
}
int User::n = 0;
void User::print_info()const
{
    cout << "name:" << name << endl;
    cout << "password:******" << endl;
    cout << "email:" << email << endl;
}
void User::print_n()
{
    cout << "当前有" << n << "位用户。" << endl;
}

User.cpp源码:

#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_password();
    user2.set_email();
    user2.print_info();
    User::print_n();
}

运行结果:

 

 

 

 实验总结:

理解了类与对象的运用

类属性定义可以通过static来实现

学会了对const的使用

学会了通过使用友元来实现对私有成员的访问

 

posted @ 2021-10-24 20:05  suanmeitang  阅读(36)  评论(3编辑  收藏  举报