C++ Programming: Adding two objects, using pass-by-reference?

I have been working on a lab project for my C++ class, and am struggling. I'm new to programming, and we just got into Object-Oriented Programming.

My question concerns adding two objects. According to the lab paper, the function

Complex Add(Complex& pRHSOp);

MUST:
{Add THIS Complex number and pRHSOp to form a new Complex number named sum and returns sum. Note: pRHSOp is passed-by-reference because it is generally faster to pass objects by reference.}

where THIS function is the secondary constructor
(Complex::Complex(pInitReal, pInitImag)

Here is the beginning of the class declaration:

#ifndef COMPLEX_HPP
#define COMPLEX_HPP

#include <fstream>

using namespace std;

class Complex {
public:
Complex();
Complex(double pInitReal, double pInitImag);
Complex Add(Complex& pRHSOp);
______________________________________…
And here is a fraction of my C++ source code, so far:

#include <iomanip>
#include "Complex.hpp"

using namespace std;

Complex::Complex()
{
Init(0, 0);
}

Complex::Complex(pInitReal, pInitImag)
{
Init(pInitReal, pInitImag);
}

Complex Complex::Add(Complex& pRHSOp)
{
...
}
______________________________________…

Any help would be greatly appreciated. I feel like I have to make pRHSOp a constant reference, but I'm not too sure.
To add the two, you simply add the two imaginary parts and the real parts.

1
2
3
4
5
Complex Complex::Add(Complex& pRHSOp) 
{
             this->pInitReal+=pRHSOp.pInitReal    // pInitReal is the real part
             this->PInitImag+=pRHSOp.pInitImag // pInitImag is the imaginary part
}


Note that you don't actually need the this pointer.
Topic archived. No new replies allowed.