Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

constructor and destructor

Posted on 2004-04-20
3
Medium Priority
?
519 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 160 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

824 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