2. Complete the implementation of the class list. Each object is a list of integers.
Note that the destructor releases the memory occupied by data. Use the program
template list.cpp and read from the input file list.in.
// File: list.cpp
3
#include
#include
using namespace std;
class list
{
private:
int* data; // pointer to entries in the list
int len; // size of the list
public:
list(int n=1, int value=0); // list of size n, all entries
// are value
list(istream& in); // reads len then data from in
list(const list& b); // copy constructor
~list(void){ delete [] data;} // destructor
list& operator=(const list& b); // operator =
void write(ostream& out); // sends the entries in data to
out
};
int main () // test the members
{
ifstream fin (“list.txt”);
ofstream fout (“listout.txt”);
list x(fin); // constructor which reads from a file
list y(fin);
list z(x); // copy constructor
fout
fout
fout
z = y; // operator =
fout
z = 7; // try conversion
fout
return 0;
}
void list::write(ostream& out)
{
int i;
out
for(i=0; i
}

list.txt

3 1 2 3

5 5 4 3 2 1



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *