实验一
#include<iostream> #include<math.h> using namespace std; class Complex { public: Complex(double r=0,const double i=0) { real=r; imag=i; } Complex(const Complex &p); double get_real(); double get_imag() const; void show() const; void add(const Complex &c1); friend Complex add(const Complex &c2,const Complex &c3); friend bool is_equal(const Complex &c4,const Complex &c5); friend double abs(const Complex c6); private: double real,imag; }; Complex::Complex(const Complex &p) { real=p.real; imag=p.imag; } double Complex::get_real() { return real; } double Complex::get_imag ()const { return imag; } void Complex::show() const { if(imag>0) cout<<real<<"+"<<imag<<"i"<<endl; else if(imag==0) cout<<real<<endl; else cout<<real<<imag<<"i"<<endl; } void Complex::add(const Complex &c1) { real+=c1.real; imag+=c1.imag; } Complex add(const Complex &c2,const Complex &c3) { Complex c; c.real=c2.real+c3.real; c.imag=c2.imag+c3.imag; return c; } bool is_equal(const Complex &c4,const Complex &c5) { if(c4.real==c5.real&&c4.imag==c5.imag) return true; else return false; } double abs(Complex c6) { double a=sqrt((c6.real*c6.real)+(c6.imag*c6.imag)); return a; } int main() { using namespace std; Complex c1(-9,5); const Complex c2(4); 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; }


#include <iostream> #include <string> using namespace std; class User { public: User(string na="Mary",string p="111111",string e="") { n++; name=na; passwd=p; email=e; } User() { n++; name="John"; passwd="112233"; email="xyz@gmail.com"; } void set_email(); void change_passwd(); void print_info(); static void print_n() { cout<<"there are "<<n<<" users."<<endl; } private: string name; string passwd; string email; static int n; }; int User::n=0; void User::set_email() { string w; cout<<"Enter email address:"; cin>>w;email=w; cout<<"email is set successfully"<<endl; } void User::change_passwd() { string p1,p2,p3,p4,p5; cout<<"Enter old password: "; cin>>p1; if(p1==passwd) { cout<<"Enter new password: "; cin>>p2; } else { cout<<"password input error. Please re-enter again: "; cin>>p2; if(p2==passwd) { cout<<"Enter new password: "; cin>>p3; } else cout<<"password input error. Please re-enter again: "; cin>>p3; if(p3==passwd) { cout<<"Enter new password: "; cin>>p4; } 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; } int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "xyz@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(); }




浙公网安备 33010602011771号