#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(int r=0,int i=0):real((double)r),imag((double)i){} Complex(double r); Complex(double r, double i); ~Complex()=default; Complex(const Complex &a)//复制构造函数 { real=a.real; imag=a.imag; } double get_real() const//返回函数实部 { return real; } double get_imag() const//返回函数虚部 { return imag; } void show() const//输出复数 { if (get_imag()>0) cout<<get_real()<<"+"<<get_imag()<<"i"<<endl; else if (get_imag()==0) cout<<get_real()<<endl; else cout<<get_real()<<"-"<<-get_imag()<<"i"<<endl; } void add(const Complex &a)//用于把一个复数加到自身 { real+=a.real; imag+=a.imag; } friend Complex add(const Complex &a1, const Complex &a2);//友元函数:实现两个复数相加 friend bool is_equal(const Complex &a1, const Complex &a2);//友元函数,判断相等 friend double abs(const Complex &a);//友元函数,对复数进行取模运算 private: double real; double imag; }; Complex::Complex(double r) { real=r; imag=0; } Complex::Complex(double r, double i) { real=r; imag=i; } Complex add(const Complex &a1, const Complex &a2) { Complex a3; a3.real=a1.real+a2.real; a3.imag=a1.imag+a2.imag; return a3; } bool is_equal(const Complex &a1, const Complex &a2) { if ((a1.real==a2.real)&&(a1.imag==a2.imag)) return true; else return false; } double abs(const Complex &a) { double sum; sum=sqrt(a.real*a.real+a.imag*a.imag); return sum; }
#include"Complex.hpp" #include<iostream> int main() { using namespace std; Complex c1(7,-1); const Complex c2(-2,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<<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 { private: string name; string password; string email; static int n; public: User(string name1); User(string nameq,string password1,string email1); ~User()=default; void set_email() { string h; cout<<"Enter email address:"; cin>>h; email=h; cout<<"email is set successfully"<<endl; } void change_passwd() { string p,newp; cout<<"Enter old password:"; int t=0; for(int i=1;i<=3;i++) { cin>>p; if(p!=password) { t++; if(t==3) { cout<<"password input error.Please try after a while."<<endl; break; } else cout<<"password input error. Please re-enter again:"; } else { cout<<"Enter new password:"; cin>>p; cout<<"new password is set successfully..."<<endl; break; } } } void print_info() { cout<<"name:"<<name<<endl; cout<<"password:******"<<endl; cout<<"email:"<<email<<endl; } static void print_n() { cout<<"there are "<<n<<" users."<<endl; } }; int User::n=0; User::User(string name1) { name=name1; password="111111"; email=" "; n++; } User::User(string name1,string password1,string email1) { name=name1; password=password1; email=email1; n++; }
#include "User.hpp" #include <iostream> 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(); }