below is the data that i have to read from a txt file using the fstream lib
and store them in some different types of classes, its basically a pollymorphism activity.
i wrote the classese but cant find a way to read this.
i have worte some demo main ftn code, can any one help complete it with me
ps: first digit is the id of the animal, 1 for gold fish, 2 for kiwi, 3 for mamel
the second number is the x position and the third is the y position nad 4 is just the number of times for the move ftn tobe called
#This file contains animal data
#
#Data format is explained in comments
#
# fish positionx postiony isfreshwater (boolean: 0 or 1)
1 33 2 0
1 3 9 1
1 344 999 1
#
# kiwi positionx postiony canfly (boolean: 0 or 1)
2 5 5 1
2 3 7 1
2 34 99 0
# gikian positionx postiony nlegs
3 9 11 2
3 55 65 2
3 9 9 2
main ftn
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <fstream>
using namespace std;
class Animal {
public:
virtual void move() = 0;
};
class Fish: Animal {
int x, y;
bool isfreshwater;
public:
void move() {
cout << "can swim" << endl;
}
};
class Bird : Animal {
int x, y;
bool canfly;
public:
void move() {
cout << "can fly" << endl;
}
};
class Mamel : Animal {
int x, y;
int nlegs;
public:
void move() {
cout << "can move" << endl;
}
};
class Goldfish : Fish {
public:
void dine_on(int* microbes) {
cout << "eaten " << microbes << " this many microbes" << endl;
}
};
class Kiwi :Bird {
};
class gikian :Mamel {
};
class idcard {
gikian* aggregation;
int regno;
};
class heart {
string blood_group;
gikian composiotion;
};
int main(){
ifstream file("input_file.txt");
int id;
int pos_x=0, posy=0;
while(!file.eof()) {
// readid
file >> id;
// ifid == goldfish ie : 1
if (id == 1) {
Goldfish ∗ fp = new g o l d f i s h;
inputstream >> ∗(fp);//need to overload this
}
//elseifid ==kiwi, ie:2
else if (id == 2) {
Kiwi ∗kp = new Kiwi;
inputstream >> ∗(kp);//need to overload this
}
//else if id == gikian, ie:3
else if (id == 3) {
gikian ∗gp = new gikian;
inputstream >> ∗(gp);//need to overload this
}
}
file.close();
return 0;
}
| |