Complex.hpp
#include<iostream> #include<cmath> using namespace std; class Complex{ private: double a,b; public: Complex() :a{0},b{0} { } Complex(double x) { a=x; b=0; } Complex(double y,double z) { a=y;b=z; } Complex(const Complex &obj) { a=obj.a; b=obj.b; } double get_real() const { return a; }; double get_imag() const { return b; }; void show() const; void add(const Complex &T); friend Complex add(const Complex &A,const Complex &B); friend bool is_equal(const Complex &A,const Complex &B); friend double abs(Complex &A); void add(Complex &A,Complex &B); }; void Complex::show() const { if(b>0) cout << a <<" + "<< b << "i" ; else if(b==0) cout<< a ; else if(b<0) { cout << a <<" - "<< -b << "i" ; } } void Complex::add(const Complex &T) { a+=T.a; b+=T.b; } Complex add(const Complex &A,const Complex &B) { Complex C; C.a=A.a+B.a; C.b=A.b+B.b; return C; } bool is_equal(const Complex &A,const Complex &B) { return (A.a==B.a)&&(A.b==B.b); } double abs(Complex &A) { return sqrt(pow(A.a,2)+pow(A.b,2)); }
task4
#include "Complex.hpp" #include <iostream> // 类测试 void test() { 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; } int main() { test(); }
运行结果
Use.hpp
#include<iostream> #include<string> using namespace std; class User{ private: string name,passwd,email; static int k; public: User(string NAME,string PASSWD="111111",string EMAIL=""):name{NAME}, passwd{PASSWD} ,email{EMAIL} {k++;}; void set_email(); void change_passwd(); void print_info(); static void print_n(); }; int User::k=0; void User::set_email() { cout<<"Enter email address:"; string new_email; cin>>new_email; cout<<endl; email=new_email; cout<<"email is set successfully. . ."<<endl; } void User::change_passwd() { string old_password,new_password; int n=1; while(n){ cout<<"Enter old password:"; cin>>old_password; cout<<endl; if(old_password!=passwd) { if(n%3==0){ cout<<"password input error. Please try after a while."<<endl; break; } else cout<<"password input error. Please re-enter again:"; } else if(old_password==passwd) { cout<<"Enter new passwd:"; cin>>new_password; cout<<endl; cout<<"new password is set successfully. . ."<<endl; break; } ++n; } } void User::print_info() { cout<<"name: "<<name<<endl; cout<<"passwd: *****"<<endl; cout<<"email: "<<email<<endl; } void User::print_n() { cout<<"therer are "<<User::k<<" users."<<endl; }
task5
#include "User.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
运行结果
标签:const,cout,double,void,Complex,实验,include From: https://www.cnblogs.com/oRIng/p/16792063.html