Every time i try to call a function from a derived class the program keeps crashing. The function is first declared as a pure virtual function, but then it is defined in the derived class and becomes instatiated. But when i call the function, the program just crashes.. what am i doing wrong?
#include <iostream>
#include <fstream>
#include "Transform and original class.h"
usingnamespace std;
int main ()
{
OriginalFile *pointer1;
fstream file;
string name;
cout << "Please type in a name of a file" << endl;
getline (cin, name);
file.open (name.c_str(), ios::in | ios::out | ios::app);
if (file.is_open())
cout << "File has been opened" << endl;
else
cout << "Error" << endl;
pointer1->readFile (file); //Here is where the program keeps crashing
}
#define TRANSFORM AND ORIGINAL CLASS_H
#include "FileFilter class.h"
#include <fstream>
#include <iostream>
usingnamespace std;
//this class will transform the content in the file to all uppercase
class Transform : public FileFilter
{
char lines [100000];
public:
virtualvoid transform (fstream &file)
{
int n = 0;
int m = 0;
int b = 0;
char byte;
while (!file.eof())
{
file.get(byte);
lines[n] = byte;
n++;
}
file.seekg (0, ios::beg);
while (!file.eof())
{
lines[m] = toupper(lines[m]);
m++;
}
file.seekg (0, ios::beg);
while (!file.eof())
{
cout << lines[b];
b++;
}
}
};
//This is Class that will hold the original copy of the file
class OriginalFile : public FileFilter
{
char lines_ [100000];
public:
virtualvoid readFile (fstream & file1)
{
int n = 0;
int m = 0;
char byte;
while (!file1.eof())
{
file1.get(byte);
lines_[n] = byte;
n++;
}
file1.seekg (0, ios::beg);
while (!file1.eof())
{
cout << lines_[m];
m++;
}
}
};
I tried to declare a class object from one of the derived classes so that i can assign its address to the pointer, but the compiler is not letting me. Did i instatiate the pure virtual functions properly?
Nevermind, I fixed the problem. It turned out I was supposed to define all the pure virtual functions in all derived classes in order for me to create an object.