ddng.net
当前位置:首页 >> 定义复数类ComplEx >>

定义复数类ComplEx

参考代码如下:public class Program { public static void main(String[] args) { class complex /*class complex begin*/ { private double real,imagin; public complex() { real=0; imagin=0; } public complex(double a,double b) { real = a; imagin = b; } public

模仿运算/ //类中声明 Complex operator +(Complex &); //重载运算符+ Complex operator -(Complex &); //重载运算符- //实现 Complex Complex::operator +( Complex &c) { Complex t; t.Real = this->Real + c.Real; t.Image =

#include <cstdlib>#include <iostream>#include<stdio.h>#include<stdlib.h> class Complex{ public: Complex():i(0),j(0){} Complex(double a,double b):i(a),j(b){} Complex(Complex const & c):i(c.i),j(c.j){} void Show(){ printf(" %.2g+%.2gi ",i,j); } private:

//定义复数类,构造两个对象,并调用成员函数对它们进行加减乘除 #include "iostream.h" class Complex { private : double real; double imag; public : void set(); void add(Complex a,Complex b); //+ void minus(Complex a,Complex b); //- void

class Complex{ public: Complex(); Complex(int real, int virtual); void SetComplex(int real, int virtual); void PrintComplex(); Complex Add(Complex c); private: int a; int b; }; Complex::Complex(){ } Complex::Complex(int real, int virtual){ a = real;

一个复数类Complex,定义带有2个参数(都为缺省参数)的构造函数;重载“/”(成员函数实现);重载“<<”(友元函数实现).class Complex Complex operator/(Complex &x) ostream &operator<<(ostream &out,Complex &x)

#include <iostream> using namespace std; class Complex{ private: double real; double imag; public: Complex(double r){real=r;imag=0;}; Complex(double r=0,double i=0){real=r;imag=i;}; void add(Complex c1){real += c1.real;imag += c1.imag;}; void

二楼的回答,很好呀!也可以把//main.h后面部分复制到最后一个#include下面,这样,就可以不要#include "main.h",一个程序制定.若是小程序建议这样调试.若是想着留着以后用的话,按二楼的去做.

这个书上应该有 原模原样用来讲 运算符重载 语法的例题

#includeusingnamespacestd;classComplex{public:Complex(floatr1,floati1);Complex(floatr1);voidadd(Complex&c);voidshow();private:floatr;floati;};Complex::Complex(floatr1,floati1){r=r1;i=i1;}voidComplex::add(Complex&c){r+=c.r;i+=c.i;}voidComplex::show(){cout0?'+':'-')

网站首页 | 网站地图
All rights reserved Powered by www.ddng.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com