Solved

constructor and destructor

Posted on 2004-04-20
3
509 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now