What is the difference between this two codes for Complex class ?
What is the preference of code that written by pointers ?
Thanks ...

First code with pointers :

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

Second code without pointers :

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

The pointers are used two different ways. One is to declare the Complex objects on the heap using dynamic memory as opposed to on the stack using static memory. The other way is to pass the Complex objects to member variables by reference rather than by value.

Large objects are preferentially passed by reference because it is more efficient than recopying all the data in them. However, for something like this class that isn't really a major burden. Passing by reference means the value of the parameter passed in may be changed during the function and that value will be reflected back in the calling function as well. If that's not what you want, then either pass by constant reference or pass by value.

Which version is better/prefered----neither, they both have their uses.

What is the difference between this two codes for Complex class ?
What is the preference of code that written by pointers ?
Thanks ...

First code with pointers :

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

Second code without pointers :

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

Objects of Complex class are cloned when the messages sum and subtract are given an object with example - 2.

Passing by reference means the value of the parameter passed in may be changed during the function and that value will be reflected back in the calling function as well.

Well you are right. But there is often the case when passing-by-reference-to-const comes handy. It is usually the BEST method for passing big, user-defined objects.
the defination (in this context is):

Complex::Complex ( const double &real, const double &imaginary ){}

This serves two purposes: First, it make sure that the passing is by reference so you enjoy all the facilities references gives(like avoiding pointers). Secondly, the const makes sure that you get the abstraction offered by pass-by-value, i.e. you are sure that the function will not change any of the passed argument.
This is called const-correctness http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.1

Anyway, the first code is obliviously not a good choice when working with C++. C++ creators have tried very hard so that the programmer can get rid of pointers where he wants to.

To dear adatapost:
It is not polite to quote everything that OP has posted. I know it is easy to press the reply button, but it sure increases the page size and thus decreasing the amount of data that can fit in one page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.