实验一 类与对象

Complex.hpp文件源码

#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
public:
    Complex(double a=0,double b=0):real(a),imag(b){};
    Complex(const Complex &c1);
    double get_real() const {return real;}
    double get_imag() const {return imag;}
    void show()const;
    void add(const Complex  &c1);
    friend Complex add(const Complex &c1,const Complex &c2);
    friend bool is_equal(const Complex &c1,const Complex &c2);
    friend double abs(const Complex &c1);
private:
    double real;
    double imag;
};
Complex::Complex(const Complex &c1)
{
    real=c1.real;
    imag=c1.imag;
}
void Complex::show() const
{
    if(imag==0)
        cout << real;
    else if(imag<0)
        cout << real <<"-"<< abs(imag) <<"i";
    else
        cout << real <<"+"<< imag <<"i";
}
void Complex::add (const Complex &c1)
{
    real+=c1.real;
    imag+=c1.imag;
}
Complex add(const Complex &c1,const Complex &c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.imag=c1.imag+c2.imag;
    return c3;
}
bool is_equal(const Complex &c1,const Complex &c2)
{
    if(c1.real==c2.real&&c1.imag==c2.imag)
        return true;
    return false;
}
double abs(const Complex &c1)
{
    return sqrt(c1.real*c1.real+c1.imag*c1.imag);
}

task3.cpp源码

#include "Complex.hpp"
#include <iostream>
int main()
{
using namespace std;
Complex c1(10, -3);
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文件源码

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

task4.cpp源码

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

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Sou Long", "45263", "1485784293@qq.com");
    user1.print_info();
    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Monky Luffy");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
    return 0;
}

运行测试结果截图

 

posted @ 2021-10-25 21:41  无心+之举  阅读(34)  评论(3)    收藏  举报