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

实验2 类与对象(2)

时间:2022-10-19 02:23:19浏览次数:45  
标签:cout 对象 double void Complex 实验 User string

2022.10.12 OOP实验课

实验2 类与对象(2)

任务4

Complex.hpp

#pragma once

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

class Complex
{
private:
    double real;
    double imag;
public:
    //构造函数
    Complex();
    //~Complex();
    Complex(double real0, double imag0);
    Complex(const Complex& obj);
    Complex(double real0);

    //成员函数
    double get_real() const {return real;};
    double get_imag() const {return imag;};
    void show() const;
    void add(const Complex obj);

    //友元函数
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs(Complex obj);
};
//构造函数
Complex::Complex():real{0.0},imag{0.0}
{
}

Complex::Complex(double real0, double imag0):real{real0}, imag{imag0}
{
}

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

Complex::Complex(double real0):real{real0},imag{0.0}
{
}
//成员函数
void Complex::show() const{
    if(imag == 0){
        std::cout << real;
    }
    else if(imag>0){
        std::cout << real << "+" << fabs(imag) << "i";
    }
    else if(imag<0){
        std::cout << real << "-" << fabs(imag) <<"i";
    }
}

void Complex::add(const Complex obj){
    real += obj.real;
    imag += obj.imag;
}
//友元函数
Complex add(Complex c1, Complex c2){
    return Complex(c1.real+c2.real, c1.imag+c2.imag);
}

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

double abs(Complex obj){
    return sqrt(pow(obj.real,2) + pow(obj.imag,2));
}

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


User.hpp

#pragma once

#include <iostream>
#include <string>

using namespace std;

class User{
private:
//数据成员
    string name, passwd, email;
    

public:
//函数成员
    //构造函数
    User(string name0);
    User(string name0, string passwd, string email);

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

    static int n;
};

int User::n = 0;
//构造函数
User::User(string name0): name{name0}, passwd{"111111"}, email{""}{n++;}
User::User(string name0, string passwd0, string email0): name{name0}, passwd{passwd0}, email{email0}{n++;}
//成员函数 
void User::set_email(){
    cout <<"Enter email address: ";
    cin >> email;
    cout << "email is set sucessfully..." << endl;
}

void User::change_passwd(){
    string testpasswd;
    cout << "Enter old password: ";
    cin >> testpasswd;//输入测试密码

    for(int i=1; i<3 && testpasswd != passwd; i++){
        //一共三次机会
        cout << "password input error. Please re-enter again: ";
        cin >> testpasswd;
    }
    if(testpasswd != passwd){
        //连续输错三次
        cout << "passworf input error. Please try after a while.\n";
    }else if(testpasswd == passwd){
        cout << "Enter new passwd: ";
        cin >> passwd;
        cout << "new password is set successfully...";
    }
}

void User::print_info(){
    cout << "name:   " << name << endl;//打印名字

    int num = passwd.length();
    cout << "passwd: ";
    for( int i=0; i < num ; i++){
        cout << "*";
    }
    cout << endl;//打印密码

    cout << "email:  " << email << endl;//打印邮箱 

}

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

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("Lenard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}
int main(){
    test();
}

测试结果:


标签:cout,对象,double,void,Complex,实验,User,string
From: https://www.cnblogs.com/Aquarius-CHEqijin/p/16804782.html

相关文章

  • 实验二
    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控制器编写自定义网络......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验二 类和对象
    #pragmaonce#include<iostream>#include<iomanip>#include<cmath>usingstd::cout;usingstd::endl;usingstd::setfill;usingstd::setw;usingstd::left;......