Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

constructor and destructor

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

715 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