实验二
- 实验任务4
Complex.hpp文件源码:
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 class Complex { 5 public: 6 Complex(); 7 Complex(double real0, double imag0=0); 8 Complex(const Complex& obj); 9 double get_real()const { return real; } 10 double get_imag()const { return imag; } 11 void show()const; 12 void add(Complex temp); 13 friend Complex add(Complex c1, Complex c2); 14 friend bool is_equal(Complex c1, Complex c2); 15 friend double abs(Complex c1); 16 private: 17 double real; 18 double imag; 19 }; 20 Complex::Complex():real{0},imag{0}{} 21 22 Complex::Complex(double real0,double imag0):real{real0},imag{imag0}{} 23 Complex::Complex(const Complex&obj):real{obj.real},imag{obj.imag}{} 24 void Complex::show()const { 25 if (imag == 0) 26 cout << real; 27 else 28 cout << real << imag << "i"; 29 } 30 void Complex:: add(Complex temp) { 31 real += temp.real; 32 imag += temp.imag; 33 } 34 Complex add(Complex c1, Complex c2) { 35 Complex c3; 36 c3.real = c1.real + c2.real; 37 c3.imag = c1.imag + c2.imag; 38 return c3; 39 } 40 bool is_equal(Complex c1, Complex c2) { 41 if (c1.imag = c2.imag && c1.real == c2.real) 42 return 1; 43 else 44 return 0; 45 } 46 double abs(Complex c1) { 47 return sqrt((c1.real * c1.real + c1.imag * c1.imag)); 48 }
task4.cpp源码:
1 #include "Complex.hpp" 2 #include <iostream> 3 4 // 类测试 5 void test() { 6 using namespace std; 7 8 Complex c1(6, -8); 9 const Complex c2(6.8); 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 }
运行结果:

- 实验任务五
User.hpp:
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 using namespace std; 6 class User { 7 public: 8 User(string name0, string passwd0 = "111111", string mail0 = " "); 9 void set_email(); 10 void change_passwd(); 11 void print_info(); 12 static void print_n(); 13 private: 14 string name; 15 string passwd; 16 string mail; 17 static int n; 18 }; 19 int User::n = 0; 20 User::User(string name0, string passwd0 , string mail0) :name{name0},passwd{passwd0}, 21 mail{ mail0 }{++n; } 22 void User::set_email() { 23 string new_mail; 24 cout << "Enter email adress: "; 25 cin >> new_mail; 26 mail = new_mail; 27 cout << "email is set successfully..." << endl; 28 } 29 void User::change_passwd() { 30 cout << "Enter old password: "; 31 string old_passwd, new_passwd; 32 int times = 0; 33 for (times; times < 3; times++) { 34 cin >> old_passwd; 35 if (old_passwd == passwd) 36 { 37 cout << "set new password: " ; 38 cin >> new_passwd; 39 passwd = new_passwd; 40 cout << "new password is set successfully..." << endl; 41 break; 42 } 43 else 44 cout << "password input error. "; 45 if (times == 2) 46 { 47 cout << "Please try after a while." << endl; 48 break; 49 } 50 cout << "Please re-enter again: "; 51 } 52 } 53 void User::print_info() { 54 string s1(passwd.length(), '*'); 55 cout<<std::left<<setw(10) << "name: " << name << endl; 56 cout << std::left << setw(10) <<"passwd: " << s1 << endl; 57 cout << std::left << setw(10) <<"email: " << mail << endl; 58 } 59 void User::print_n() { 60 cout << "there are " << n << " users" << endl; 61 }
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("Leonard"); 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号