实验1 类与对象

四、实验结论

实验任务3

complex.hpp文件源码

class complex
{
    public:
       complex(){}
       complex(double a):real(a),imag(0){}
       complex(double b,double c):real(b),imag(c){}
       complex(const complex &co):real(co.real),imag(co.imag){}
       ~complex()=default;
       double get_real() const{return real;}
       double get_imag() const{return imag;}
       void show() const;
       void add(const complex d);
       friend complex add(const complex c1,const complex c2);
       friend bool is_equal(const complex c3,const complex c4);
       friend double ab(const complex c5);
    private:
    double real;
    double imag;
};

void complex::show() const
{
    if(imag>0)
      std::cout<<real<<"+"<<imag<<"i";
    else if(imag==0)
      std::cout<<real;
    else std::cout<<real<<imag<<"i";
}
void complex::add(const complex p)
{
    real+=p.get_real();
    imag+=p.get_imag();
}
complex add(const complex p1,const complex p2)
{
    complex u;
    u.real=p1.get_real()+p2.get_imag();
    u.imag=p1.get_real()+p2.get_imag();
    return u;
}
bool is_equal(const complex c3,const complex c4)
{
    if(c3.get_real()==c4.get_real()&&c3.get_imag()==c4.get_imag())
       return true;
    else return false;
}
double ab(complex c5)
{
    return sqrt(c5.get_real()*c5.get_real()+c5.get_imag()*c5.get_imag());
}

task3.cpp源码

#include <iostream>
#include <math.h>
#include "complex.hpp"
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 << ab(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文件源码

 


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

 

task4.cpp源码

#include<iostream>
#include<string>
#include"User.hpp"
int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Lucy", "76523", "888666@qq.com");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Max");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}

运行测试结果截图

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-10-25 21:32  bjfbfelkbuif  阅读(41)  评论(1)    收藏  举报