trouble with fstream and polymorphism
Oct 7, 2013 at 2:08am UTC
I'm trying to output the data onto a text file
Im using polymorphism(virtual and abstract class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
#include <fstream>
using namespace std;
class Enemy{
protected :
int attackPower;
public :
virtual void attack()=0;
ofstream myfile;
myfile.open ("Enemy.txt" ); // Keep getting an error
myfile<< "This is a line" ;
myfile.close();
};
class Ninja: public Enemy{
public :
void attack()
{
cout<<" Ninja Chop " <<endl;
}
};
class Monster: public Enemy{
public :
void attack()
{
cout<<" Monster attack " <<endl;
}
};
int main()
{
Ninja n;
Monster m;
Enemy *enemy1 = &n;
Enemy *enemy2 = &m;
enemy1 -> attack();
enemy2 -> attack();
system("pause" );
return 0;
}
I need the output to be shown on a text file.
Oct 7, 2013 at 2:38am UTC
Lines 16 to 19 have to be in a function, you can't just write arbitrary code inside a class like that.
Oct 7, 2013 at 7:19am UTC
Oh so, it could be a function within a class.
Okay now that its settled, I want to get the output
from the command prompt and also have it in the
text file, would I just do;
1 2 3 4 5
// Function
ofstream myfile;
myfile.open ("Enemy.txt" );
myfile<< attack() ;
myfile.close();
Topic archived. No new replies allowed.