實驗1 類與物件
阿新 • • 發佈:2021-10-20
task3
Complex.hpp原始碼:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include<iostream> 5 #include<cmath> 6 7 using namespace std; 8 9 class Complex { 10 private: 11 double real; 12 double imag; 13 public: 14 Complex(); 15 Complex(doublereal0); 16 Complex(double real0,double imag0); 17 Complex(const Complex &obj); 18 19 double get_real()const; 20 double get_imag()const; 21 void show()const; 22 void add(const Complex &obj); 23 24 friend Complex add(Complex obj1,Complex obj2);25 friend bool is_equal(const Complex &obj1,const Complex &obj2); 26 friend double abs(const Complex &obj); 27 }; 28 29 Complex::Complex() { 30 real=imag=0; 31 } 32 Complex::Complex(double real0) { 33 real=real0; 34 imag=0; 35 } 36 Complex::Complex(double real0,doubleimag0) { 37 real=real0; 38 imag=imag0; 39 } 40 Complex::Complex(const Complex &obj) { 41 real=obj.real; 42 imag=obj.imag; 43 } 44 45 double Complex::get_real()const { 46 return real; 47 } 48 double Complex::get_imag()const { 49 return imag; 50 } 51 void Complex::show()const { 52 if(imag!=0) 53 cout<<real<<imag<<"i"; 54 else 55 cout<<real; 56 } 57 void Complex::add(const Complex &obj) { 58 real=real+obj.real; 59 imag=imag+obj.imag; 60 } 61 62 Complex add(Complex obj1,Complex obj2) { 63 obj1.real=obj1.real+obj2.real; 64 obj1.imag=obj1.imag+obj2.imag; 65 return obj1; 66 } 67 bool is_equal(const Complex &obj1,const Complex &obj2) { 68 if(obj1.real==obj2.real&&obj1.imag==obj2.imag) return true; 69 else return false; 70 } 71 double abs(const Complex &obj) { 72 return sqrt(pow(obj.real,2)+pow(obj.imag,2)); 73 } 74 75 #endif
Complex.cpp原始碼:
1 #include"Complex.hpp" 2 #include<iostream> 3 int main() { 4 using namespace std; 5 6 Complex c1(10, -1); 7 const Complex c2(15.2); 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 }
測試結果:
task4
User.hpp原始碼:
1 #ifndef USER_HPP 2 #define USER_HPP 3 4 #include<iostream> 5 #include<string> 6 7 using namespace std; 8 9 class User { 10 private: 11 string name; 12 string passwd; 13 string email; 14 static int n; 15 public: 16 User(string name0); 17 User(string name0,string passwd0,string email0); 18 void set_email(); 19 void change_passwd(); 20 void print_info(); 21 static void print_n(); 22 }; 23 24 int User::n=0; 25 26 User::User(string name0) { 27 name=name0; 28 passwd="111111"; 29 email=""; 30 n++; 31 } 32 User::User(string name0,string passwd0,string email0) { 33 name=name0; 34 passwd=passwd0; 35 email=email0; 36 n++; 37 } 38 39 void User::set_email() { 40 cout<<"Enter email address: "; 41 cin>>email; 42 cout<<"email is set successfully..."<<endl; 43 } 44 void User::change_passwd() { 45 cout<<"Enter old password: "; 46 string s; 47 cin>>s; 48 int i; 49 for(i=1; i<=2; i++) { 50 if(s==passwd) break; 51 cout<<"password input error.Please re-enter again: "; 52 cin>>s; 53 } 54 if(s!=passwd) cout<<"password input error. Please try after a while."<<endl; 55 else { 56 cout<<"Enter new passwd: "; 57 cin>>s; 58 passwd=s; 59 cout<<"new passwd is set successfully..."<<endl; 60 } 61 } 62 63 void User::print_info() { 64 cout<<"name: "<<name<<endl; 65 cout<<"passwd: "<<"******"<<endl; 66 cout<<"email: "<<email<<endl; 67 } 68 69 void User::print_n() { 70 cout<<"there are "<<n<<" users."<<endl; 71 } 72 73 #endif
User.cpp原始碼:
1 #include "User.hpp" 2 #include <iostream> 3 4 int main() { 5 using namespace std; 6 7 cout << "testing 1......" << endl; 8 User user1("馬化騰", "10001", "[email protected]"); 9 user1.print_info(); 10 11 cout << endl 12 << "testing 2......" << endl 13 << endl; 14 User user2("馬雲"); 15 user2.change_passwd(); 16 user2.set_email(); 17 user2.print_info(); 18 19 User::print_n(); 20 }
測試結果: