实验二 类和对象

#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
    public:
        Complex(double a=0,double b=0):real{a},imag{b}{};
        Complex(const Complex&c1);
        ~Complex()=default;
        
        double get_real()const {return real;}
        double get_imag()const {return imag;}
        void show()const;
        void add(Complex const &c1);
        
        friend Complex add(Complex const&c1,Complex const&c2);
        friend bool is_equal(Complex const&c1,Complex const&c2);
        friend double abs(Complex const&c1);
    private:
        double real;
        double imag;
    
};

Complex::Complex(const Complex &c1)
{
    real=c1.real;
    imag=c1.imag;
}
void Complex::show()const
{
    if(imag==0)
    cout<<real;
    else if(imag<0)
    cout<<real<<"-"<<abs(imag)<<"i";
    else
    cout<<real<<"+"<<imag<<"i";
    
}
void Complex::add(Complex const&c1)
{
    real+=c1.real;
    imag+=c1.imag;
}
Complex add(Complex const&c1,Complex const&c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.imag=c1.imag+c2.imag;
    return c3;
}

bool is_equal(Complex const&c1,Complex const&c2)
{
    if(c1.real==c2.real&&c1.imag==c2.imag)
    return true;
    return false;
}
double abs(Complex const&c1)
{
    return sqrt(c1.real*c1.real+c1.imag*c1.imag);
}

 

 

#include<iostream>
#include<string>
using namespace std;

class User
{
    public:
        User(string name,string passwd="111111",string email=""):n{name},p{passwd},e{email}{count++;}
        ~User()=default;
        
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
        
    private:
        string n,p,e;
        static int count;
};
    int User::count=0;
    void User::set_email()
    {
        cout<<"Enter email adresss:";
        cin>>e;
        cout<<"email s set successfully..."<<endl;
    }
    
    void User::change_passwd()
    {
        cout<<"Enter old password:";
        string temp;
        int i=3;
        while(i)
        {
            cin>>temp;
            if(temp==p)
            {
                cout<<"new passwd is set successfully..."<<endl;
                break;
            }
            else
            {
                i--;
                if(i!=0)
                cout<<"password input error.Please re-enter again:";
            }
            if(i==0)
            {
                cout<<"password input error.Please try after a while."<<endl;
            }
        }
    }
    
    void User::print_info()
    {
        cout<<"name:"<<n<<endl;
        cout<<"passwd:"<<"******"<<endl;
        cout<<"email:"<<e<<endl;
    }
    void User::print_n()
    {
        cout<<"there are"<<count<<"users."<<endl;
    }

 

posted @ 2022-10-12 18:23  waitwaitw  阅读(31)  评论(0编辑  收藏  举报