实验一 类和对象

实验结论

实验任务三:

Comple.hpp 源码:

using namespace std;
#include<iostream>
class Complex {
public:
    Complex(double r = 3, double i = 0) :real(r), imag(i) {};
    Complex(Complex& c) {
        real = c.real;
        imag = c.imag;

    }
    ~Complex() = default;
    double get_real ()const ;
    double get_imag ()const;
    void show () const;
    void add(const Complex &c);
    friend Complex add(const Complex &c1,const Complex &c2);
    friend bool is_equal(const Complex &c1,const Complex &c2);
    friend double  abs(Complex const &c);
private:
    double real, imag;
};
double Complex::get_real() const  {
    return real;
}
double Complex::get_imag() const  {
    return imag;
}
void Complex::show() const{
    std::cout << real;
    if(imag!=0)
        if(imag<0)
        cout<<" - " << fabs(imag) << 'i';
        else
        cout << " + " << imag << 'i';
}
void Complex::add(const Complex &c) {
    real += c.real;
    imag += c.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;
    else
        return false;
}
double abs(const Complex &c) {
    return sqrt(c.real * c.real + c.imag * c.imag);
}

 

Task3.cpp 源码:

#include <iostream>
#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 << 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 <string>
#include <iostream>
#include <unistd.h>
using namespace std;

class User{
public:
    User(string Name,string passwd="111111",string email=""):name(Name),passwd(passwd),email(email){
        count++;
    };
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();

private:
    string name;
    string passwd;
    string email;
    static int count ;
};
int User::count=0;
void User::set_email() {
    cout<<"enter email address :";
    cin>>email;
    cout<<"email is set successfully..."<<endl;

}
void User::change_passwd() {
    string oldPasswd;
    string newPasswd;
    int errorTime = 0;
    cout << "Enter old password: ";
    cin >> oldPasswd;
    while (oldPasswd != passwd) {
        errorTime++;
        cout << "password is error . Please re-enter again :";
        cin >> oldPasswd;
        if (errorTime == 2) {
            cout<<"password is error . Please try after a while "<<endl;
            return;
        }
    }
    cout << "Enter new password :";
    cin >> newPasswd;
    passwd = newPasswd;
    cout << "new password is set successfully..."<<endl;
}
void User::print_info() {
    cout<<"name:     "<<name<<endl;
    cout<<"password: "<<"******"<<endl;
    cout<<"email:    "<<email<<endl;
}
void User::print_n() {
    cout<<"there are "<<count<<" user";
}

 

Task.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() ;
}

 

实验截图:

实验一 类和对象

 

 实验一 类和对象

 

 

 

上一篇:实验一 类与对象


下一篇:实验一