1. 程式人生 > 其它 >實驗一:類與物件

實驗一:類與物件

實驗三

<task3.cpp>

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

<Complex.hpp>

# ifndef COMPLEX_HPP
# define COMPLEX_HPP
# include<iostream>
//複數類
class Complex {
private:
    double real;
    double imag;
public:
    Complex() {}
    Complex(double a) :real(a), imag(0) {}
    Complex(double a, double b) :real(a), imag(b) {}
    Complex(const Complex& p) :real(p.get_real()), imag(p.get_imag()) {}        
    ~Complex() {}
    double get_real()const;
    double get_imag()const;
    void show()const;
    Complex add(const Complex& p);
    friend Complex add(const Complex& p,const Complex &q);
    friend bool is_equal(const Complex& p, const Complex& q);
    friend double abs(const Complex& p);
};
//返回實部
double Complex::get_real()const
{    
    return real;
}
//返回虛部
double Complex::get_imag()const
{
    return imag;
}
//列印複數
void Complex::show()const                                                                                                                                                                                                                                                                                                                                
{
    if (imag > 0)
        std::cout << real << "+" << imag << "i" << std::endl;
    else if (imag < 0)
        std::cout << real << imag << "i" << std::endl;
    else
        std::cout << real << std::endl;
}
//一個複數加到自身
Complex Complex::add(const Complex& p)
{
    real += p.get_real();
    imag += p.get_imag();
    return(real, imag);
}
//兩個複數加法,值返回
Complex add(const Complex& p, const Complex& q)
{
    return Complex(p.real+ q.real, + p.imag+q.imag);
}
//判斷兩個複數相等
bool is_equal(const Complex &p, const Complex &q)
{
    if (p.real == q.real && p.imag == q.imag)
        return true;
    else
        return false;
}
//取模運算
double abs(const Complex &p)
{
    double a = p.real;
    double b = p.imag;
    return sqrt(a * a + b * b);
}
#endif

執行結果:

實驗四

<task4.cpp>

#include "User.hpp"
#include <iostream>
int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();
    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
}

<User.hpp>

# ifndef USER_HPP
# define USER_HPP
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//使用者類
class User {
private:
    string name;
    string password;
    string email;
    static int count;
public:
    User(string a) :name(a), password("111111"), email{ "\0" } {count++; }
    User(string a, string b, string c) :name(a), password(b), email(c) { count++; }
    ~User() {}
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
};
//使用者人數初始為0
int User::count = 0;
//設定郵箱
void User::set_email()
{
    cout << "Enter email address: ";
    string a;
    while (1)
    {
        cin >> a;
        if (a.find('@')!=string::npos && a.find(".com")!=string::npos)//判斷是否建立正確
        {
            cout << "email is set successfully." << endl;
            email = a;
            break;
        }
        else
            cout << "email is error,try again: " ;
    }
}
//修改密碼
void User::change_passwd()
{
    int n = 3;
    cout << "Enter old password: ";
    while (n--)
    {
        string a;
        cin >> a;
        //驗證密碼
        if (a == password)//密碼正確
        {
            cout << "Right,enter new password: ";
            while (1)
            {
                cin >> a;
                if (a.length() < 7)//密碼小於7位
                {
                    password = a;
                    cout << "password is changed successfully"<<endl;
                    break;
                }
                else
                    cout << "new password input error.Please re-enter again: ";
            }
            break;
        }
        else//密碼錯誤
            if (n != 0)
                cout << "password input error.Please re-enter again: ";
            else
                cout << "password input error.Please try after a while" << endl;
    }

}
//列印個人資訊
void User::print_info()
{
    cout << left << setw(10) << "name:" << name << endl << setw(10) << "password:" << "******" << endl <<
        setw(10) << "email:" << email << endl;
}
//列印人數
void User::print_n()
{
    if (count > 1)
        cout << "there are " << count << " users.";
    else if (count == 1)
        cout << "there is 1 user.";
    else
        cout << "none";
}
#endif

執行結果1:

執行結果2: