首页 > 其他分享 >实验二 类和对象

实验二 类和对象

时间:2022-10-19 00:55:25浏览次数:49  
标签:cout 对象 double void passwd Complex 实验 User

实验任务4

task4.cpp

#include "Complex.hpp"
#include <iostream>
// 类测试

void test() {
    using namespace std;
    Complex c1(1, -8);
    const Complex c2(6.4);
    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();
}

Complex.hpp

#pragma once
#include <iostream>
#include <cmath>
using namespace std;
    
//Complex类的声明
class Complex {
public:
    Complex();
    Complex(double a, double b);
    Complex(double a);
    Complex(const Complex& obj);
    ~Complex() = default;

    //成员函数
    double get_real() const{ return real; }
    double get_imag() const{ return imag; }

    void show() const;
    void add(Complex c2);


    //友元函数
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);

    friend double abs(Complex c1);
private:
    double real;
    double imag;
};

Complex::Complex() {
    real = 0;
    imag = 0;
}
Complex::Complex(double a, double b) {
    real = a;
    imag = b;
}
Complex::Complex(double a) {
    real = a;
    imag = 0;
}
Complex::Complex(const Complex& obj):real{obj.real},imag{obj.imag}{}

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

void Complex::add(Complex c2){
    real += c2.real;
    imag += c2.imag;
}

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

bool is_equal(Complex c1, Complex c2) {
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    else
        return false;
}
 
double abs(Complex c1) {
   return sqrt(c1.real * c1.real + c1.imag * c1.imag);
}

使用的测试数据:

 c1=(1, -8) ,  c2=(6.4)

 实验任务5

task5.cpp

#include "User.hpp"
#include <iostream>
// 测试User类
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();
}

User.hpp

#pragma once
#include<iostream>
#include<string>
 
using namespace std;

class User {
public:
    User(string mz);
    User(string mz, string mm, string yx);
    ~User() = default;

    //成员函数
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();

private:
    string name;
    string passwd;
    string email;
    static int count;       //静态数据成员声明
};

int User::count = 0;        //静态数据成员定义和初始化,使用类名限定

void User::print_n() { cout << "There are " << count << " Users." << endl; }
 
User::User(string mz) :name{ mz }, passwd{ "111111" }, email{""} { count++; }

User::User(string mz, string mm, string yx) :name{ mz }, passwd{ mm }, email{ yx } { count++; }

void User::set_email() {
    string address;
    cout << "enter email address:";
    cin >> address;
    email = address;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() {
    string old_passwd;
    string new_passwd;
    int t = 1;
    cout << "enter old passwd:";
    cin >> old_passwd;
    do {
        if (passwd == old_passwd) {
            cout << "enter new passwd:";
            cin >> new_passwd;
            passwd = new_passwd;
            break;
        }
        else
        {
            t++;
            cout << "passwd input error.Please re-enter again:";
            cin >> old_passwd;
        }
    } while (t < 3);
    if (t == 3) {
        cout << "passwd enter error.Please try after a while." << l;
    }
}
void User::print_info() {
    int length = passwd.length();
    cout << "name:   " << name << endl;
    cout << "passwd: ";
        while (length--) {
            cout << "*";
        }
        cout << endl;
    cout << "email:  " << email << endl;
}

测试数据1:

测试数据2:

 

标签:cout,对象,double,void,passwd,Complex,实验,User
From: https://www.cnblogs.com/mohaolan/p/16804303.html

相关文章

  • 实验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控制器编写自定义网络......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验二 类和对象
    #pragmaonce#include<iostream>#include<iomanip>#include<cmath>usingstd::cout;usingstd::endl;usingstd::setfill;usingstd::setw;usingstd::left;......
  • 实验5:开源控制器实践——POX
    生成拓扑:点击查看代码sudomn--topo=single,3--mac--controller=remote,ip=127.0.0.1,port=6633--switchovsk,protocols=OpenFlow10阅读Hub模块代码,使用tcpdu......
  • 实验5:开源控制器实践——POX
    (一)基本要求h1pingh2、h2和h3的tcpdump抓包结果使用tcpdump验证Hub模块使用tcpdump验证Switch模块L2_learning模块代码流程图(二)进阶要求重新搭建(一......
  • 类和对象(2)
    实验4:Complex.hpp文件源码1#pragmaonce23//Complex类的定义4#include<iostream>56usingnamespacestd;78//Complex类的声明9classComp......