zoukankan      html  css  js  c++  java
  • C#之运算符重载学习案例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Override_test
    {
        public class ComplexClass
        {
            public class complex
            {
                private float real;
                private float img;
                public complex(float p, float q)     //构造函数
                {
                    real = p;
                    img = q;
                }
                public complex()     //构造函数
                {
                    real = 0;
                    img = 0;
                }
                public void Print()
                {
                    Console.WriteLine("{0}+{1}i", real, img);
                }
                public static complex operator +(complex Lhs, complex rhs)
                {
                    complex sum = new complex();
                    sum.real = Lhs.real + rhs.real;
                    sum.img = Lhs.img + rhs.img;
                    return sum;
                }
                public static complex operator -(complex Lhs, complex rhs)
                {
                    complex result = new complex();
                    result.real = Lhs.real - rhs.real;
                    result.img = Lhs.img - rhs.img;
                    return result;
                }
            }
            static void Main()
            {
                complex A = new complex(10.5f, 12.5f);
                complex B = new complex(8.0f, 4.5f);
                complex C;
                Console.Write("Complex Number A:");
                A.Print();
                Console.Write("Complex Number B:");
                B.Print();
                C = A + B;
                Console.Write("
    A+B=");
                C.Print();
                C = A - B;
                Console.Write("A-B=");
                C.Print();
            }
        } 
    }

  • 相关阅读:
    对MFC文档、视图、框架的理解
    MFC中快速将CVIew转换成CScrollView
    MFC中的一个错误
    单文档中视图与文档的相互
    python函数
    python模块介绍和引入
    python面向对象和面向过程
    python数据类型2
    python数据类型
    python无法使用input功能
  • 原文地址:https://www.cnblogs.com/zztong/p/6695226.html
Copyright © 2011-2022 走看看