C++语言程序设计实验一 类与对象

 

 

   Complex.hpp文件源码:

复制代码
#include <iostream>

using namespace std;

class Complex {
public:
    Complex(float r = 0, float i = 0) : real{ r }, imag{ i }{ }
    Complex(const Complex& obj) : real{ obj.real }, imag{ obj.imag }{ }
    float get_real() const { return real; }
    float get_imag() const { return imag; }
    void show() const {
        if (imag == 0) {
            cout << real;
        }
        else if(imag < 0){
            cout << real << " - " << std::abs(imag) << "i";
        }
        else {
            cout << real << " + " << imag << "i";
        }
    }
    void add(const Complex c2) {
        real += c2.real;
        imag += c2.imag;
    }
    friend Complex add(Complex c1, Complex c2) {
        Complex c3;
        c3.real = c1.real + c2.real;
        c3.imag = c1.imag + c2.imag;
        return c3;
    }
    friend bool is_equal(Complex c1, Complex c2) {
        bool equal = false;
        if (c1.real == c2.real && c1.imag == c2.imag) {
            equal = true;
        }
        return equal;
    }
    friend float abs(Complex c1) {
        float m = 0;
        m = std::sqrt(c1.real * c1.real + c1.imag * c1.imag);
        return m;
    }

private:
    float real;
    float imag;
};
复制代码

  task3.cpp源码:

复制代码
#include "Complex.hpp"
#include <iostream>
int main()
{
    using namespace std;
    Complex c1(4, -1);
    const Complex c2(-2);
    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;
}
复制代码

  运行测试结果截图:

 

 

  myUser.hpp文件源码 :(由于我的电脑内可能存在User.hpp文件,故使用myUser命名该文件)

复制代码
#include <iostream>
#include <iomanip>

using namespace std;

class User {
public:
    User(string namestr, string passwdstr = "111111", string emailstr = " ") : name{ namestr }, passwd{ passwdstr }, email{ emailstr }{ n++; }
    void set_email() {
        string new_email;
        cout << "Enter email address: ";
        cin >> new_email;
        email = new_email;
        cout << "email is set successfully\n";
    }
    void change_passwd() {
        string old_passwd, new_passwd;
        int count = 0;
        cout << "Enter old password: ";
        cin >> old_passwd;
        while (count < 2) {
            if (passwd != old_passwd) {
                cout << "password input error. Please re-enter again: ";
                cin >> old_passwd;
            }
            else {
                cout << "Enter new password: ";
                cin >> new_passwd;
                passwd = new_passwd;
                cout << "new password is set successfully...\n";
                return;
            }
            count++;
        }
        if (passwd != old_passwd) {
            cout << "password input error. Please try after a while.\n";
        }
        else {
            cout << "Enter new password: ";
            cin >> new_passwd;
            passwd = new_passwd;
            cout << "new password is set successfully...\n";
        }
    }
    void print_info() {
        cout << left << setw(8) << "name:" << name << endl;
        cout << setw(8) << "passwd:" << "******" << endl;
        cout << setw(8) << "email:" << email << endl;
    }
    void static print_n() {
        cout << "there are " << n << " users.";
    }

private:
    string name;
    string passwd;
    string email;
    static int n;
};
复制代码

  task4.cpp源码:

复制代码
#include "myUser.hpp"
#include <iostream>

int User::n = 0;

int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("Luna", "46713", "112@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();
}
复制代码

  运行测试结果截图:

 

 

 

 

 

 

 

posted @   miilue  阅读(104)  评论(5编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
点击右上角即可分享
微信分享提示