Results 1 to 6 of 6

Thread: Problems with object-oriented programming, classes, objects, functions

  1. #1
    Join Date
    Apr 2010
    Posts
    31

    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;
            }
    
    };

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    31

    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);
            }

  4. #4
    Join Date
    May 2009
    Posts
    529

    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.

  5. #5
    Join Date
    May 2009
    Posts
    637

    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.

  6. #6
    Join Date
    Nov 2008
    Posts
    1,221

    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;
    }

Similar Threads

  1. Object oriented programming with java
    By cloud101 in forum Software Development
    Replies: 2
    Last Post: 22-02-2012, 01:11 AM
  2. Replies: 4
    Last Post: 11-07-2011, 02:39 PM
  3. Replies: 3
    Last Post: 08-01-2011, 06:20 AM
  4. static methods in object oriented programming
    By Satchel in forum Software Development
    Replies: 5
    Last Post: 07-01-2011, 09:58 PM
  5. Object oriented functions in Visual Basic 6.0
    By fLUTE in forum Software Development
    Replies: 4
    Last Post: 27-12-2010, 05:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,496,270.87664 seconds with 17 queries