实验1 类与对象

# ifndef COMPLEX_H
#define COMPLEX_H

# include <iostream>
# include <cmath>
 class Complex{
    public:
        Complex(double a=0,double b=0): real(a),imag(b) {}
        Complex(const Complex &C);
        double get_real()const{
            return real;
        }
        double get_imag()const{
            return imag;
        }
        void show()const{
            std::cout<<real;
            if(imag>0)
            std::cout<<"+"<<imag<<"i"<<std::endl;
            else if(imag<0)
            std::cout<<imag<<"i"<<std::endl;
            
        }
        void add(const Complex &C1)
        {
            real+=C1.get_real();
            imag+=C1.get_imag();
        }
        friend Complex add(const Complex &c2,const Complex &c3);
        friend bool is_equal(const Complex &c4,const Complex &c5);
        friend double abs(Complex &C2);
    private:
        double real,imag;
};
Complex add(const Complex &c2,const Complex &c3)
{
    Complex C;
    C.real=c2.real+c3.real;
    C.imag=c2.imag+c3.imag;
    return C;
}
bool is_equal(const Complex &c4,const Complex &c5) 
{
    if(c4.real==c5.real&&c4.imag==c5.imag)
    {
        return true;
    }
    else
    {
        return false;
    }
}
double abs(Complex &C2)
{
    double x=C2.real;
    double y=C2.imag;
    return sqrt(x*x+y*y);
}
Complex::Complex(const Complex &C){
    real=C.real;
    imag=C.imag;
}
#endif
#include "Complex.hpp"
#include <iostream>

int main()
{
    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;
}

 

 

#ifndef USER_HPP
#define USER_HPP

#include <iostream>
#include <string>
using namespace std;
class User {
public:
    User(string na, string p = "111111", string e = "") : name(na), passwd(p), email(e) { n++; }
    void set_email(){
        string h;
        cout << "Enter email address: ";
        cin >> h;
        email = h;
        cout << "email is set successfully..." << endl;
    }
    void change_passwd(){
        string c;
        string h;
        int x=3;
        cout << "Enter old password: ";
        cin >> c;
        if (c == passwd)
        {
            cout << "Enter new password: ";
            cin >> h;
            passwd = h;
            cout << "new password is set successfully... " << endl;
        }
        else {
            x--;
            while (x != 0)
            {
                cout << "password input error. Please re-enter again: ";
                cin >> c;
                if (c == passwd)
                    break;
                else
                    x--;
            }
            if (x != 0)
            {
                cout << "Enter new password: ";
                cin >> h;
                passwd = h;
                cout << "new password is set successfully... " << endl;
            }
            else
            {
                cout << "password input error. Please try after a while." << endl;
            }

        }
        

    }
    void print_info(){
        cout << "name:   " << name <<endl;
        cout << "passwd: " << "******" <<endl;
        cout << "email:  " << email <<endl;
    }
    static void print_n() {
        cout << "there are " << n << " users. " << endl;
    }
private:
    string name;
    string passwd;
    string email;
    static int n;

};
int User::n = 0;

#endif
#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();
}

 更变数据:

 

posted @ 2021-10-22 11:14  wjsx  阅读(60)  评论(3)    收藏  举报