实验一 类与对象

Complex.hpp

#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
    public:
        Complex(double r=0.0,double m=0.0):real(r),imag(m) {};
        Complex(Complex &c):real(c.real),imag(c.imag) {};
        ~Complex () {};
        double get_real() const
        {
            return real;
        }
        double get_imag() const
        {
            return imag;
        }
        void show() const
        {
            cout<<real;
            if(imag>0)
            {
                cout<<'+'<<imag<<'i';
            }
            if(imag<0)
            {
                cout<<imag<<'i';
            }
        }
        void add(Complex const &c)
        {
            real+=c.real;
            imag+=c.imag;
        }
        friend Complex add(Complex const &c1,Complex const &c2);
        friend bool is_equal(Complex const &c1,Complex const &c2);
        friend double abs(Complex const &c);
    private:
        double real,imag;    
};
Complex add(Complex const &c1,Complex const &c2)
{
    Complex r;
    r.real=c1.real+c2.real;
    r.imag=c1.imag+c2.imag;
    return r;
}
bool is_equal(Complex const &c1,Complex const &c2)
{
    if(c1.real==c2.real&&c1.imag==c2.imag)
    {
        return true;
    }
    else
    {
        return false;
    }
}
double abs(Complex const &c)
{
    return sqrt(c.real*c.real+c.imag*c.imag);
}
#endif

 

Complex.cpp

#include"Complex.hpp"
#include<iostream>
int main()
{
    using namespace std;
    Complex c1(6,-8);
    const Complex c2(9);
    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;
}

 

 

 
User.hpp
#ifndef USER_HPP
#define USER_HPP
#include<iostream>
#include<string>
using namespace std;
class User
{
    public:
        User(string a,string b,string c):name(a),password(b),email(c)
        {
            num++;
        }
        User(string a):name(a),password("111111"),email("")
        {
            num++;
        }
        User(User &u):name(u.name),password(u.password),email(u.email)
        {
            num++;
        }
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
         ~User() {};
    private:
        string name;
        string password;
        string email;
        static int num;
};
int User::num=0;
void User::set_email()
{
    cout<<"Enter email address:";
    cin>>email;
    cout<<"email is set successfully..."<<endl;
}
void User::change_passwd()
{
    string p,np;
    int i=1;
    cout<<"Enter old password:";
    cin>>p;
    while(p!=password)
    {
        cout<<"password input error.Please re-enter again:";
        cin>>p;
        i++;
        if(i>=3)
        {
            cout<<"password input error.Please try after a while."<<endl;
            break;
        }
    }
    if(p==password)
    {
        cout<<"Enter new password: ";
        cin>>np;
        password=np;
        cout<<"new passwd is set sucessfully..."<<endl;
    }
}
void User::print_info() 
{
    cout<<"name:"<<name<<endl;
    cout<<"passwd:"<<"******"<<endl;
    cout<<"email:"<<email<<endl;
}
void User::print_n()
{
    if(num==1)
    {
        cout<<"there is 1 user."<<endl;
    }
    else
    {
        cout<<"there are "<<num<<" users."<<endl;
    }
}
#endif

 

User.cpp

#include"User.hpp"
#include<iostream>
int main()
{
    using namespace std;
    cout<<"testing 1......"<<endl;
    User user1("Jack","123456","Jack@qq.com");
    user1.print_info();
    cout <<endl
         <<"testing 2......"<<endl
         <<endl;
    User user2("Mary");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
}

 

 

 

总结:

通过这次实验,发现自己仍存在着众多的不足之处,比如编写代码的速度有待提升以及在编写代码的时候总会丢失一些细节的东西,例如const,&等。

编程真的是一段不断尝试,不断进步的过程,只有多尝试,多练习,才能做到更好。

posted @ 2021-10-25 21:21  过路游人XYZ  阅读(40)  评论(4)    收藏  举报