zoukankan      html  css  js  c++  java
  • 声明一个复数类complex,使用友元函数add实现复数的加法-简单

    源程序:

    //1.声明复数的类,complex,使用友元函数 add 实现复数加法。

    #include < iostream >

    using namespace std;

    class Complex

    {

    private:

      double real, image;

    public:

      Complex() {}

      Complex(double a, double b)

      {

        real = a;

        image = b;

      }

      void setRI(double a, double b)

      {

        real = a;

        image = b;

      }

      double getReal()

      {

        return real;

      }

      double getImage()

      {

        return image;

      }

      void print()

      {

        if (image>0)

          cout << "复数:" << real << " + " << image << "i" << endl;

        if (image<0)

          cout << "复数:" << real << " - " << image << "i" << endl;

      }

      friend Complex add(Complex, Complex);//声明友元函数

    };

    Complex add(Complex c1, Complex c2)//定义友元函数

    {

      Complex c3;

      c3.real = c1.real + c2.real;//访问 Complex 类中的私有成员

      c3.image = c1.image + c2.image;

      return c3;

    }

    void main()

    {

      Complex c1(19, 0.864), c2, c3;

      c2.setRI(90, 125.012);

      c3 = add(c1, c2);

      cout << "复数一:";

      c1.print();

      cout << "复数二:";

      c2.print();

      cout << "相加后:";

      c3.print();

      system("pause");

    }

    运行结果:

  • 相关阅读:
    springmvcdemo
    src/main/resorces applicationContext.xml
    maven 中setting.xml
    Git
    刘相兵 AWR
    Oracle 查询SQL 执行次数---hash value
    Oracle 查询重复索引列
    Oracle 史上最全近百条Oracle DBA日常维护SQL脚本指令
    Oracle 查询表对应的索引
    11g RAC 自动升级 PSU
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11889477.html
Copyright © 2011-2022 走看看