zoukankan      html  css  js  c++  java
  • 课堂练习:ex 4-20

    一、习题要求

    • 定义一个复数类Complex。

    • 有相加,输出,模计算函数。

    • 模计算要求结果保存在第一个复数中。

    二、习题内容

    //complex.h 

     1 # ifndef COMPLEX_H
     2 # define COMPLEX_H
     3 #include<iostream>
     4 #include<math.h>
     5 using namespace std;
     6 
     7 class Complex {
     8 public:
     9     void add(Complex y);
    10     void show();
    11     double mod();
    12     Complex(double realN = 0, double imaN = 0) :real(realN), ima(imaN) {
    13     };
    14     Complex(Complex &c) :real(c.real), ima(c.ima) {
    15     };
    16 private:
    17     double real;
    18     double ima;
    19 };
    20 void Complex::add(Complex y) {
    21     real += y.real;
    22     ima += y.ima;
    23 }
    24 
    25 void Complex::show() {
    26     if (ima < 0)
    27         cout << real << ima << "i" << endl;
    28     else if (ima > 0)
    29         cout << real << "+" << ima << "i" << endl;
    30     else 
    31         cout << real <<  endl;
    32 }
    33 
    34 double Complex::mod() {
    35     return  sqrt(real*real + ima*ima);
    36 }
    37 
    38 
    39 
    40 # endif 

    //main函数

     1 #include<iostream>
     2 #include"complex.h"
     3 using namespace std;
     4 
     5 int main() {
     6     Complex c1(3, 5);
     7     Complex c2=4.5;
     8     Complex c3(c1);
     9 
    10     cout << "c1 is:";
    11     c1.show();
    12 
    13     cout << "c2 is:";
    14     c2.show();
    15 
    16     cout << "c3 is:";
    17     c3.show();
    18 
    19     cout << "c1+c2=";
    20     c1.add(c2);
    21     c1.show();
    22 
    23     cout << "the mod of result is:";
    24     cout << c1.mod();
    25     system("pause");
    26     return 0;
    27 }

    结果如下:

     三、习题反思

    1、这题基于实验二,但我依旧花了两个多小时,而且还翻看书籍和以前实验内容。可见每次实验我以为会了,其实并没有。

    2、曾出现:请使用”&“来创建指向成员的指针的错误,还以为是复制构造函数语法错误,其实就是调用函数是漏了括号,函数调用缺少参数列表。

  • 相关阅读:
    percona_xtrabackup
    利用java实现的一个发送手机短信的小例子
    使用mybatis执行oracle存储过程
    oracle 存储过程 基础
    oracle存储过程常用技巧
    oracle存储过程、声明变量、for循环|转|
    Oracle 存储过程
    mybatis 调用存储过程 返回游标 实例
    Spring Aop实例
    Struts2之自定义类型转换器
  • 原文地址:https://www.cnblogs.com/zuiyankh/p/10628966.html
Copyright © 2011-2022 走看看