实验结论
task 4
Complex.hpp
#include<iostream> #include<math.h> using namespace std; class Complex{ public: Complex (double x = 0, double y = 0) : real{x}, imag{y} {} Complex (const Complex &obj) : real{obj.real} , imag{obj.imag} {} double get_real() const { return real; } double get_imag() const { return imag; } void show() const { cout << real; if(imag > 0) cout << " + " << imag << "i"; else if(imag < 0) cout << " - " << -imag << "i"; } void add(const Complex &obj){ real += obj.get_real(); imag += obj.get_imag(); } friend Complex add(const Complex &a, const Complex &b){ return Complex(a.real + b.real, a.imag + b.imag); } friend bool is_equal(const Complex &a, const Complex &b){ return fabs(a.real - b.real) <= 1e-5 && fabs(a.imag - b.imag) <= 1e-5; } friend double abs(const Complex &obj){ return sqrt(obj.real * obj.real + obj.imag * obj.imag); } private: double real, imag; };
task4.cpp
#include<iostream> #include"Complex.hpp" void test(){ 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; } int main(){ test(); }
测试结果
更换一组复数的测试结果
task 5
User.hpp
#include<iostream> #include<string> using namespace std; class User{ public: User (string Name, string Passwd = "111111", string Email = "") : name{Name} , passwd{Passwd}, email{Email} {n ++; } void set_email(){ cout << ("Enter email address: "); cin >> email; puts("email is set successfully..."); } void change_passwd(){ string Passwd; cout << ("Enter old password: "); cin >> Passwd; for(int wrong = 1; wrong <= 2 && Passwd != passwd; wrong ++){ cout <<("password input error. Please re-enter again: "); cin >> Passwd; } if(Passwd == passwd){ cout << "Enter new passwd: "; cin >> passwd; puts("new passwd is set successfully..."); } else puts("password input error. Please try after a while."); } void print_info(){ cout << "name: " << name << "\n"; cout <<"passwd: "; for(int i = 0; passwd[i]; i ++) cout << "*"; cout << "\n"; cout << "email: " << email << "\n"; } static void print_n(){ cout << "there are " << n << " users.\n"; } private: string name, passwd, email; static int n; }; int User::n = 0;
task5.cpp
#include<iostream> #include"User.hpp" void test(){ using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main(){ test(); }
测试结果
更换一组测试信息的测试结果
标签:const,cout,对象,void,Passwd,Complex,实验,include From: https://www.cnblogs.com/lyhy/p/16785629.html