Solved

constructor and destructor

Posted on 2004-04-20
3
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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

626 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