实验二
实验任务4
Complex.hpp文件源码:
1 #include<iostream> 2 #include<math.h> 3 using namespace std; 4 class Complex { 5 public: 6 Complex(double r=0.0,double i=0.0) :real(r), imag(i) { } 7 Complex( Complex& t) :real(t.real), imag(t.imag) { } 8 double get_real()const { 9 return real; 10 }; 11 double get_imag()const { 12 return imag; 13 }; 14 void show()const { 15 cout << real; 16 if (imag >0) 17 cout << "+" << imag << "i"; 18 else if(imag<0) 19 cout << imag<<"i"; 20 }; 21 void add(const Complex& c) { 22 real += c.real; 23 imag += c.imag; 24 } 25 friend Complex add(const Complex &c1,const Complex &c2) { 26 Complex c3; 27 c3.real = c1.real + c2.real; 28 c3.imag = c1.imag + c2.imag; 29 return c3; 30 }; 31 friend bool is_equal(const Complex &c1,const Complex &c2) { 32 if (c1.real == c2.real && c1.imag == c2.imag) 33 return true; 34 else 35 return false; 36 }; 37 friend double abs(const Complex c) { 38 double l; 39 l = sqrt(c.real*c.real+c.imag*c.imag); 40 return l; 41 }; 42 private: 43 double real, imag; 44 };
task4.cpp源码:
1 #include "Complex.hpp" 2 #include <iostream> 3 4 // 类测试 5 void test() { 6 using namespace std; 7 8 Complex c1(3, -4); 9 const Complex c2(4.5); 10 Complex c3(c1); 11 12 cout << "c1 = "; 13 c1.show(); 14 cout << endl; 15 16 cout << "c2 = "; 17 c2.show(); 18 cout << endl; 19 cout << "c2.imag = " << c2.get_imag() << endl; 20 21 cout << "c3 = "; 22 c3.show(); 23 cout << endl; 24 25 cout << "abs(c1) = "; 26 cout << abs(c1) << endl; 27 28 cout << boolalpha; 29 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 30 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 31 32 Complex c4; 33 c4 = add(c1, c2); 34 cout << "c4 = c1 + c2 = "; 35 c4.show(); 36 cout << endl; 37 38 c1.add(c2); 39 cout << "c1 += c2, " << "c1 = "; 40 c1.show(); 41 cout << endl; 42 } 43 44 int main() { 45 test(); 46 }
运行测试结果截图:
更换数据后运行结果:
实验任务5
User.hpp文件源码:
1 #include<iostream> 2 using namespace std; 3 class User { 4 public: 5 User(string na, string p = "111111", string e="000000"); 6 void set_email() { 7 cout << "Enter email address:"; 8 cin >> email; 9 cout << " email is set successfully..." << endl; 10 } 11 void change_passwd(); 12 void print_info()const; 13 void static print_n(); 14 private: 15 string name ; 16 string passwd; 17 string email; 18 static int n; 19 }; 20 int User::n = 0; 21 User::User(string na, string p, string e) :name(na), passwd(p), email(e) { n++; }; 22 void User:: print_n() { 23 cout << "there are" << n << "users."; 24 } 25 void User::change_passwd() { 26 string old = passwd; 27 int count=0; 28 cout << "Enter old password:"; 29 cin >> passwd; 30 count ++; 31 while (passwd != old&&count<3) { 32 cout << "password inout error.Please re-enter again:"; 33 cin >> passwd; 34 count++; 35 } 36 if (count == 3) { 37 cout << "password inout error.Please try after a while" << endl; 38 } 39 if (count != 3) { 40 cout << "Enter new passwd:"; 41 cin >> passwd; 42 cout << "new password is set successfully..." << endl; 43 } 44 } 45 void User::print_info() const{ 46 cout << "name:" << name << endl; 47 int l; 48 l= size(passwd); 49 cout << "passwd:"; 50 while (l--) { 51 cout << "*"; 52 } 53 cout << endl; 54 cout << "email:" << email << endl; 55 }
task5.cpp源码 :
1 #include "User.hpp" 2 #include <iostream> 3 4 // 测试User类 5 void test() { 6 using std::cout; 7 using std::endl; 8 9 cout << "testing 1......\n"; 10 User user1("Jonny", "92197", "xyz@hotmail.com"); 11 user1.print_info(); 12 13 cout << endl 14 << "testing 2......\n\n"; 15 16 User user2("Marry"); 17 user2.change_passwd(); 18 user2.set_email(); 19 user2.print_info(); 20 21 cout << endl; 22 User::print_n(); 23 } 24 25 int main() { 26 test(); 27 }
测试二截图:

更换用户信息数据后测试:

浙公网安备 33010602011771号