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

【實驗一】類與物件

@

目錄

1、任務一

不使用C++標準庫,自行設計並實現一個複數類Complex,使其滿足如下要求:

  • 資料成員
    用來表示複數的實部real和虛部imag,實部、虛部,均為小數形式。
  • 函式成員
    • 建構函式
      支援以下方式定義複數物件:
      Complex c1; // 不帶引數
      Complex c2(3); // 只有一個引數,相當於3 + 0i
      Complex c3(3, 4); // 兩個引數,相當於3 + 4i
      Complex c4(c3); // 用c3構造c4
      
    • 成員函式
      • get_real() 返回複數實部
      • get_imag() 返回複數虛部
      • show() 用於輸出複數。要求以 3 + 4i , 3 - 4i 這樣的形式輸出
      • add() 用於把一個複數加到自身,比如 c1.add(c2) ,相當於 c1 += c2
  • 友元函式
    • add() 用於實現兩個複數相加,返回複數。比如 c3 = add(c1, c2);
    • is_equal() 用於判斷兩個複數是否相等,返回true/false。比如 is_equal(c1, c2)
    • abs() 用於對複數進行取模運算
      將類Complex的定義及友元函式實現,單獨儲存在檔案Complex.hpp中。
      使用ComplexTest.cpp中的程式碼,測試類Complex的各項介面是否正確。

Complex.hpp

#include <iostream>
#include <cmath>

class Complex
{
private:
    double a;
    double b;

public:
    Complex();
    Complex(double a);
    Complex(double a, double b);
    Complex(const Complex &complex_temp);
    ~Complex();

    double get_real() const;
    double get_imag() const;
    void show() const ;
    void add(Complex complex_temp);
    Complex add(const Complex complex_temp) const;
    bool equal(const Complex complex_temp) const;
    double abs(const Complex complex1) const;

    friend Complex add(const Complex complex1, const Complex complex2);
    friend bool is_equal(const Complex complex1, const Complex complex2);
    friend double abs(const Complex complex_temp);
};

Complex::Complex()
{
}
Complex::Complex(double a) : a{a}
{
}
Complex::Complex(double a, double b) : a{a}, b{b}
{
}
Complex::Complex(const Complex &complex_temp) : a{complex_temp.a}, b{complex_temp.b}
{
}

Complex::~Complex()
{
}

double Complex::get_real() const
{
    return a;
}

double Complex::get_imag() const
{
    return b;
}

void Complex::show() const
{
    std::cout << a << " + " << b << "i";
}

void Complex::add(Complex complex_temp)
{
    a += complex_temp.a;
    b += complex_temp.b;
}

Complex Complex::add(const Complex complex_temp) const
{
    return Complex(a + complex_temp.get_real(), b + complex_temp.get_imag());
}

bool Complex::equal(const Complex complex_temp) const
{
    if(a == complex_temp.get_real() && b == complex_temp.get_imag()){
        return true;
    }
    return false;
}

double Complex::abs(const Complex complex_temp) const
{
    return sqrt(pow(complex_temp.get_imag(), 2) + pow(complex_temp.get_real(), 2));
}


Complex add(const Complex complex1, const Complex complex2);
bool is_equal(const Complex complex1, const Complex complex2);
double abs(const Complex complex_temp);

Complex add(const Complex complex1, const Complex complex2){
    complex1.add(complex2);
    return complex1;
}
bool is_equal(const Complex complex1, const Complex complex2){
    return complex1.equal(complex2);
}
double abs(const Complex complex_temp){
    return(complex_temp.abs(complex_temp));
}

ComplexTest.cpp

#include "Complex.hpp"
#include <iostream>
#include <cmath>

using namespace std;


int main(){
    using namespace std;
    Complex c1(5, -4);
    const Complex c2(5.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;
    system("pause");
    return 0;
}

執行結果:

2、任務二

設計並實現一個使用者類User,並在主函式中使用和測試這個類。具體要求如下:

  • 資料成員
    每個使用者有使用者名稱(name)、密碼(passwd)、聯絡郵箱(email)三個屬性。
    還有一個類屬性,用於記錄使用者總數n。
  • 函式成員
    • 建構函式
    User u1("Mary"); // 密碼和郵箱,使用預設設定
    User u2("John", "112233", "[email protected]")
    
    如果定義使用者物件時未設定密碼和郵箱,密碼預設為6個1,聯絡郵箱預設為空串。
    • 成員函式
      • set_email()
        設定郵箱。提示使用者從鍵盤輸入郵箱。
      • change_passwd()
        修改密碼。修改密碼前,要求先輸入舊的密碼,驗證無誤後,才允許修改;如果輸入舊
        密碼時,連續三次輸入錯誤,則提示使用者稍後再試,暫時退出修改密碼程式。
      • print_info()
        列印使用者名稱、密碼、聯絡郵箱。其中,密碼以6個*方式顯示。

User類的定義和實現,儲存在檔案User.hpp中:

#include <iostream>
#include <string>

using namespace std;

class User
{
private:
    string name;
    string passwd;
    string email;
    static int count;

public:
    User();
    User(string name);
    User(string name, string passwd, string email);
    ~User();

    static void print_count();

    void set_email();
    void change_passwd();
    void print_info();
};

int User::count = 0;
void User::print_count(){
    cout << "\nthere are " << count << "users\n";
}

User::User()
{
    this->name = "無";
    this->passwd = "666666";
    this->email = "[email protected]";
    this->count++;
}
User::User(string name) : name{name}
{
    this->passwd = "666666";
    this->email = "[email protected]";
    this->count++;
}
User::User(string name, string passwd, string email) : name{name}, passwd{passwd}, email{email}
{

    this->count++;
}

User::~User()
{

    this->count++;
}

void User::set_email()
{
    string email_temp;
    string passwd_temp;
    cout << "\nDear " << name << "~" << "\nCheck password: ";
    cin >> passwd_temp;
    while (passwd_temp != passwd)
    {
        cout << "password input error. Please re-enter again: ";
        cin >> passwd_temp;
    }
    cout << "Enter email address: ";
    cin >> email_temp;
    this->email = email_temp;
    cout << "email is set successfully...\n";
}

void User::change_passwd()
{
    string passwd_temp;
    cout << "\nDear " << name << "~" << "\nEnter old password: ";
    cin >> passwd_temp;
    while (passwd_temp != passwd)
    {
        cout << "password input error. Please re-enter again: ";
        cin >> passwd_temp;
    }
    cout << "Enter new passwd: ";
    cin >> passwd_temp;
    this->passwd = passwd_temp;
    cout << "passwd changed successfully\n";
}

void User::print_info()
{
    cout << "\n使用者名稱:" << name << "\n密碼:******" << "\n郵箱:" << email << endl;
}

User類的使用和測試,儲存在檔案UserTest.cpp中:

#include "Complex.hpp"
#include <iostream>
#include <cmath>

using namespace std;


int main(){
    using namespace std;
    Complex c1(5, -4);
    const Complex c2(5.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;
    system("pause");
    return 0;
}

執行結果:

實驗總結

const的用法,以及建構函式、初始化列表、友元函式。
加深對面向物件中封裝的理解