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

实验二 类与对象

时间:2022-10-12 18:33:47浏览次数:36  
标签:real const 对象 double imag Complex 实验 void

实验4

不使用C++标准库,自行设计并实现一个简化版复数类Complex.

 1 #pragma once
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 class Complex
 5 {
 6 public:
 7     Complex(double a=0, double b=0) :real{ a }, imag{ b }{};
 8     Complex(const Complex& c) :real{ c.real }, imag{ c.imag }{};
 9     double get_real()const { return real; }
10     double get_imag()const { return imag; }
11     void show()const;
12     void add(const Complex& c);
13     friend Complex add(const Complex& c1, const Complex& c2);
14     friend bool is_equal(const Complex& c1, const Complex& c2);
15     friend double abs(Complex& c);
16 private:
17     double real;
18     double imag;
19 };
20 void Complex::show()const {
21     if (imag > 0)
22         cout << real << "+" << imag << "i";
23     if (imag < 0)
24         cout << real << imag << "i"  ;
25     if (imag == 0)
26         cout << real ;
27 }
28 void Complex::add(const Complex& c) {
29     real += c.real;
30     imag += c.imag;
31 }
32 Complex add(const Complex& c1, const Complex& c2) {
33     Complex c;
34     c.real = c1.real + c2.real;
35     c.imag = c1.imag + c2.imag;
36     return c;
37 }
38 bool is_equal(const Complex& c1, const Complex& c2) {
39     if (c1.real == c2.real && c1.imag == c2.imag)
40         return true;
41     else
42         return false;
43 }
44 double abs(Complex& c) {
45     double ans;
46     ans = sqrt(c.real * c.real + c.imag * c.imag);
47     return ans;
48 }
#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();
}

 

标签:real,const,对象,double,imag,Complex,实验,void
From: https://www.cnblogs.com/zhouxv/p/16784952.html

相关文章

  • 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既是面向过程,也能面向对象.初学来理解为啥要面向对象,不太可能,用处......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验五:开源控制器实践——POX
    基本要求1.tcpdump验证Hub模块h1pingh2的tcpdump抓包结果截图h1pingh3的tcpdump抓包结果截图2.tcpdump验证Switch模块h1pingh2的tcpdump抓包结果截图h1p......