实验2.4
hpp:
1 #pragma once 2 #include <iostream> 3 #include <cmath> 4 using namespace std; 5 class Complex{ 6 public: 7 Complex(double a=0,double b=0):real{a},imag{b}{}; 8 Complex(const Complex& p):real{p.real},imag{p.imag}{}; 9 void show()const; 10 double get_imag()const; 11 double get_real()const; 12 void add(const Complex& p); 13 friend bool is_equal(const Complex& a,const Complex& b); 14 friend double abs(const Complex& p); 15 friend Complex add(const Complex& a,const Complex& b); 16 private: 17 double real;double imag; 18 }; 19 void Complex::show()const{ 20 if(real==0&&imag==0){ 21 cout<<0; 22 } 23 else if(real==0&&imag!=0){ 24 cout<<imag<<"i"; 25 } 26 else if(real!=0&&imag==0){ 27 cout<<real; 28 } 29 else if(real!=0&&imag!=0){ 30 if(imag<0){ 31 cout<<real<<imag<<"i"; 32 } 33 else{ 34 cout<<real<<"+"<<imag<<"i"; 35 } 36 } 37 }; 38 39 40 double Complex::get_imag()const{ 41 return imag; 42 } 43 44 double Complex::get_real()const{ 45 return real; 46 } 47 48 double abs(const Complex& p){ 49 return sqrt(p.get_real()*p.get_real()+p.get_imag()*p.get_imag()); 50 } 51 52 Complex add(const Complex& a,const Complex& b) { 53 Complex c(a.get_real()+b.get_real(),a.get_imag()+b.get_imag()); 54 return c; 55 } 56 57 void Complex::add(const Complex& p){ 58 real+=p.get_real(); 59 imag+=p.get_imag(); 60 } 61 62 bool is_equal(const Complex& a,const Complex& b){ 63 bool i; 64 if(a.get_real()==b.get_real()&&a.get_imag()==b.get_imag()){ 65 i=1; 66 } 67 else{ 68 i=0; 69 } 70 return i; 71 }
cpp:
1 #include "Complex.hpp" 2 #include <iostream> 3 void test(){ 4 using namespace std; 5 6 Complex c1(2,-4); 7 const Complex c2(1.5); 8 Complex c3(c1); 9 10 cout<<"c1= "; 11 c1.show(); 12 cout<<endl; 13 14 cout<<"c2= "; 15 c2.show(); 16 cout<<endl; 17 cout<<"c2.imag= "<<c2.get_imag()<<endl; 18 19 cout<<"c3= "; 20 c3.show(); 21 cout<<endl; 22 23 cout<<"abs(c1)= "; 24 cout<<abs(c1)<<endl; 25 26 cout<<boolalpha; 27 cout<<"c1==c3:" <<is_equal(c1,c3)<<endl; 28 cout<<"c1==c2:" <<is_equal(c1,c2)<<endl; 29 30 Complex c4; 31 c4=add(c1,c2); 32 cout<<"c4=c1+c2= "; 33 c4.show(); 34 cout<<endl; 35 36 c1.add(c2); 37 cout<<"c1+=c2,"<<"c1= "; 38 c1.show(); 39 cout<<endl; 40 } 41 int main(){ 42 test(); 43 }
实验2.5
hpp:
1 #pragma once 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 class User{ 6 public: 7 User(string a="Mary",string b="111111",string c=""):name{a},passwd{b},email{c}{count++;}; 8 void print_info(); 9 void change_passwd(); 10 void set_email(); 11 static void print_n(); 12 private: 13 string name,passwd,email;static int count; 14 }; 15 16 void User::print_info(){ 17 string s1(passwd.size(),'*'); 18 cout<<"name: "<<name<<endl; 19 cout<<"passwd: "<<s1<<endl; 20 cout<<"email: "<<email<<endl; 21 } 22 23 int User::count=0; 24 25 void User::print_n(){ 26 cout<<"There are "<<count<<" users."<<endl; 27 } 28 29 void User::change_passwd(){ 30 cout<<"Enter old password: "; 31 string s1; 32 int i=1; 33 cin>>s1; 34 for(i=1;i<=2;i++){ 35 if(s1==passwd){ 36 cout<<"Enter new passwd: "; 37 cin>>passwd; 38 break; 39 } 40 else{ 41 cout<<"password input error.Please re-enter again:"; 42 cin>>s1; 43 } 44 } 45 if(i==3){ 46 cout<<"password input error.Please try after a while."<<endl; 47 } 48 } 49 50 void User::set_email(){ 51 cout<<"Enter email address: "; 52 cin>>email; 53 cout<<"email is set successfully..."<<endl;; 54 }
cpp:
1 #include "User.hpp" 2 #include <iostream> 3 void test(){ 4 using std::cout; 5 using std::endl; 6 7 cout<<"testing 1......\n"; 8 User user1("jonny","92197","[email protected]"); 9 user1.print_info(); 10 11 cout<<endl; 12 cout<<"testing 2......\n\n"; 13 14 User user2("Leonard"); 15 user2.change_passwd(); 16 user2.set_email(); 17 user2.print_info(); 18 19 cout<<endl; 20 User::print_n(); 21 } 22 int main(){ 23 test(); 24 }
标签:const,cout,double,void,Complex,实验,include From: https://www.cnblogs.com/gzj035/p/16796185.html