實驗一 類與物件
阿新 • • 發佈:2021-10-22
實驗任務3 Complex類的自我實現
簡要說明:不使用C++標準庫,自行設計並實現一個複數類Complex,並滿足簡單的使用需求。
程式由 Complex.hpp 與 task3.cpp 兩部分構成,分別完成複數類定義及實現,主程式展示的功能。
Complex.hpp程式碼如下:
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <cmath> using std::cout; using std::endl; class Complex { public: Complex(double r = 0, double i = 0) : real(r), image(i) {} Complex(const Complex &c) : real(c.real), image(c.image) {} double get_real(); double get_imag() const; void show() const; void add(const Complex &c); friend Complex add(const Complex &c1, const Complex &c2); friend bool is_equal(const Complex& c1, const Complex& c2); friend double abs(const Complex& c); private: double real; double image; }; double Complex::get_real() { return real; } double Complex::get_imag() const { return image; } void Complex::show() const { if(real == 0) cout << image << "i" << endl; if(image == 0) cout << real << endl; if(image < 0) cout << real << " - " << -image << "i" << endl; } void Complex::add(const Complex &c) { real += c.real; image += c.image; } Complex add(const Complex& c1, const Complex& c2) { Complex r; r.real = c1.real + c2.real; r.image = c1.image + c2.image; return r; } bool is_equal(const Complex& c1, const Complex& c2) { if (c1.real == c2.real && c1.image == c2.image) { return true; } else { return false; } } double abs(const Complex& c) { double ret = sqrt(c.real * c.real + c.image * c.image); return ret; } #endif // !COMPLEX_HPP
主程式 task3.cpp程式碼如下:
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(1, -2); const Complex c2(6.7); 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; }
使用更改的資料的測試結果:
實驗任務4 User類的設計與實現
簡要說明:設計並實現一個使用者類User, 滿足基礎的建立使用者,修改密碼與郵箱的功能,並進行簡單的合法校驗。
程式由 User.hpp 與 task4.cpp 兩部分構成,分別完成使用者類定義及實現,主程式展示的功能。
User.hpp 程式碼如下:
#ifndef USER_HPP #define USER_HPP #include <iostream> #include <string> using std::string; using std::cin; using std::cout; using std::endl; class User { public: User(string name, string pwd="111111", string email="") : name(name), password(pwd), email(email) { cnt++; } void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name; string password; string email; static int cnt; }; int User::cnt = 0; void User::set_email() { string newemail; cout << "Enter email address: "; cin >> newemail; while (newemail.find('@') == -1) { cout << "Wrong! There must be an '@' in your email address!\nPlease re_enter again: "; cin >> newemail; } email = newemail; cout << "email is set sucessfully..." << endl; } void User::change_passwd() { string oldpwd; string newpwd; int t = 0; cout << "Enter old password: "; cin >> oldpwd; while (true) { if (oldpwd == password) { cout << "Enter new password: "; cin >> newpwd; while (newpwd.length() != 6) { cout << "The password can only contain 6 characters!\nPlease re_enter again: "; cin >> newpwd; } password = newpwd; cout << "new password is set successfully..." << endl; break; } else { if (t >= 2) { cout << "Password input error. Please try after a while." << endl; break; } cout << "Password input error. Please re_enter again: "; cin >> oldpwd; t++; } } } void User::print_info() { cout << "name:\t" << name << endl; cout << "passwd:\t" << "*****" << endl; cout << "email:\t" << email << endl; } void User::print_n() { cout << "there are " << cnt << " users." << endl; } #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();
}
使用更改的資料的測試結果: