Solved

constructor and destructor

Posted on 2004-04-20
3
512 Views
Last Modified: 2010-04-01
I am told to write a constructor which accepts data from a file and writes it to a node and a destructor which reads data from the node to a file. I am not knowing what to pass as parameter for the destructors. Could you please give me a sample code. For example reading dates from file to date objects and a destructor reading the date objects and writing them to files.
0
Comment
Question by:rmvprasad
3 Comments
 
LVL 9

Expert Comment

by:Cayce
ID: 10870972
Something like this:

class MyClass {
private:
  Node MyNode;
  string FileName;
public:
  MyClass(string FileName) {
    this->FileName = FileName;
    // open file (filename) and write info to node
  }
  void ~MyClass() {
    // write node info to file (filename)
  }
};

Destructors never accept parameters, however all the object data is still valid at the time the destructor is called (unless it;s dynamically allocated and previously released).

This looks like homework, remember EE policies.
0
 
LVL 2

Expert Comment

by:guntherothk
ID: 10871203
The purpose of a destructor is to clean up the resources allocated to a class instance before the instance is destroyed. The destructor doesn't need parameters because everything it needs to know is in the class instance. The most usual thing that happens in a destructor is to free memory or file handles owned by the instance.
0
 
LVL 5

Accepted Solution

by:
allmer earned 40 total points
ID: 10871845
I assume that 'node' would be a node in, say a linked list.
#include <list>
using namespace std;
class MyClass {
typedef list<int> nodeList;
nodeList nodes;
nodeList::iterator listPos;
CString filePath
public:
  MyClass(CString _filePath) {
    filePath = _filePath;
    double value = 0;
    File *in = fopen(filePath,"r");
    while(!feof(in)) {
       fscanf("%f\n",value);
       nodes.push_back(value);
    }
    fclose(in)
  }
  void ~MyClass() {
    double value = 0;
    File *out = fopen(filePath,"w");
    for(listPos = nodes.begin(); listPos != nodes.end(); listPos++) {
        value = listPos;
        fprintf("%f\n",value);
    }
    fclose(out);
  }
};
I didn't do error checking so that is up to you
but I think it will lead you to what you are supposed to do.
Hope it helps,
Jens
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question