Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class template

Status
Not open for further replies.

akashvij

Technical User
Mar 11, 2002
19
US
hello everybody
This is my first time here. I am working on a class project on class template. i have to give member function definition. Plz help me i am not able to understand what i am suppose to do. Plz give me some hints or explanation about it.Member function is

bool readFromFile(char *fileName)-> read data of type t from the file named 'filename',and store the data into the array.Returns true if reading was successful,false otherwise.
and i have one more member function
bool printIntoFile(char *fileName)->dump the contents of the array into a file named 'filename'.Return true if writing was succesful, false otherwise.
 
I've given you the structure of a template class... It's up to you to code the reading and the writing.

hint:
while redaing or writing... remember that you don't know the type of data... you should use TYPE when declaring variables...

examples:
int i; //bad
cin >> i;

TYPE i; //good
cin >> i;

/---------------------------------------------/

// Main.cpp
#include "Container.h"

void main(){
Container<int> ctnr;
ctnr.readFromFile(&quot;in.txt&quot;);
ctnr.printIntoFile(&quot;out.txt&quot;);
}

/---------------------------------------------/

// Container.h
#ifndef _Container_h_
#define _Container_h_

template <class TYPE>
class Container{
protected:
TYPE arr[1024]; // choose an appropriate number.
public:
bool readFromFile(char *fileName);
bool printIntoFile(char *fileName);
};

#include &quot;Container.cpp&quot;

#endif

/---------------------------------------------/

// Container.cpp
#ifndef _Container_cpp_
#define _Container_cpp_

#include &quot;Container.h&quot;

template <class TYPE>
bool Container<TYPE>::readFromFile(char *fileName){
bool returnValue=true;
// your code to read the file
return returnValue;
}

template <class TYPE>
bool Container<TYPE>::printIntoFile(char *fileName){
bool returnValue=true;
// your code to write the file
return returnValue;
}

#endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top