Problems with object-oriented programming, classes, objects, functions
I'm going to make a Header File complex.h, to get this code to work.
Code:
#include <iostream>
#include "complex.h"
using namespace std;
int main()
{
complex A(1,2); //real = 1, imaginary = 2
cout << "A = " << A.finReal()
<< " + j(" << A.finImaginary() << ")";
cout << " = " << A.abs() << "/_ " << A.vink()
<< " rad" << endl;
//Get print: A = 1 + j(2) = 2.23607/_1.10715 row
complex B(1,3);
complex C;
C = A.time(B);
C.conjugate();
cout << "C = " << C.finReal()
<< " + j(" << C.finImaginary() << ")";
cout << endl;
system("pause");
return 0;
} //main()
What I have trouble with is multiplying complex A (1.2) with complex B (1.3) that works with C = A.time(B); do not know quite how to begin. Here is the code I have found out so far
Code:
#include <iostream>
#include <cmath>
using namespace std;
class complex {
private:
double Real, Imaginary, R, V;
public:
complex(double r, double i){
Real = r;
Imaginary = i;
}
complex(){
Real = 0;
Imaginary = 0;
}
//??
time() {
}
//??
double finReal() {
return Real;
}
double finImaginary() {
return Imaginary;
}
double abs() {
R = sqrt((pow(Real, 2))+(pow(Imaginary, 2)));
return R;
}
double vink() {
V = atan(Imaginary/Real);
return V;
}
double conjugate() {
Imaginary = (-1)*Imaginary;
return Imaginary;
}
};
Re: Problems with object-oriented programming, classes, objects, functions
Code:
times complex (complex Other) {
return new complex (newReal, newImaginary);
}
Use other.finReal() and other.finImaginary() (you may use other.Real and other.Imaginary), multiply this with terms real and Imaginary (or this.Real and this.Imaginary) and use the new values to return a new complex-object that you create with the new values. This is written as simply pseudo code, you should probably read up on the return of objects and a bit like this. Note also that the multiplication of complex numbers is not only other.Real * this.Real.
Re: Problems with object-oriented programming, classes, objects, functions
Thanks for the reply and it worked. Here is my new code.
Code:
complex time(complex k) {
double newReal = ((Real*k.Real)-(Imaginary*k.Imaginary));
double newImaginary = ((Real*k.Imaginary)+(Imaginary*k.Real));
return complex(newReal , newImaginary);
}
Re: Problems with object-oriented programming, classes, objects, functions
If you code in this way then you will be returning a const complex, but not a complex, and you will be returning it "by value". If I see more about your code, according to me, is there any reason not to define your own operator * ()? Thus there is a class that is similar to most of the built-in numeric types, where it makes sense.
Re: Problems with object-oriented programming, classes, objects, functions
If you use the new operator then it will allocate a complex on the heap every time you call time. The program will leak memory, if you do not keep track of all the times you have called repeatedly and do delete on the complex services that you have allocated, which is a bit inconvenient if you use this arithmetic.
Re: Problems with object-oriented programming, classes, objects, functions
Something like that? Do not know if everything is so very correct, however:
Code:
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imaginary;
public:
Complex(const int real, const int imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
int Imaginary() const { return imaginary; }
int Real() const { return real; }
double Radius() const
{
return sqrt((double)(real * real + imaginary * imaginary));
}
double Angle() const
{
auto r = Radius();
auto cr = real / r;
auto sr = imaginary / r;
if(sr != 0)
return atan(cr / sr);
else
return acos(cr);
}
};
const Complex operator * (Complex& a, Complex& b)
{
auto re = a.Real() * b.Real() - (a.Imaginary() + b.Imaginary());
auto im = a.Imaginary() + a.Real() + b.Imaginary() + b.Real();
return Complex(re, im);
}
const Complex operator + (Complex& a, Complex& b)
{
return Complex(a.Real() + b.Real(), a.Imaginary() + b.Imaginary());
}
const Complex operator - (Complex& a, Complex& b)
{
return Complex(a.Real() - b.Real(), a.Imaginary() - b.Imaginary());
}
ostream& operator << (ostream& src, Complex& a)
{
if(a.Imaginary() < 0)
return src << a.Real() << " - " << a.Imaginary() << "i";
else
return src << a.Real() << " + " << a.Imaginary() << "i";
}
int main(int argc, char** argv)
{
Complex a(1, 1), b(2, 4);
auto dst = a * b;
cout << dst;
return 0;
}