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

实验二 类和对象(2)

时间:2022-10-12 19:11:41浏览次数:34  
标签:const cout 对象 void Passwd Complex 实验 include

实验结论

task 4

Complex.hpp

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

using namespace std;

class Complex{

public:
        
    Complex (double x = 0, double y = 0) : real{x}, imag{y} {}
    Complex (const Complex &obj) : real{obj.real} , imag{obj.imag} {}
    
    double get_real() const { return real; }
    double get_imag() const { return imag; }
    
    void show() const {
        cout << real;
        if(imag > 0)
            cout << " + " << imag << "i";
        else if(imag < 0)
            cout << " - " << -imag << "i";
    }
    
    void add(const Complex &obj){
        real += obj.get_real();
        imag += obj.get_imag();
    }

    friend Complex add(const Complex &a, const Complex &b){
        return Complex(a.real + b.real, a.imag + b.imag);
    }

    friend bool is_equal(const Complex &a, const Complex &b){
        return fabs(a.real - b.real) <= 1e-5 && fabs(a.imag - b.imag) <= 1e-5;
    }

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

private:
    double real, imag;
};

 

task4.cpp

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

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();
}

测试结果

 

 

 更换一组复数的测试结果

 

 

 

task 5

User.hpp

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

class User{

public:
    
    User (string Name, string Passwd = "111111", string Email = "") 
        : name{Name} , passwd{Passwd}, email{Email} {n ++; }
        
    void set_email(){
        cout << ("Enter email address: ");
        cin >> email;
        puts("email is set successfully...");
    }
    
    void change_passwd(){
        
        string Passwd;
        
        cout << ("Enter old password: ");
        cin >> Passwd;
        
        for(int wrong = 1; wrong <= 2 && Passwd != passwd; wrong ++){
            cout <<("password input error. Please re-enter again: ");
            cin >> Passwd;
        }
        
        if(Passwd == passwd){
            cout << "Enter new passwd: ";
            cin >> passwd;
            puts("new passwd is set successfully...");
        }
        else
            puts("password input error. Please try after a while.");
    
    }
    
    void print_info(){
        cout << "name:   " << name << "\n";
        cout <<"passwd: ";
        
        for(int i = 0; passwd[i]; i ++)
            cout << "*";
        cout << "\n";
        cout << "email:  " << email << "\n";
    }
    
    static void print_n(){
        cout << "there are " << n << " users.\n";
    }
    
private:
    string name, passwd, email;
    static int n;
}; 

int User::n = 0;

task5.cpp

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

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();
}

测试结果

 

 

 

更换一组测试信息的测试结果

 

标签:const,cout,对象,void,Passwd,Complex,实验,include
From: https://www.cnblogs.com/lyhy/p/16785629.html

相关文章

  • 0day安全:软件漏洞技术分析-Crack小实验
    Crack小实验#include<stdio.h>#definePASSWORD"1234567"intverify_password(char*password){ intauthenticated; authenticated=strcmp(password,PASSWORD)......
  • 实验二
    1#pragmaonce2#include<iostream>3#include<cmath>4usingnamespacestd;5classComplex6{7friendComplexadd(Complexc1,Complexc2);......
  • 实验二 类和对象
    #include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0,doubleb=0):real{a},imag{b}{};Complex(......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验二 类与对象
    实验4不使用C++标准库,自行设计并实现一个简化版复数类Complex.1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4classComplex5{6public......
  • Python 为什么会有个奇怪的“...”对象?
    在写上一篇《​​Python为什么要有pass语句?​​》时,我想到一种特别的写法,很多人会把它当成pass语句的替代。在文章发布后,果然有三条留言提及了它。所谓特别的写法就是......
  • 实验5:开源控制器实践——POX
    一、基础要求1、使用tcpdump验证Hub模块h1pingh22、使用tcpdump验证Switch模块L2_learning模块代码流程图h1pingh2h1pingh3二、进阶要求1、重新搭建......
  • 实验2 类与对象(2)
    实验结论:实验任务四:Complex.hpp:#pragmaonce#include<iostream>#include<math.h>usingnamespacestd;classComplex{public:Complex(doubler=0,do......
  • 把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]......