Programalama > C++

Etiketler: operator, overloading

Ort. 4
Puan ver:
//Class Bölümü Complex.h
#ifndef Complex_H
#define Complex_H
#include<iostream>
using std::istream;
using std::ostream;

class Complex{

friend istream & operator>>(istream &,Complex &) ;
friend ostream & operator<<(ostream &,Complex &) ;

public:
Complex & operator+(const Complex & )const;
Complex & operator-(const Complex & )const;
Complex & operator*(const Complex & )const;
Complex & operator/(const Complex & )const;
Complex(float=0.0,float=0.0);


private:
float real;
float imaginary;

};
#endif




//Fonksiyonların tanımlanması functions.cpp


#include<iostream>
#include "Complex.h"
using std::istream;
using std::ostream;
#include<iomanip>
using std::setw;
using std::endl;



Complex::Complex(float x,float y)
{
real=x;
imaginary=y;
}

Complex & Complex::operator /(const Complex &s)const
{
Complex div;

div.real=((real*s.real)+(imaginary*s.imaginary))/(s.real*s.real)-(s.imaginary*s.imaginary);
div.imaginary=(((-1)*real*s.imaginary)+(imaginary*s.real))/(s.real*s.real)-(s.imaginary*s.imaginary);

return div;
}


Complex & Complex::operator *(const Complex &s)const
{
Complex mul;

mul.real=(real*s.real)-(imaginary*s.imaginary);
mul.imaginary=(real*s.imaginary)+(imaginary*s.real);

return mul;
}

Complex & Complex::operator +(const Complex &s)const
{

return Complex(real+s.real,imaginary+s.imaginary);

}


Complex & Complex::operator -(const Complex &s)const
{
Complex sub;

sub.real=real-s.real;
sub.imaginary=imaginary-s.imaginary;

return sub;
}



ostream & operator<<(ostream & output,Complex & v) 
{
output<<"("<<v.real<<")+("<<v.imaginary<<")i"<<endl;
return output;
}


istream & operator>>(istream & input,Complex & va) 
{

input>>va.real;
input.ignore();
input>>va.imaginary;
input.ignore(1);

return input;
}




//main kısım  main.cpp


#include<iostream>
#include "Complex.h"
using std::endl;
using std::cout;
using std::cin;

int main()
{
Complex c2(0,0);
Complex c1(0,0);
Complex c3,c4,c5,c6;

cout<<"enter first complex num (real+imaginary i)"<<endl;
cin>>c1;
cout<<endl<<"enter second complex num (real+imaginary i)"<<endl;
cin>>c2;
cout<<endl;


c3=c1+c2;
c4=c1-c2;
c5=c1*c2;
c6=c1/c2;





cout<<"c1+c2 : "<<c3<<endl;

cout<<"c1-c2 : "<<c4<<endl;

cout<<"c1*c2 : "<<c5<<endl;

cout<<"c1/c2 : "<<c6<<endl;




system("pause");


return 0;
}


Yorumlar                 Yorum Yaz
Bu hazır kod'a ilk yorumu siz yapın!
KATEGORİLER
ASP - 240
ASP.NET - 24
C# - 75
C++ - 174
CGI - 8
DELPHI - 247
FLASH - 49
HTML - 536
PASCAL - 246
PERL - 11
PHP - 160
WML - 9
XML - 2
Copyright © 2002 - 2024 Hazır Kod - Tüm Hakları Saklıdır.
Siteden yararlanırken gizlilik ilkelerini okumanızı tavsiye ederiz.
hazirkod.com bir İSOBİL projesidir.