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

实验二 类和对象

时间:2022-10-12 18:35:12浏览次数:35  
标签:const 对象 double void Complex 实验 c1 imag

#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
    public:
        Complex(double a=0,double b=0):real{a},imag{b}{};
        Complex(const Complex&c1);
        ~Complex()=default;
        
        double get_real()const {return real;}
        double get_imag()const {return imag;}
        void show()const;
        void add(Complex const &c1);
        
        friend Complex add(Complex const&c1,Complex const&c2);
        friend bool is_equal(Complex const&c1,Complex const&c2);
        friend double abs(Complex const&c1);
    private:
        double real;
        double imag;
    
};

Complex::Complex(const Complex &c1)
{
    real=c1.real;
    imag=c1.imag;
}
void Complex::show()const
{
    if(imag==0)
    cout<<real;
    else if(imag<0)
    cout<<real<<"-"<<abs(imag)<<"i";
    else
    cout<<real<<"+"<<imag<<"i";
    
}
void Complex::add(Complex const&c1)
{
    real+=c1.real;
    imag+=c1.imag;
}
Complex add(Complex const&c1,Complex const&c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.imag=c1.imag+c2.imag;
    return c3;
}

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

 

 

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

class User
{
    public:
        User(string name,string passwd="111111",string email=""):n{name},p{passwd},e{email}{count++;}
        ~User()=default;
        
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
        
    private:
        string n,p,e;
        static int count;
};
    int User::count=0;
    void User::set_email()
    {
        cout<<"Enter email adresss:";
        cin>>e;
        cout<<"email s set successfully..."<<endl;
    }
    
    void User::change_passwd()
    {
        cout<<"Enter old password:";
        string temp;
        int i=3;
        while(i)
        {
            cin>>temp;
            if(temp==p)
            {
                cout<<"new passwd is set successfully..."<<endl;
                break;
            }
            else
            {
                i--;
                if(i!=0)
                cout<<"password input error.Please re-enter again:";
            }
            if(i==0)
            {
                cout<<"password input error.Please try after a while."<<endl;
            }
        }
    }
    
    void User::print_info()
    {
        cout<<"name:"<<n<<endl;
        cout<<"passwd:"<<"******"<<endl;
        cout<<"email:"<<e<<endl;
    }
    void User::print_n()
    {
        cout<<"there are"<<count<<"users."<<endl;
    }

 

标签:const,对象,double,void,Complex,实验,c1,imag
From: https://www.cnblogs.com/djy769855989/p/16785536.html

相关文章

  • 实验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]......
  • 实验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既是面向过程,也能面向对象.初学来理解为啥要面向对象,不太可能,用处......