实验一 类与对象

complex.hpp

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

task3.cpp

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

运行结果截图

实验一 类与对象

 User.hpp

#ifndef USER_HPP
#define USER_HPP
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
class User
{
public:
        User(string m,string x="111111",string p="\0"):name(m),passwd(x),email(p){n++;}
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n(){cout<<"there are "<<n<<" users." <<endl;}
        string name,passwd,email;
        static int n;
};
int User::n=0;
void User::set_email()
{
    
    
    if(email.length()==0) 
        {
            cout<<"Enter email address:";
            cin>>email;
            cout<<"email is set sucessfully..."<<endl;
        }
} 
void User::change_passwd()
{
    string x,a,b,c,m,n;
    cout<<"Enter old password:";
    cin>>x;
    if(x==passwd)
    {
        cout<<"Enter new passwd:";
        cin>>x;
        passwd=x; 
        cout<<"new passwd is set successfully..."<<endl;
    }
    else
    {
        cout<<"password input error. Please re-enter again:";
        cin>>b;
        if(b==passwd){cout<<"Enter new passwd:";
                        cin>>m;
                        passwd=m; 
                        cout<<"new passwd is set successfully..."<<endl;}
        else 
        {
            cout<<"password input error. Please re-enter again:";
            cin>>c;
            if(c==passwd){cout<<"Enter new passwd:";
                            cin>>n;
                            passwd=n; 
                            cout<<"new passwd is set successfully..."<<endl;}
            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;
}
#endif

task4.cpp

#include "User.hpp"
#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Marry", "454545", "qwe@hotmail.com");
    user1.print_info();
    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Jack");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}

运行结果截图

实验一 类与对象

 

实验一 类与对象

 

上一篇:实验一


下一篇:实验一 类与对象