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

实验2 类与对象(2)

时间:2022-10-16 16:35:06浏览次数:49  
标签:const cout 对象 Complex 实验 User using include

一、实验内容:

实验任务4:

1.Complex.hpp:

#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
public:
    Complex(double x = 0, double y = 0) : real{x}, imag{y} {}
    ~Complex() {}
    Complex(const Complex &m) : real{m.real}, imag{m.imag} {}
    double get_real() { return real; }
    double get_imag() const { return imag; }
    void show() const
    {
        if (imag > 0)
            cout << real << "+" << imag << "i" << endl;
        if (imag == 0)
            cout << real << endl;
        if (imag < 0)
            cout << real << imag << "i" << endl;
    }
    Complex add(const Complex &n)
    {
        real = n.real + real;
        imag = n.imag + imag;
        return Complex(real, imag);
    }
    friend Complex add(const Complex &p, const Complex &q)
    {
        double real = p.real + q.real;
        double imag = p.imag + q.imag;
        Complex r;
        r.real = real;
        r.imag = imag;
        return r;
    }
    friend bool is_equal(Complex &p, const Complex &q)
    {
        if ((p.real != q.real) || (p.imag != q.imag))
            return false;
        else
            return true;
    }
    friend double abs(Complex &h)
    {
        return sqrt(h.real * h.real + h.imag * h.imag);
    }

private:
    double real, imag;
};

2.测试cpp:

#include <iostream>
#include "task4 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();
}

运行结果:

3.测试cpp(更换数据后):

#include <iostream>
#include "task4 Complex.hpp"
// 类测试
void test()
{
    using namespace std;

    Complex c1(7, -1);
    const Complex c2(-2.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:

1.User.hpp:

// User.hpp
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class User
{
public:
    User(string x, string y = "111111", string z = "") : name(x), passwd(y), email(z) { n++; }
    ~User() { n--; }
    void set_email()
    {
        string m;
        cout << "Enter email address:";
        cin >> m;
        cout << "email is set successfully..." << endl;
        email = m;
    }
    void change_passwd()
    {
        string a;
        int i;
        cout << "Enter old passwd:";
        cin >> a;
        for (i = 1; i < 3 && a.compare(passwd) != 0; i++)
        {
            cout << "passwd input error.Please re-enter again:";
            cin >> a;
        }
        if (a.compare(passwd) != 0)
            cout << "passwd input error.Please try after a while." << endl;
        else
        {
            cout << "Enter new passwd:";
            cin >> a;
            cout << "new passwd is set successfully..." << endl;
            passwd = a;
        }
    }
    void print_info()
    {
        int length;
        length = passwd.length();
        cout << std::left << setw(8) << "name:" << name << endl;
        cout << std::left << setw(8) << "passwd:";
        for (int i = 1; i <= length; i++)
            cout << "*";
        cout << endl;
        cout << std::left << setw(8) << "email:" << email << endl;
    }
    static void print_n()
    {
        cout << "there are " << n << " users." << endl;
    }

private:
    string name, passwd, email;
    static int n;
};
int User::n = 0;

2.测试cpp:

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

运行结果:

 

 

二、实验总结:

1.实验任务4的Complex初次撰写时,未注意到c2是一个const修饰的类,导致在测试代码时只要c2运用到的类成员函数都发生了错误。在之后,我修改了类Complex中有关成员函数,加上了const修饰,问题迎刃而解。

2.实验任务5的User撰写时,判断密码输入对错的循环体我书写错误,一开始以(i < 3 && a.compare(passwd) != 0)判断条件并且以if(i == 3)判断跳出位置,最后测试证明 此代码下即使第三次密码输入正确也会显示“try after a while”,最后索性用if( a.compare(passwd) != 0)判断,成功解决问题。

标签:const,cout,对象,Complex,实验,User,using,include
From: https://www.cnblogs.com/jyksblog/p/16796386.html

相关文章

  • 实验5:开源控制器实践——POX
    基础要求实验总结本次实验的难度相较于之前的几次实验要有所提升,本次实验中运用POX控制器,这使得我熟悉了这一控制器的安装方法及其基础使用方法,同时对于在前几......
  • 实验2 类和对象
    实验4#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doubler=0.0,doublei=0.0):real(r),i......
  • 实验1
    实验一  #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("II\n");return......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实......
  • 实验二
    任务四Complex.hpp#pragmaonce#include<iostream>#include<cmath>usingstd::cout;usingstd::endl;classComplex{public:Complex(doublex0,doubley......
  • 如何选择数据存储空间?华为云对象存储服务,让你省心又划算​
    上至大企业,下至普通用户,在平日里都习惯将一些重要的文件存储在网络云盘上,这样才便捷日常的使用,随意翻出来查看也省时,畅享移动的数据存储魅力。来自华为云的对象存储服务OBS(O......
  • 实验2
    实验2.4hpp:1#pragmaonce2#include<iostream>3#include<cmath>4usingnamespacestd;5classComplex{6public:7Complex(doublea=0,......
  • 实验2 类与对象(2)
    Task4hpp文件如下#pragmaonce#include<iostream>#include<complex>classComplex{public: Complex(); Complex(doubler,doublei=0.0); Complex(cons......
  • Java基础(七)| 类、对象、封装和构造详解
    ⭐本专栏旨在对JAVA的基础语法及知识点进行全面且详细的讲解,完成从0到1的java学习,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握JAVA编程,同时为后续的框架学习,进阶开......
  • 实验四
    一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......