请帮帮新手 重载加法

请帮帮新手 重载加法 - 故障解答 - 电脑教程网

请帮帮新手 重载加法

日期:2007-03-16   荐:
请帮帮新手 重载加法电力局提倡节约用电。如果某单位一季度用电总额超过2000度,总额加算10%。如果用电总额在3000度以上,则加算20%。如果将“每月用电度数”作为一个类,请为该类重载加法,使当三个该类对象对象相加为一个对象时,可以得到电力局实际收取的用电度数。请给出源程序!#include "stdio.h"class CElectro{public:CElectro(int nElectro);friend CElectro operator (const CElectro& left , const CElectro& right);void SetElectro(int nElectro);void Output();private:int m_nElectro;};CElectro::CElectro(int nElectro){SetElectro(nElectro);}void CElectro::Output(){printf("%d" , m_nElectro);}void CElectro::SetElectro(int nElectro){if( nElectro <= 0)m_nElectro = 0;else if( nElectro <= 2000 )m_nElectro = nElectro;else if( nElectro > 2000 && nElectro <= 3000 )m_nElectro = nElectro (int)((double)nElectro * 10 / 100);else m_nElectro = nElectro (int)((double)nElectro * 20 / 100);}CElectro operator (const CElectro& left , const CElectro& right){return CElectro(left.m_nElectro right.m_nElectro);}void main(){CElectro a(1000) , b(2500) , c(5000);CElectro d(0);d = a b c;d.Output();}这个就 好的很那,难道不行?o ,看错了 ,每月用电度数作为一个类?稍加改动就成吧。#include "stdio.h"class CElectro{public:CElectro(int nElectro);friend CElectro operator (const CElectro& left , const CElectro& right);void SetElectro(int nElectro);void Output();private:int m_nElectro;//实际电量int m_nActual;//结算电量};CElectro::CElectro(int nElectro):m_nElectro(nElectro){SetElectro(nElectro);}void CElectro::Output(){printf("实际电量:%d" , m_nElectro);printf("结算电量:%d",m_nActual);}void CElectro::SetElectro(int nElectro){if( nElectro <= 0)m_nActual = 0;else if( nElectro <= 2000 )m_nActual = nElectro;else if( nElectro > 2000 && nElectro <= 3000 )m_nActual = nElectro (int)((double)nElectro * 10 / 100);else m_nActual = nElectro (int)((double)nElectro * 20 / 100);}CElectro operator (const CElectro& left , const CElectro& right){return CElectro(left.m_nElectro right.m_nElectro);}void main(){CElectro a(1000) , b(2500) , c(5000);a.Output();b.Output();c.Output();CElectro d(0);d = a b c;d.Output();}这个问题很简单的给你一个重载的例子自己研究吧最近忙#include"fushu.h"void main(){char i;/*float m,d,e,f;cout<<"请输入第一个复数的实部和虚部:";cin>>m>>d;cout<<"请输入第二个复数的实部和虚部:";cin>>e>>f;*///Complex a(m,d),b(e,f),c;Complex a,b,c;cout<<"请输入第一个复数:";cin>>a;cout<<a; cout<<"请输入第二个复数:";cin>>b;cout<<"第一个复数:";cout<<a;//a.display();cout<<"第二个复数:";cout<<b;//b.display();cout<<endl;cout<<endl;while(1){cout<<"1 两复数相加"<<endl;cout<<"2 两复数相减"<<endl;cout<<"3 两复数相乘"<<endl;cout<<"4 两复数相除"<<endl;cout<<"5 复数自加(前增量:a )"<<endl;cout<<"6 复数自加(后增量: a)"<<endl;cout<<"7 复数加等于(a =b)"<<endl;cout<<"8 复数赋值(c=a)"<<endl;cout<<"9 退出"<<endl;cout<<"请输入你的选择(1-9):";cin>>i;switch(i){case '1'://c=addComplex(a,b);c=a b;break;case '2'://c=subComplex(a,b);c=a-b;break;case '3'://c=multiply(a,b);c=a*b;break;case '4'://c=divide(a,b);c=a/b;break;case '5':c=a;c ;break;case '6':c=a; c;break;case '7':c=a;c =b;break;case '8':c=a;break;case '9':cout<<"谢谢使用!再见!"<<endl;return;}cout<<"你所选的操作的结果为:"; cout<<c;//c.display();}}#include<iostream.h>#include<math.h>class Complex{friend ostream &operator<<(ostream &output,Complex&obj);friend istream &operator>>(istream &input,Complex&obj);public:Complex(double r1,double im){real=r1;imag=im;}Complex(){}//void display();Complex &operator ();//&左值Complex &operator =(Complex t);Complex &operator (int);Complex operator =(Complex t);Complex operator (Complex t);Complex operator -(Complex t); Complex operator *(Complex t);Complex operator /(Complex t);//friend Complex addComplex(Complex &a,Complex &b); // friend Complex subComplex(Complex &a,Complex &b);//friend Complex multiply(Complex &a,Complex &b);//friend Complex divide(Complex &a,Complex &b);private:double real;double imag;};ostream &operator<<(ostream &output,Complex &obj){char sign=' ';if(obj.imag<0)sign='-';output<<obj.real<<sign<<fabs(obj.imag)<<"i"<<endl;return output;}istream &operator>>(istream &input,Complex &obj){cout<<"请输入复数的实部虚部:";input>>obj.real>>obj.imag;return input;}Complex &Complex::operator (int)//后加{//Complex temp(*this);real ;return *this;}Complex &Complex::operator ()//前加{ real;return *this;}Complex &Complex::operator =(Complex t){real =t.real; imag =t.imag;return *this;}Complex Complex::operator =(Complex t){return Complex(real=t.real,imag=t.imag);}Complex Complex::operator (Complex t)//存在BUG应该用友员实现{return Complex(real t.real,imag t.imag);}Complex Complex::operator -(Complex t)//{return Complex(real-t.real,imag-t.imag);//}Complex Complex::operator *(Complex t){return Complex(real*t.real-imag*t.imag,real*t.imag real*imag);}Complex Complex::operator /(Complex t){return Complex((real*t.real imag*t.imag)/(t.real*t.real t.imag*t.imag),(imag*t.real-real*t.imag)/(t.real*t.real t.imag*t.imag));}/*Complex addComplex(Complex &a,Complex &b){Complex temp1;temp1.real=a.real b.real;temp1.imag=a.imag b.imag;return temp1;}Complex subComplex(Complex &a,Complex &b){Complex temp4;temp4.real=a.real-b.real;temp4.imag=a.imag-b.imag;return temp4;}Complex multiply(Complex &a,Complex &b){Complex temp2;temp2.real=a.real*b.real-a.imag*b.imag;temp2.imag=a.real*b.imag b.real*a.imag;return temp2;}Complex divide(Complex &a,Complex &b){Complex temp3;temp3.real=(a.real*b.real a.imag*b.imag)/(b.real*b.real b.imag*b.imag);temp3.imag=(a.imag*b.real-a.real*b.imag)/(b.real*b.real b.imag*b.imag);return temp3;}*/#include<iostream>using namespace std;class ele{ float tdata; //真实电量 float data; //收取电量 public: ele() { tdata=0; data=0; } ele(int n) { if(n<0)data=0; else if(n<2000)data=n; else if(n<3000)data=n*1.1; else data=n*1.2; tdata=n; } ~ele(){} ele operator (const ele &); void operator=(const ele &); void show();};ele ele::operator (const ele &e) { ele temp(e.tdata tdata); return temp;}void ele::operator=(const ele &e) { data=e.data; tdata=e.tdata;}void ele::show(){ cout<<"真实电量为:"<<tdata<<" 收取电量为:"<<data<<endl;}int main(){ ele e1(999),e2(1002),e3(3000); e1.show(); e1=e1 e2; e1.show(); e1=e1 e3; e1.show(); system("PAUSE"); return 0;}晕?gt;>≌飧鑫侍獠皇且丫
标签: