The art of Polymorphism

How do I merge Polymorphism and file streams,
I need to print the output onto a file stream, but nothing seems to happen

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
57
58
59
60
61
  #include <iostream>
#include <fstream>


using namespace std;

class Enemy{
      protected:
              int attackPower;
              ofstream myfile;
               
      public:
           virtual  void attack(){
                    
                     myfile.open ("Output.txt");
                     myfile << /* Lost here*/;
                     myfile.close();
           }
           
           void setAttackPower(int);
           
};

void Enemy::setAttackPower(int a )
{
     attackPower = a;
}

class Ninja: public Enemy{
      public:
             void attack()
             {
                  cout<<" Ninja Chop -"<< attackPower<<endl;     
             }
};

class Monster: public Enemy{
      public:
             void attack()
             {
                  cout<<" Monster attack -"<< attackPower<<endl;     
             }      
      
};

int main()
{
    
    Ninja n;
    Monster m;
    Enemy *enemy1 = &n;
    Enemy *enemy2 = &m;
    enemy1 -> setAttackPower(20);
    enemy2 -> setAttackPower(30);
    enemy1 -> attack();
    enemy2 -> attack();
    
    system("pause");
    return 0;
}


the outputs from the program should be in a text file..
Thanks
how about instead of cout you just put myfile?
Alrite, so you mean,

1
2
3
4
5
6
7
class Ninja: public Enemy{
      public:
             void attack()
             {
                  myfile<<" Ninja Chop -"<< attackPower<<endl;     
             }
};


instead of cout, myfile.
It compiles perfectly but the "Ninja Chop-20" is missing from the command prompt
and the text file was not created.
Any other options. Thanks
Yes but don't in your constructor you need to open a file with your ofstream.
And if you want it in the command promp also then use cout and myfile streams. So it sends to the txt and console. Sorry for the late response was busy.
Thanks,its okay.
wait, so you're telling me not to put in the constructor
First, I need to open a file and then start putting my output in there..
Is that what you're trying to say..?
No and Yes. In the constructor of Enemy, initialize the ofstream object. For example, here is your (slightly modified) Enemy class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Enemy {
    protected:
        int attackPower;
        std::ofstream myfile;

        Enemy() : myfile("Outfile.txt") {}

    public:
        virtual ~Enemy() {
            myfile.close();
        }

        virtual void attack(void) = 0;
        
        void setAttackPower(int atk) {
            attackPower = atk;
        }
}


Then, for everything else, simply use myfile as the output. If you want the text to show in the console too, you can use both cout and myfile.
if you want it in the command promp also then use cout and myfile streams.

You could use a tee in this case.

For example, Boost's TeeDevice (from Boost.Iostreams)

C++ “hello world” Boost tee example program
http://stackoverflow.com/questions/999120/c-hello-world-boost-tee-example-program

Or this stand alone implementation:

Tee command?
http://www.daniweb.com/software-development/cpp/threads/326447

which I posted more about here:
http://www.cplusplus.com/forum/windows/59895/#msg323582

Andy

Boost.Iostreams
http://www.boost.org/doc/libs/1_54_0/libs/iostreams/doc/index.html
Last edited on
Topic archived. No new replies allowed.