-
December 1st, 2008, 07:52 AM
#1
[RESOLVED] What's wrong with the assignment operator?
Hi. Can someone please point out what's wrong with my "=" operator for this class? As it stands, it doesn't seem to do anything.
Any help would be greatly appreciated!
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//############################################################################################################################################################
//############################################################################################################################################################
//############################################################################################################################################################
class float_seq
{
public:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float_seq(int n=100) //CONSTRUCTOR. Accepts an integer as the size of the float_seq array.
{ //
if (n>1) //
{ //
number_of_elements = n; //
data = new float[number_of_elements]; //
} //
else //
{ //
cout << "Error: '" << n << "' is not a valid array size" << endl; //
} //
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float_seq(const float_seq &a):number_of_elements (a.number_of_elements) //COPY CONSTRUCTOR.
{ //
data = new float[number_of_elements]; //
for(int i=0;i<number_of_elements;i++) data[i] = a.data[i]; //
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~float_seq() //DESTRUCTOR
{ //
delete[] data; //
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float_seq &operator=(const float_seq &a) //'=' OPERATOR.
{ //
if (this == &a) return (*this); //
//
if(number_of_elements!=a.number_of_elements) //
{ //
number_of_elements=a.number_of_elements; //
delete[] data; //
data = new float[number_of_elements]; //
} //
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float float_seq::read(int i) const // READ FUNCTION FOR FLOAT_SEQ. "const" makes function read-only.
{ //
float temp=-1; //
if ((i<=(number_of_elements-1)) && (i>=0)) // ...checks that 'i' is a valid array size
{ //
cout << "Read function called:\tArray[" << i << "] = " << data[i] << endl; // ...prints out array value
temp = data[i]; //
} //
//
else // ...else if "i" is OOB, user is told
{ //
cerr << "Error: '" << i << "' is not a valid array reference" << endl; //
} //
//
return temp; // ...returns value, or -1 if input was invalid.
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float float_seq::write(int i, float input) // WRITE FUNCTION FOR FLOAT_SEQ.
{ //
float temp=-1; //
if ((i<=(number_of_elements-1)) && (i>=0)) //
{ //
data[i] = input; //
cout << "Write function called:\tArray[" << i << "] = " << data[i] << endl; //
temp = 0; //
} //
//
else //
{ //
cerr << "Error: '" << i << "' is not a valid array reference" << endl; //
} //
//
return temp; //
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
friend istream &operator>>(istream&,float_seq&); // STREAM OPERATORS
friend ostream &operator<<(ostream&,float_seq&); // friend declarations for
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private: // PRIVATE OBJECTS //
int number_of_elements; // number of elements in float_seq
float *data; // pointer to first element in float_seq
};
ostream &operator<<(ostream &out, float_seq &c)
{
out<<"\nArray elements are:\n";
for (int i=0;i<c.number_of_elements;i++)
{
out << "data[" << i << "]\t=\t" << c.data[i] << endl;
}
out << "\n\nSize is:\t" << c.number_of_elements << "\n\n";
return (out);
}
//############################################################################################################################################################
//############################################################################################################################################################
//############################################################################################################################################################
int main(void)
{
float_seq V(10); //creates 'V'
V.zero();
V.float_seq::write(3,(float) 3.1415); // sets array[3] in 'V' to '3.1415'
cout<<V;
float_seq W(V); // creates new copy of 'V', called 'W'
W.float_seq::write(3, (float) 1.602); // sets array[3] in 'W' to 1.602
cout<<W;
W=V; // assigns 'V' to 'W'
cout<<W;
cin.get();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|