首页 > 其他分享 >实验2 类和对象(2)

实验2 类和对象(2)

时间:2022-10-17 13:23:53浏览次数:44  
标签:const cout 对象 double void Complex 实验 include

实验4

complex.hpp

#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
public:
    Complex(double x0, double y);
    Complex(const Complex& obj) : x{ obj.x }, y{ obj.y } {}
    double get_real()const { return x; }
    double get_imag()const { return y; }
    void show()const;
    void add(Complex obj);
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs (Complex obj);
    double x, y;
};
Complex::Complex(double x0 = 0.0, double y0 = 0.0)
{
    x = x0;
    y = y0;
}
void Complex::show() const {
    if (y > 0)
    {
        cout << x << "+" << y << "i";
    }
    else if (y < 0)
    {
        cout << x << "-" << abs(y) << "i";
    }
    else
    {
        cout << x;
    }
}
void Complex::add(Complex obj) {
    x += obj.x;
    y += obj.y;
}
Complex add(Complex c1, Complex c2) {
    Complex c3;
    c3.x = c1.x  + c2.x;
    c3.y = c1.y + c2.y;
    return c3;
}
bool is_equal(Complex c1, Complex c2)
{
    if (c1.x == c2.x && c1.y == c2.y)
        return true;
    else
        return false;
}
double abs( Complex obj )
{
    double ab;
    ab = sqrt(obj.x * obj.x + obj.y * obj.y);
    return ab;
}

task4.cpp

 
#include "Complex.hpp"
#include <iostream>
void test() {
    using namespace std;
    Complex c1(1, -5);
    const Complex c2(1.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;
}

int main() {
    test();
}

实验五

user.hpp

#pragma once
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class User
{
public:
    User(string name0, string password = "111111", string email0 = ""):name{name0}, passwd{ password }, email{ email0 } 
    {n++;}
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() { cout << "there are" << n << "users"; }
private:
    string name, passwd, email;
    static int n;
};
int User::n = 0;
void User::set_email() {
    cout << "Enter email address :";
    cin >> email;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() {
    cout << "Enter old passwd:";
    int i;
    for (i = 0; i < 3; i++)
    {
        string b;
        cin >> b;
        if (b.compare(passwd) != 0)
        {
            if (i == 2)
                cout << "password input error.please try after a while." << endl;
            else
                cout << "password input error.please re-enter again:";
        }
        else
        {
            cout << "Enter new passwd:";
            cin >> passwd;
            cout << "new passwd os set successfully...";
            break;
        }
    }
}
void User::print_info()
{
    cout << "name:" << name << endl << "passwd:";
    string s1(passwd.length(), '*');
    cout << s1 << endl;
    cout << "email:" << email << endl;
}

task5.cpp

#include "User.hpp"
#include <iostream>
void test() {
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("John", "114514", "[email protected]");
    user1.print_info();

    cout << endl
        << "testing 2......\n\n";

    User user2("Lion");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}
int main() {
    test();
}

 

标签:const,cout,对象,double,void,Complex,实验,include
From: https://www.cnblogs.com/zqpqaz151034/p/16798866.html

相关文章