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

实验二 类与对象(二)

时间:2022-10-18 16:59:16浏览次数:40  
标签:real const cout 对象 void Complex 实验 include

实验二任务四

代码:

Complex.hpp:

#pragma once
#include<iostream>
#include"math.h"
using namespace std;

class Complex{
    public:
        Complex(double r = 0.0,double i = 0.0):real{r},imag{i} {}
        Complex(const Complex &c):real{c.real},imag{c.imag} {}
        ~Complex()=default;
        
        double get_real() const {return real;} 
        double get_imag() const {return imag;}
        void add(const Complex &c);
        void show() const;
        
        friend Complex add(const Complex &c1,const Complex &c2);
        friend bool is_equal(const Complex &c1,const Complex &c2);
        friend double abs(Complex &c);
    private:
        double real,imag; 
};
void Complex::show() const{
    if(real==0 && imag==0 )
        cout << 0;
    else if(real!=0 && imag==0)
        cout << real;
    else if(real==0 && imag!=0)
        cout << imag << "i";
    else if(real!=0 && imag>0)
        cout << real << "+" << imag << "i";
    else
        cout << real << imag << "i";        
}

void Complex::add(const Complex &c){
    real+=c.real; imag+=c.imag;
}

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

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

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

 

task4:

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

运行结果截图:

 

 

 

实验二任务五

代码:

User.hpp

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

class User{
    public:
        User(string na,string p="111111",string e=""):name{na},passwd{p},email{e} {n++;}
        void set_email();
        void change_passwd();
        void print_info() const;
        void static print_n() {cout << "there are " << n << " users" << endl;}
    private: 
        string name,passwd,email;
        static int n;
};

int User::n=0;

void User::set_email()
{
    cout << "Enter email address: ";
    cin >> email;
    cout << "email is set successfully..." << endl;;
}

void User::change_passwd()
{
    int i;
    string old_passwd=passwd;
    cout << "Enter old password: ";
    for(i = 1;i <= 3;i++)
    {
        cin >> passwd;
        if(passwd==old_passwd)
        {
            cout << "Enter new passwd: ";
            cin >> passwd;
            cout << "new passwd is set successfully..." << endl;
            break;
        }
        else if(passwd!=old_passwd && i<3)
            cout << "password input error. Please re-enter again: ";
        else
            cout << "password input error. Please try after awhile" << endl;
    }
}

void User::print_info() const
{    
    string s2(passwd.length(), '*');
    cout << "name:   " << name << endl;
    cout << "passwd: " << s2 << endl;
    cout << "email:  " << email << endl;
}

task5:

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

运行结果截图:

test1:

test2:

 

标签:real,const,cout,对象,void,Complex,实验,include
From: https://www.cnblogs.com/whwssr/p/16803155.html

相关文章

  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopam......
  • 实验二
    实验任务4.hpp#pragmaonce#include<iostream>#include<string>#include<iomanip>#include<cmath>usingstd::cout;usingstd::endl;classComplex{public......
  • el-select change下拉获取对象
    <el-selectv-if="model"filterableclass="w"v-model="ruleForm.allModelId"placeholder="请选......
  • 对象新增方法
    Object.is()的缺点:会自动转换类型,而=的NaN不等于自身,+0等于-0,可以使用Object.is()来严格判断Object.assign()用于对象合并consttarget={a:1};constsource={b:2......
  • 拒绝了对对象 ‘GetTips‘ (数据库 ‘vipsoft‘,架构 ‘dbo‘)的 EXECUTE 权限
    SQLServer2016安装数据库—属性—权限—选择用户或角色—勾选执行权限即可。 ......
  • 实验二 类和对象(2)
    一.实验任务41.源代码:Complex.hpp:#pragmaonce#include<cmath>#include<iostream>usingnamespacestd;classComplex{public:Complex(doublem=0,double......
  • 实验二 类和对象
    实验四.cpp#include"Complex.hpp"#include<iostream>voidtest(){usingnamespacestd;Complexc1(7,-8);constComplexc2(7.8);Complexc......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验6: 开源控制器实践——RYU
    开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境U......
  • JavaScript的日期对象的使用Date
    1<script>2letdate=newDate();//返回的是当前的时间3console.log(date.getFullYear());//获取的当前年份4console.log(date.getMonth()+1);......