1.实验四 Complex类的实现
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 class Complex 5 { 6 private: 7 double real; 8 double imag; 9 public: 10 Complex(double r=0.0,double i=0.0) 11 { 12 real=r; 13 imag=i; 14 } 15 Complex(Complex &c) 16 { 17 real=c.real; 18 imag=c.imag; 19 } 20 double get_real() const 21 { 22 return real; 23 } 24 double get_imag() const 25 { 26 return imag; 27 } 28 void show() const 29 { 30 if(imag>0) 31 cout<<real<<"+"<<imag<<"i"<<endl; 32 else if(imag==0) 33 cout<<real<<endl; 34 else 35 cout<<real<<imag<<"i"<<endl; 36 } 37 void add(const Complex &c1) 38 { 39 real=real+c1.real; 40 imag=imag+c1.imag; 41 } 42 friend Complex add(const Complex &c2,const Complex &c3) 43 { 44 Complex c4; 45 c4.real=c2.real+c3.real; 46 c4.imag=c2.imag+c3.imag; 47 return c4; 48 } 49 friend bool is_equal(const Complex &c5,const Complex &c6) 50 { 51 if(c5.real==c6.real&&c5.imag==c6.imag) 52 return true; 53 else 54 return false; 55 } 56 friend double abs(Complex c7) 57 { 58 double model; 59 model=sqrt(c7.imag*c7.imag+c7.real*c7.real); 60 return model; 61 } 62 };
运行结果:
2. 实验五 User类实现
1 #include<iostream> 2 #include<cmath> 3 #include<string> 4 using namespace std; 5 class User 6 { 7 private: 8 string name; 9 string password; 10 string email; 11 static int count; 12 13 public: 14 15 User(string n,string p={"111111"},string e={" "}):name{n},password{p},email{e} 16 { 17 count++; 18 } 19 ~User() 20 { 21 count--; 22 } 23 void set_email() 24 { 25 cout<<"Enter email address: "; 26 cin>>email; 27 cout<<"email is set successfully"<<endl; 28 } 29 void change_password() 30 { 31 string oldpassword; 32 int i=1; 33 cout<<"Enter old password: "; 34 cin>>oldpassword; 35 while(i) 36 { 37 if(oldpassword.compare(password)!=0) 38 { 39 i++; 40 if(i>3) 41 { 42 cout<<"please try after a while"<<endl; 43 break; 44 } 45 cout<<"password input error. Please re-enter again: "; 46 cin>>oldpassword; 47 continue; 48 49 } 50 else 51 { 52 string newpassword; 53 cout<<"Enter new password: "; 54 cin>>newpassword; 55 cout<<"password is set successfully"<<endl; 56 password=newpassword; 57 break; 58 } 59 } 60 } 61 void print_info() 62 { 63 cout<<"name: "<<name<<endl; 64 string secret=password; 65 int i; 66 cout<<"password: "; 67 for(i=1;i<=secret.length();i++) 68 cout<<"*"; 69 cout<<endl; 70 cout<<"email: "<<email<<endl; 71 } 72 static int get_count() 73 { 74 cout<<count<<endl; 75 return count; 76 } 77 }; 78 int User::count=0;
运行结果:
logo 标签:real,string,对象,double,imag,Complex,实验,cout From: https://www.cnblogs.com/lc114514/p/16785151.html