实验一 类与对象

任务3:复数类complex

 

  .hpp文件源码

#ifndef COMPLEX_HPP
#define COMPLEX_HPP


#include<iostream>
#include<math.h>
using namespace std;

class Complex
{
private:
    double real;
    double imag;
public:
    Complex();
    Complex(double a);
    Complex(double x,double y);
    Complex(Complex &c);

    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(const Complex &c1);
    
    friend Complex add(const Complex &c2,const Complex &c3);
    friend bool is_equal(const Complex &c5,const Complex &c6);
    friend double abs(const Complex &c7);
};

Complex::Complex():real{0},imag{0}{}
Complex::Complex(double a):real{a},imag{0}{}
Complex::Complex(double x,double y):real{x},imag{y}{}
Complex::Complex(Complex &c){real=c.real;imag=c.imag;}

double Complex::get_real() const
{
    return real;
}

double Complex::get_imag() const
{
    return imag;
}

void Complex::show() const
{
    if(imag==0) cout<<real;
    else if(imag<0)(cout<<real<<imag<<"i");
    else cout<<real<<"+"<<imag<<"i";
}

void Complex::add(const Complex &c1)
{
    real+=c1.real;
    imag+=c1.imag;
}

Complex add(const Complex &c2,const Complex &c3)
{
    Complex c4(0,0);
    c4.real=c2.real+c3.real;
    c4.imag=c2.imag+c3.imag;
    return c4;
}

bool is_equal(const Complex &c5,const Complex &c6)
{
    if(c5.real==c6.real&&c5.imag==c6.imag) return true;
    else return false;
}

double abs(const Complex &c7)
{
    double n;
    n=sqrt(c7.real*c7.real+c7.imag*c7.imag);
    return n;
}

#endif

  .cpp源码

#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;
}

 运行结果:

 

任务4:用户类user

 

  .hpp文件源码

#ifndef USER_HPP
#define USER_HPP

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

class User
{
private:
    string name;
    string passwd;
    string email;
    static int n;
public:
    User(string a);
    User(string na,string p,string e);
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
};

int User::n=0;

User::User(string a):name{a},passwd{"111111"},email{}
{
    ++n;
}

User::User(string na,string p,string e):name{na},passwd{p},email{e}
{
    ++n;
}

void User::set_email()
{
    cout<<"Enter email address: ";
    cin>>email;
    cout<<"email is set successfully..."<<endl;
}

void User::change_passwd()
{
    cout<<"Enter old password: ";
    string a,b;
    cin>>a;
    int i=1,flag=0;
    if(a==passwd)
    {
        cout<<"Enter new password: ";
        cin>>b;
        passwd=b;
        cout<<"new passwd is set successfully..."<<endl;
    }
    else
    {
        while(i<3)
        {
            cout<<"password input error.Please re-enter again: ";
            cin>>a;
            ++i;
            if(a==passwd)
            {
            cout<<"Enter new password: ";
            cin>>b;
            passwd=b;
            cout<<"new passwd is set successfully..."<<endl;
            flag=1;
            break;
            }
        }
        if(flag==0)
        {
            cout<<"psaaword input error.Please try after a while."<<endl;
        }
    }
}

void User::print_info()
{
    cout<<"name:   "<<name<<endl;
    cout<<"passwd: "<<"******"<<endl;
    cout<<"email:  "<<email<<endl;
}

void User::print_n()
{
    cout<<"there are "<<n<<" users.";
}

#endif 

  .cpp源码

#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-26 22:44  markpakka  阅读(22)  评论(3)    收藏  举报