- 实验任务三
source code in Complex.hpp :
1 #include <iostream> 2 3 using namespace std; 4 class Complex{ 5 public: 6 Complex(){ 7 real = 0; 8 imag = 0; 9 } 10 Complex(double c1): real(c1), imag(0) {}; 11 Complex(double c1,double c2): real(c1) , imag(c2) {}; 12 Complex(Complex& c3): real(c3.real), imag(c3.imag) {}; 13 double get_real() const; 14 double get_imag() const ; 15 void show() const ; 16 void add(const Complex& c1); 17 18 friend Complex add(const Complex& c1,const Complex& c2); 19 friend bool is_equal(const Complex& c1, const Complex& c2) ; 20 friend double abs(Complex& c1); 21 22 private: 23 double real; 24 double imag; 25 }; 26 27 double Complex::get_real () const{ 28 return real; 29 } 30 31 double Complex::get_imag () const{ 32 return imag; 33 } 34 35 void Complex::show() const{ 36 if(imag>0) 37 cout << real << "+ " << imag << "i"; 38 else if(imag==0) 39 cout<<real; 40 else cout<<real<<" - "<<(-imag)<<"i"; 41 } 42 43 void Complex::add(const Complex& c1){ 44 real += c1.real; 45 imag += c1.imag; 46 } 47 48 Complex add(const Complex& c1,const Complex& c2){ 49 Complex temp(0,0); 50 temp.real = c1.real + c2.real; 51 temp.imag = c1.imag + c2.imag; 52 return temp; 53 } 54 55 bool is_equal(const Complex& c1, const Complex& c2){ 56 return (c1.real == c2.real && c1.imag == c2.imag); 57 } 58 59 double abs(Complex& c1){ 60 return c1.real*c1.real + c1.imag*c1.imag; 61 }
The Running results :
- 实验任务四
source code in User.hpp :
1 #include <iostream> 2 #include <iomanip> 3 #include <string> 4 #include <cstring> 5 using namespace std; 6 class User 7 { 8 public: 9 User(string n); 10 User(string n,string p,string e); 11 ~User(){}; 12 void set_email(); 13 void change_passwd (); 14 void print_info(); 15 static void print_n(); 16 private: 17 string name; 18 string passwd; 19 string email; 20 static int num; 21 }; 22 int User::num=0; 23 User::User(string n):name(n),passwd("111111"),email(" "){ 24 ++num; 25 } 26 User::User(string n,string p,string e):name(n),passwd(p),email(e){ 27 ++num; 28 } 29 void User::set_email(){ 30 string input; 31 cout<<"Enter email address:"; 32 cin>>input; 33 string key_="@"; 34 if(input.find(key_)!=string::npos){ 35 email=input; 36 cout<<"email is set successfully..."<<endl; 37 }else 38 cout<<"Wrong format!"; 39 40 } 41 void User::change_passwd(){ 42 int times=3; 43 int change_pw=0; 44 cout<<"Enter old password:"; 45 for(int i=0;i<times;++i){ 46 string oldpsw; 47 cin>>oldpsw; 48 if(strcmp(oldpsw.c_str(),passwd.c_str())!=0){ 49 cout<<"password input error. Please re-enter again: "; 50 continue; 51 } 52 cout<<"Enter new password:"; 53 string newpsw; 54 cin>>newpsw; 55 if (newpsw.size()>=6){ 56 change_pw=1; 57 passwd=newpsw; 58 cout<<"new passwd is set successfully..."<<endl; 59 }else{ 60 cout << "passwd format error!\n"; 61 } 62 } 63 if(change_pw==0){ 64 cout<<"Please try after a while.\n"; 65 } 66 } 67 void User::print_info(){ 68 cout<<"name:\t"<<name<<endl; 69 cout<<"passwd:\t"<<"******"<<endl; 70 cout<<"email:\t"<<email<<endl; 71 } 72 void User::print_n(){ 73 74 if(num==1){ 75 cout<<"there is 1 user."<<endl; 76 } 77 else{ 78 cout<<"there are "<<num<<" users."<<endl; 79 } 80 }
The Running results :
- 实验总结
通过这次实验,掌握了类和对象的基础知识,也熟悉了String的使用。