4. 实验任务4
不使用C++标准库,自行设计并实现一个简化版复数类Complex
Complex.hpp
1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4 5 class Complex { 6 7 public: 8 Complex(double r = 0, double i = 0): real{r}, imag{i} {} 9 Complex(const Complex& obj): real{obj.real}, imag{obj.imag} {} 10 11 double get_real() const { 12 return real; 13 } 14 double get_imag() const { 15 return imag; 16 } 17 void show() const; 18 void add(const Complex& c); 19 20 friend Complex add(Complex c1, Complex c2); 21 friend bool is_equal(Complex c1, Complex c2); 22 friend int abs(Complex c1); 23 24 private: 25 double real, imag; 26 }; 27 28 void Complex::show() const { 29 if(imag == 0) { 30 cout << real; 31 } 32 else if(imag < 0) { 33 cout << real << " - " << -1 * imag << "i"; 34 } else{ 35 cout << real << " + " << imag << "i"; 36 } 37 } 38 void Complex::add(const Complex& c) { 39 real += c.real; 40 imag += c.imag; 41 } 42 43 Complex add(Complex c1, Complex c2) { 44 Complex c3(c1.real + c2.real, c1.imag + c2.imag); 45 return c3; 46 } 47 bool is_equal(Complex c1, Complex c2) { 48 return (c1.real == c2.real && c2.imag == c1.imag); 49 } 50 int abs(Complex c) { 51 return sqrt(c.real * c.real + c.imag * c.imag); 52 }
task4.cpp
1 #include "Complex.hpp" 2 #include <iostream> 3 #include <math.h> 4 // 类测试 5 void test() { 6 using namespace std; 7 8 Complex c1(3, -5); 9 const Complex c2(3.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. 实验任务5
User.hpp
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class User { 7 8 public: 9 User(string n, string p = "111111", string e = "") : 10 name{ n }, passwd{ p }, email{ e } { 11 num ++; 12 } 13 14 void set_email(); 15 void change_passwd(); 16 void print_info(); 17 static void print_n() { 18 cout << "there are " << num << " users."; 19 } 20 21 private: 22 string name, passwd, email; 23 static int num; 24 }; 25 26 int User::num = 0; 27 28 void User::set_email() { 29 cout << "Enter email address: " ; 30 string email_; 31 cin >> email_; 32 email = email_; 33 cout << "email is set successfully..." << endl; 34 } 35 36 void User::change_passwd() { 37 string old_pwd, new_pwd; 38 int count = 1; 39 40 cout << "Enter old password: "; 41 cin >> old_pwd; 42 43 while(old_pwd != passwd) { 44 cout << "password input error. Please re-enter again: "; 45 cin >> old_pwd; 46 count++; 47 48 if(count == 3) { 49 cout << "password input error. Please try after a while. " << endl; 50 return; 51 } 52 } 53 cout << "Enter new password: "; 54 cin >> new_pwd; 55 passwd = new_pwd; 56 57 cout << "new passwd is set successfully..." << endl; 58 59 } 60 61 void User::print_info() { 62 string pwd(passwd.length(), '*'); 63 cout << "name:\t" << name << endl; 64 cout << "passwd:\t" << pwd << endl; 65 cout << "email:\t" << email << endl; 66 }
task5.cpp
1 #include "User.hpp" 2 #include <iostream> 3 #include <string> 4 5 // 测试User类 6 void test() { 7 using std::cout; 8 using std::endl; 9 using std::string; 10 11 cout << "testing 1......\n"; 12 User user1("Jonny", "92197", "[email protected]"); 13 user1.print_info(); 14 15 cout << endl 16 << "testing 2......\n\n"; 17 18 User user2("Leonard"); 19 user2.change_passwd(); 20 user2.set_email(); 21 user2.print_info(); 22 23 cout << endl; 24 User::print_n(); 25 } 26 27 int main() { 28 test(); 29 }
实验总结 :
hpp文件中 ,不能忘了导入需要的头文件, using 所需要的命名空间中的成员
标签:const,cout,对象,void,Complex,实验,using,include From: https://www.cnblogs.com/youxiasaigao/p/16792104.html