实验2 类和对象(2)

实验任务4

#include "Complex.hpp"
#include <iostream>
void test() {
    using namespace std;
    Complex c1(4,-3);
    const Complex c2(6.6);
    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();
}
#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class Complex {
public:
    Complex(float r = 0, float i = 0) :real{r},imag(i){}
    Complex(const Complex& c);
    float get_real() const { return real; }
    float get_imag()const { return imag; }
    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 float abs(Complex& c);
private:
    float real, imag;
};
void Complex::show()const
{
    if (imag > 0)
        cout << real << "+" << imag << "i" << endl;
    else if (imag == 0)
        cout << real << endl;
    else
        cout << real << imag << "i" << endl;
}
Complex::Complex(const Complex& c)
{
    real = c.real, imag = c.imag;
}
void Complex::add(const Complex& c)
{
    real += c.real, imag += c.imag;
}
Complex add(const Complex& c1, const Complex& c2)
{
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
bool is_equal(const Complex& c1, const Complex& c2)
{
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    else
        return false;
}
float abs(Complex& c)
{
    return sqrt(c.real * c.real + c.imag * c.imag);
}

实验任务5

#include "User.hpp"
#include <iostream>
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();
    cout << endl
        << "testing 2......\n\n";
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();
}
int main() {
    test();
}
#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class User 
{
public:
    User(string n, string p = "111111", string e = "") :name{ n }, passwd{ p }, email{ e } { count++; }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() { cout<<"there are "<<count<<" users."<<endl; }
private:
    static int count;
    string name, passwd, email;
};
int User::count = 0;
void User::set_email()
{
    cout << "Enter email address:" << endl;
    cin >> email;
}
void User::change_passwd()
{
    int i;
    string passwd1;
    cout << "Enter old passwd:" << endl;
    for (i = 0; i < 3; i++)
    {
        cin >> passwd1;
        if (passwd1 == passwd)
        {
            cout << "Enter new passwd:" << endl;
            cin >> passwd1;
            passwd = passwd1;
            cout << "new password is set successfully..." << endl;
            break;
        }
        else
        {
            if (i == 2)
                cout << "password input error.Please try after a while" << endl;
            else
                cout << "password input error.Please re-enter again:" << endl;
        }   
    } 
}
void User::print_info()
{
    int len = passwd.length();
    string s1( len,'*' );
    cout << "name:" <<setw(8)<< name << endl;
    cout << "passwd:" << setw(8) << s1<< endl;
    cout << "email:" << setw(8) << email << endl;
}

测试1截图:

测试2截图:

实验总结:
1.解决了上一次实验存在的问题,即“const”常量使用的原因和方法。

2.可以较熟练的利用“static”对某一类事物共同属性的存储和使用,掌握了数据共享的工具—“friend”即友元函数和友元类的使用方法。

3.拓展了对C++标准库的使用范围,包括字符串“string”类的一些定义和函数的使用以及格式化输出的方法。

posted @ 2022-10-17 20:35  z2y  阅读(30)  评论(0)    收藏  举报