首页 > 其他分享 >实验2 类和对象(2)

实验2 类和对象(2)

时间:2022-10-19 08:22:13浏览次数:45  
标签:const string 对象 double void Complex 实验 imag

4.实验任务4

Complex.hpp

#pragma once
#include <iostream>
#include <math.h>

class Complex
{
private:
   double real,imag;
    
public:
    Complex();
    ~Complex();
    Complex(double x);
    Complex(double x, double y);
    Complex(const Complex& obj);
    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(const Complex& obj1);
    friend Complex add(const Complex& obj1, const Complex& obj2);
    friend bool is_equal(const Complex& a, const Complex& b);
    friend double abs(const Complex& obj);
};

Complex::Complex() : real(0), imag(0) {}

Complex::Complex(double x) : real(x), imag(0) {}

Complex::Complex(double x, double y) : real(x), imag(y) {}

Complex::Complex(const Complex& obj) : real(obj.real), imag(obj.imag) {}

double Complex::get_real() const { return real; }

double Complex::get_imag() const { return imag; }

void Complex::show() const 
{ 
    if(imag > 0)
    {
        std::cout << real << " + " << imag << "i";
    }else if(imag < 0) {
        std::cout << real << " - " << abs(imag) << "i";
    }else {
        std::cout << real;
    }
}

void Complex::add(const Complex& obj)
{
    real += obj.real;
    imag += obj.imag;
}
 
Complex add(const Complex& obj1, const Complex& obj2)
{
    Complex c(obj1.real + obj2.real, obj1.imag + obj2.imag);
    return c;
}

bool is_equal(const Complex& a, const Complex& b)
{
    return(a.real == b.real && a.imag == b.imag);
}

double abs(const Complex& obj)
{
    double c = sqrt(obj.real * obj.real + obj.imag * obj.imag);
}

Complex::~Complex() {}

task4.cpp

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

void test()
{
    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 << "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();
}

测试:

5.实验任务5

User.hpp

#pragma once

#include <iostream>
#include <string>
#include <iomanip>

using std::string;

class User
{
private:
    string name;
    string passwd;
    string email;
    static int n;

public:
    User(string N, string P = "111111", string E = "");
    ~User();
    void set_email();
    void change_password();
    void print_info();
    void static print_n();
};

int User::n = 0;
User::User(string N, string P, string E) : name(N), passwd(P), email(E)
{
     n++;
}

void User::set_email()
{
    std::cout << "Enter email address: ";
    std::cin >> email;
    std::cout << "email is set successfully...";
}

void User::change_password()
{
    std::cout << "Enter old password: ";
    string P1,P2;
    int k = 0;
    while (k < 3)
    {
        std::cin >> P1;
        if (passwd == P1)
    {
        std::cout << "Enter new password: ";
        std::cin >> P2;
        std::cout << "new passwd is set successfully..." << std::endl;
        break;
    }else {
        if(k < 2)
        {
            std::cout << "password input error. Please re-enter again: ";
        }else if (k == 2) { 
            std::cout << "password input error. Please try after a while." << std::endl;
        }
        k++;
    }
    }
}

void User::print_info()
{   
    string P(passwd.length(), '*');
    std::cout << std::setw(8) << std::left << "name: " << name << std::endl;
    std::cout << std::setw(8) << std::left << "passwd: " << P << std::endl;
    std::cout << std::setw(8) << std::left << "email: " << email << std::endl;
}

void User::print_n()
{
    std::cout << "there are " << n << " users." << std::endl;
}

User::~User() {}

task5.cpp

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

void test()
{
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";

    User user2("Leonard");
    user2.change_password();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main()
{
    test();
}

测试1:

测试2:

 

标签:const,string,对象,double,void,Complex,实验,imag
From: https://www.cnblogs.com/Sr-duel/p/16804897.html

相关文章

  • 实验6:开源控制器实践——RYU
    (一)基本要求搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。阅读Ryu文档的TheFirstApplication一节,运行当中的L2Switch,h1......
  • 实验2 类与对象(2)
    2022.10.12OOP实验课实验2类与对象(2)任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal;double......
  • 实验二
    Task4hpp文件如下#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0,doubleb=0):re......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基本......
  • 实验二 类和对象
    实验任务4task4.cpp#include"Complex.hpp"#include<iostream>//类测试voidtest(){usingnamespacestd;Complexc1(1,-8);constComplexc2(6......
  • 实验6:开源控制器实践——RYU
    一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、实验环境(一)基本要求1.搭建......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、......
  • 实验6:开源控制器实践——RYU
    1.建立拓扑并连接RYU控制器2.通过Ryu的图形界面查看网络拓扑3.(1)创建L2Switch.py文件点击查看代码fromryu.baseimportapp_managerfromryu.controllerimporto......
  • 实验6:开源控制器实践——RYU
    实验要求(一)基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器。建立拓扑并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑2.阅读Ryu文档的TheFirst......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......