//#ifndef Complex.hpp //#define Complex.hpp #include<iostream> #include <cmath> using namespace std; class Complex{ private: double real; double imag; public: Complex(); Complex(double r); Complex (double r,double l); Complex(Complex &p); void show() const; void add(const Complex &p); double get_real() const{return real;} double get_imag() const{return imag;} friend Complex add(const Complex &p1,const Complex &p2); friend bool is_equal(const Complex &p1,const Complex &p2); friend double abs(Complex p); }; void Complex::show() const { if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag==0) cout<<real; else cout<<real<<imag<<"i"; } Complex::Complex() { real=0; imag=0; } Complex::Complex(double r) { real=r; imag=0; } Complex::Complex(double r,double i) { real=r; imag=i; } Complex::Complex(Complex &p) { real=p.real; imag=p.imag; } void Complex::add(const Complex &p) { real+=p.real; imag+=p.imag; } Complex add(const Complex &p1,const Complex &p2){ Complex p; p.imag = p1.imag + p2.imag; p.real = p1.real + p2.real; return p; } bool is_equal(const Complex &p1,const Complex &p2) { if ((p1.real==p2.real)&&(p1.imag==p2.imag)) return true; else return false; } double abs(Complex p){ return sqrt(p.real*p.real+(p.imag*p.imag)); } //#endif #include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(3, -4); const Complex c2(4.5); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; }
//#ifndef User.h //#define User.h #include<iostream> #include<string> using namespace std; class User { private: string name,password,email; static int n; public: User(string x) {name=x; password="111111"; email='\0'; n++;} User(string x,string y,string z) {name=x; password=y; email=z; n++;} void set_email(); void change_passwd(); void print_info(); static void print_n(); }; int User::n=0; void User::print_n() { cout<<"there are "<<n<<"users"; } void User::set_email() { cout<<"Enter email address:"; string x; cin>>x; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { cout<<"Enter old password: "; string A,B; for(int i=1;i<=3;i++) { cin>>A; if(A==password) { cout<<"Enter new password: "; cin>>B; cout<<"new password is set successfully..."<<endl; } else if(i<=2&&A!=password) { cout<<"password input error. Please re-enter again: "; } else cout<<"Password input error.Please try after a while."<<endl; } } void User::print_info() { cout<<"name: "<<name<<endl; cout<<"passwd: ******"<<endl; cout<<"email: " <<email<<endl; } //#endif #include "User.h" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Polaris", "010314", "sct@hotmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }