• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
KochiyaSanae
博客园    首页    新随笔    联系   管理    订阅  订阅
实验一 类与对象

Test3

头文件

#ifndef Complex_HPP
#define Complex_HPP
#include<iostream>

class Complex {
public:
      Complex() {}
      Complex(double x1) :real(x1), imag(0) {}
      Complex(double x1, double x2) :real(x1), imag(x2) {}
      Complex(Complex& c1) :real(c1.real), imag(c1.imag) {}
      double get_real() const { return real; }
      double get_imag() const { return imag; }
      void show() const { if (imag >= 0) std::cout << real << "+" << imag << "i"; else std::cout << real << imag << "i"; }
      void add(const Complex& c2) { real += c2.real; imag += c2.imag; }
      friend Complex add(const Complex& c1, const Complex& c2);
      friend bool is_equal(const Complex& c1, const Complex& c2);
      friend double abs(const Complex& c1);
private:
    double real;
    double imag;
};
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& c)
{
    double a;
    a = sqrt(c.real * c.real + c.imag * c.imag);
    return a;
}
Complex add(const Complex& c1, const Complex& c2)
{
    Complex c3;
    c3.imag = c1.imag + c2.imag;
    c3.real = c1.real + c2.real;
    return c3;
}

#endif

源文件

#include "Complex.hpp"
#include <iostream>

int main()
{
    using namespace std;

    Complex c1(4, -5);
    const Complex c2(5.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;
}

修改值后所得结果

 

 

Test4

头文件

#ifndef User_HPP
#define User_HPP
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
class User
{
public:
    User() { n++; }
    User(string na, string pass = "222222", string em = "") :name(na), passwd(pass), email(em) { n++; }
    void set_email()
    {
        cout << "Enter email address: ";
        cin >> email;
        cout << "email is set successfully..."<< endl;
    }
    void change_passwd()
    {
        cout << "Enter old password: ";
        int i;
        string a;
        for (i = 0; i < 3; i++)
        {
            cin >> a;
            if (a == passwd)
            {
                cout << "Enter new passwd: ";
                cin >> passwd;
                i = 0;
                cout << "New password is set successfully..." << endl;
                break;
            }
            else if (a != passwd && i < 3)
            {
                cout << "password input error. Please re-enter again:";
            }
        }
        if (i == 3)
        {    
            cout << "password input error. Please try after a while"<< endl;
        }

    }
    void print_info()
    {
        cout << "name:   " << name << endl;
        cout << "passwd: " << "******" << endl;
        cout << "email:  " << email << endl;
    }
    static void print_n();


private:
    string name;
    string passwd;
    string email;
    static int n;
};
int User::n = 0;
void User::print_n()
{
    if (n == 0)
    {
        cout << "there is no users" << endl;
    }
    else if (n == 1)
    {
        cout << "there is 1 user." << endl;
    }
    else
    {
        cout << "there are " << n << " users" << endl;
    }
}

#endif

源文件

#include "User.hpp"
#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@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 on 2021-10-22 22:26  KochiyaSanae  阅读(29)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3