实验1 类与对象

实验任务3
#pragma once #include<iostream> class Complex { public: Complex(double r, double i = 0); Complex(const Complex& c); Complex() {} double get_real()const; double get_imag()const; void show()const; void add(Complex const complex); friend Complex add(const Complex& com1,const Complex& com2); friend bool is_equal(const Complex& com3,const Complex& com4); friend double abs(Complex& com5); ~Complex() {}; private: double real, imag; }; Complex::Complex(double r, double i) { real = r; imag = i; } Complex::Complex(const Complex& c) { real = c.real; imag = c.imag; } double Complex::get_real() const { return real; } double Complex::get_imag() const { return imag; } void Complex::show()const { using namespace std; double r, i; r = real; i = imag; cout << r; if (i < 0) { i *= -1; cout << " - " << i << "i"; } else if(i > 0) { cout << " + " << i << "i"; } cout << endl; } void Complex::add(Complex const complex) { real += complex.real; imag += complex.imag; } Complex add(const Complex& com1,const Complex& com2) { Complex c; c.real = com1.real + com2.real; c.imag = com1.imag + com2.imag; return c; } bool is_equal(const Complex& com3, const Complex& com4) { bool isfalse = false; if (com3.real == com4.real && com3.imag == com4.imag) { return true; } else { return false; } } double abs(Complex& com5) { return sqrt(com5.real * com5.real + com5.imag * com5.imag); }
#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 << 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

#include"User.h"

int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("Frank", "39984", "sob@gmail.com");
    user1.print_info();
    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("Sobriety");
    user2.change_password();
    user2.set_emai();
    user2.print_info();
    User::print_n();
#pragma once
#include<iostream>
#include<string>
#include <iomanip>

using namespace std;
class User
{
public:
    User(string _name, string _pasword="111111", string _email="");
    User(const User& u);
    ~User() { count--; }
    void set_emai();
    void change_password();
    void print_info();
    static void print_n();
private:
    static int count;
    string name, password, email;

};

int User::count = 0;

User::User(string _name, string _password, string _email)
{
    name = _name;
    password = _password;
    email = _email;
    count++;
}
User::User(const User& u)
{
    name = u.name;
    password = u.password;
    email = u.email;
    count++;
}

void User:: set_emai()
{

    cout << "Enter email address" << endl;
    cin >> email; 
    
}
void User::change_password()
{
    string s1,s2;
    cout << "Enter old password:" << endl;
    cin >> s1;
    for(int i=0;i<=2;i++)
    {
        if (s1.compare(password) == 0)
        {
            cout << "Enter new password" << endl;
            cin >> password;
            cout << "new password is set successfully..." << endl;
            break;
        }
        else
        {
            if (i < 2)
            {
                cout << "password input error." << "Please re-enter again:";
                cin >> s1;
            }
            else
            {
                cout << "password input error.Please try after a while." << endl;
            }
        }
    }


}
void User:: print_info()
{
    cout << "name:  "<<name<<endl;
    cout << "pasword:  "<<"******"<<endl;
    cout << "email:  " << email<<endl;
}
void User:: print_n()
{
    cout << "there are " << count << "users." << endl;
}

 

上一篇:实验一


下一篇:实验一 类与对象