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

实验2 类与对象(2)

时间:2022-10-12 16:34:26浏览次数:36  
标签:real cout 对象 imag Complex 实验 c1 include

实验结论:

实验任务四:

Complex.hpp:

#pragma once
#include <iostream>
#include<math.h> 
using namespace std;
class  Complex {
public:
    Complex(double r = 0, double i = 0)  :real{ r }, imag{ i } {};
    Complex(const Complex& c) :real{ c.real }, imag{ c.imag } {};
    double get_real() const {
        return real;
    };
    double get_imag()const {
        return imag;
    };
    void show() const;
    
    void add(Complex c1) {

        real += c1.real; imag += c1.imag;
        
    };


    friend double abs(Complex c) {

        return sqrt(c.real * c.real + c.imag * c.imag);

    }
    friend bool is_equal(Complex c1, Complex c2) {
        if (c1.real == c2.real && c1.imag == c2.imag) { return 1; }
        else { return 0; }
    };

    friend Complex add(Complex c1, Complex c2) { Complex c; c.real = c1.real + c2.real; 
    c.imag = c1.imag + c2.imag; return c;
    };
    

private:
     double real, imag;

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

}

task 4:

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

void test() {
    using namespace std;

    Complex c1(5, 12);
    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:

User.hpp

#pragma once
#include <iostream>
#include<string>
using namespace std;
class User {
public:
    User(string name, string passwd = "111111", string email = "") :name{ name }, passwd{ passwd }, email{email} { count++; };
    void print_info() {
       
        cout << name << endl;
        string s2(passwd.length(), '*');
        cout << s2 << endl;
        cout << email << endl;
    };
    static void print_n() { cout <<"there are" <<" "<< count <<" users" << endl; };
    void set_email() {
        email = "";
        cout << "enter email address:";
        cin >> email;
        cout << "email is set successfully...." << endl;
        ; };
    void change_passwd(){
        int i; string s1;
        cout << "Enter old password:";
       
        for (i=0;i<3;i++) {
            cin >> s1;
            if (s1 == passwd) {
                cout << " Enter new password:";
                cin >> passwd;
                cout << "new passwd is set successfully...." << endl;
                break;
            }
            else  {
                cout << "password input error.Please re-enter again:";

            }
           
        }
        if (i == 3) {

            cout << "password input error.Please try after awhile" << endl;
        };
    
    }
private:
    string name;
    string passwd;
    string email;
    static int count;
};

int User::count = 0;

task 5:

#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_passwd();
    user2.set_email();
    user2.print_info();

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

int main() {
    test();
}

测试1:

测试2:

 

标签:real,cout,对象,imag,Complex,实验,c1,include
From: https://www.cnblogs.com/lovelexington/p/16784561.html

相关文章

  • 把sqlalchemy对象转化成json数据类型
    把sqlalchemy对象转化成json数据类型defto_json_all(msg:list):data=[]iftype(msg)==list:foriinrange(len(msg)):temp_dict......
  • 二维数组面对对象array
    publicclassDemo05{publicstaticvoidmain(String[]args){/*[5][2]面对对象1,2array[0]2,3array[1]3,4array[2]......
  • 实验5:开源控制器实践——POX
    一、基本要求1.构建拓扑2.使用tcpdump验证Hub1)h1pingh22)h1pingh33.tcpdump验证Switch模块1)h1pingh22)h1pingh34.L2_learning模块代码流程图二、......
  • 实验5:开源控制器实践——POX
    (一)基本要求1.阅读Hub模块代码,使用tcpdump验证Hub模块h1pingh2的tcpdump抓包结果h1pingh3的tcpdump抓包结果2.阅读L2_learning模块代码,画出程序流程图,使用tcpd......
  • Python基础 - 面向对象
    面向对象基础入门,理解概念为主,其妙用需要很长时间去领悟哦.引入Python既是面向过程,也能面向对象.初学来理解为啥要面向对象,不太可能,用处......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验五:开源控制器实践——POX
    基本要求1.tcpdump验证Hub模块h1pingh2的tcpdump抓包结果截图h1pingh3的tcpdump抓包结果截图2.tcpdump验证Switch模块h1pingh2的tcpdump抓包结果截图h1p......
  • 面向对象(上)01
    面向对象(上)01Java面向对象学习的三条主线:(第4-6章)1.Java类和类的成员:属性,方法,构造器;代码块,内部类.1.面向对象的三大特征:封装性,继承性,多态......
  • 实验5:开源控制器实践—— POX
    实验5:开源控制器实践——POX一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方......
  • 实验5:开源控制器实践——POX
    实验要求基本要求1.搭建SDN拓扑,协议使用OpenFlow1.0,控制器使用部署于本地的POX(默认监听6633端口)sudomn--topo=single,3--mac--controller=remote,ip=127.0.0.1,po......