سلام . من براي نوشتن کلاس Complex دچار مشکل شده بودم . مشکل من اين بود که مي خواستم داده هاي عضو دو تا شي از يک کلاس رو با هم جمع کنم . يکي به من پيشنهاد داد که دو متغير اشاره گر به کلاس رو به توابع جمع و تفريقم پاس کنم . ( همين کد اولي که گذاشتم . )
بعد من متوجه شدم که ميشه اين برنامه رو بدون اشاره گرها هم نوشت . نوشتم جوابش هم دقيقا مثل قبلي بود . ميخواستم بدونم چه فرقي دارن اين دو تا کد ؟ کدوم يکي مزيت داره ؟
[html]
//Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex ( double = 1, double = 0 );
void sum ( Complex *, Complex * );
void subtract ( Complex *, Complex * );
void print ( );
private:
double realPart,imaginaryPart;
};
#endif
//Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::Complex ( double real, double imaginary )
{
realPart = real;
imaginaryPart = imaginary;
}
void Complex::sum ( Complex *c1, Complex *c2 )
{
realPart = c1->realPart + c2->realPart;
imaginaryPart = c1->imaginaryPart + c2->imaginaryPart;
}
void Complex::subtract ( Complex *c1, Complex *c2 )
{
realPart = c1->realPart - c2->realPart;
imaginaryPart = c1->imaginaryPart - c2->imaginaryPart;
}
void Complex::print ( )
{
cout << "( " << realPart << ", " << imaginaryPart << " )";
}
//main.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main ( )
{
double real, imaginary;
cout << "Enter first complex :\n";
cin >> real >> imaginary;
Complex *c1 = new Complex ( real, imaginary );
cout << "Enter second complex :\n";
cin >> real >> imaginary;
Complex *c2 = new Complex ( real, imaginary );
Complex *c = new Complex ( );
cout << "Dafault complex is : ";
c->print ( );
cout << "\nSum of two entered complex : ";
c->sum ( c1, c2 );
c->print ( );
cout << "\nSubtract of two entered complex : ";
c->subtract ( c1, c2 );
c->print ( );
cout << endl;
return 0;
}
[/html]
کد خودم :
[html]
//Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex ( double = 1, double = 0 );
void sum ( Complex, Complex );
void subtract ( Complex , Complex );
void print ( );
private:
double realPart,imaginaryPart;
};
#endif
//Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::Complex ( double real, double imaginary )
{
realPart = real;
imaginaryPart = imaginary;
}
void Complex::sum ( Complex c1, Complex c2 )
{
realPart = c1.realPart + c2.realPart;
imaginaryPart = c1.imaginaryPart + c2.imaginaryPart;
}
void Complex::subtract ( Complex c1, Complex c2 )
{
realPart = c1.realPart - c2.realPart;
imaginaryPart = c1.imaginaryPart - c2.imaginaryPart;
}
void Complex::print ( )
{
cout << "( " << realPart << ", " << imaginaryPart << " )";
}
//main.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main ( )
{
double real, imaginary;
cout << "Enter first complex :\n";
cin >> real >> imaginary;
Complex c1 ( real, imaginary );
cout << "Enter second complex :\n";
cin >> real >> imaginary;
Complex c2 ( real, imaginary );
Complex c;
cout << "Dafault complex is : ";
c.print ( );
cout << "\nSum of two entered complex : ";
c.sum ( c1, c2 );
c.print ( );
cout << "\nSubtract of two entered complex : ";
c.subtract ( c1, c2 );
c.print ( );
cout << endl;
return 0;
}
[/html]
:20: :40: