面向对象程序设计实验一

实验任务3:

Complex.hpp:

#include <iostream>
#include <cmath>
class Complex {
public:
    Complex() {};
    Complex(double r, double i = 0) :real(r), imag(i) {};
    Complex(Complex& p);
    double get_real()const { return real; };//常成员函数
    double get_imag()const { return imag; };
    void show() const;
    void add(const Complex& p);
    friend Complex add(const Complex& c1, const Complex& c2);//友元函数
    friend bool is_equal(const Complex& c1, const Complex& c2);
    friend double abs(const Complex& p);//取模运算
private:
    double real, imag;
};
Complex::Complex(Complex& p)
{
    real = p.real;
    imag = p.imag;
}
void Complex::show() const
{
    if(imag)//如果imag==0,不用输出虚部
        std::cout << real << imag << "i";
    else
        std::cout << real;
}
void Complex::add(const Complex& p)//实部虚部分别相加
{
    real += p.real;
    imag += p.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& p)
{
    return sqrt(p.real * p.real + p.imag * p.imag);
}

task.cpp:

#include "Complex.hpp"
#include <iostream>
int main()
{
    using namespace std;

    Complex c1(8, -9);
    const Complex c2(6.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;
}

运行截图:

面向对象程序设计实验一

 实验任务4:

 User.hpp

#include <iostream>
#include <string>
using namespace std;
class User
{
public:
    User(string n1, string p1 = "111111", string e1 = "") :name(n1), password(p1), email(e1) { n++; };
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() { cout << "there are "<<n<<" users." << endl; };
private:
    string name;//不加std::会报错,大部分标准库函数和类型都在std里
    string password;
    string 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()
{
    string s;
    cout << "Enter old password: ";
    cin >> s;
    int i = 0;
    for (i =1; i < 3; i++)
    {
        if (s == password)
        {
            cout << "Enter new password: ";
            cin >> password;
            cout << "new passwd is set successfully..."<<endl;
            break;
        }
        else
        {
            cout << "password input error.Please re-enter again: ";
            cin >> s;
        }
    }
    if (i == 3)
        cout<<"password input error.Please try after a while."<<endl;
}
void User::print_info()
{
    cout << "name: "<<name << endl;
    cout << "passwd: "<<"******" << endl;
    cout << "eamil: "<<email << endl;
}

task4.cpp

运行测试:

面向对象程序设计实验一面向对象程序设计实验一

实验总结:

1.定义成员函数的时候如果函数不改变对象状态,都应当使用const修饰

2.成员函数在外部定义要写上类名::

3.静态函数可以同过类名来调用 

 

上一篇:实验一:类与对象


下一篇:实验5 模板类与多态