need help with pollymorphism

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;
}



Last edited on
cant find a way to read this.

A big issue is that there are two types of lines: comments and entries.
Every entry appears to have four integers.
Comment starts with hash (#).

One solution:
1
2
3
4
5
6
7
8
9
10
11
std::string line;
while ( std::getline( file, line ) ) {
  if ( ! line.empty() and '#' != line[0] ) {
    // line is not a comment
    std::istringstream iss( line );
    int type{}, x{}, y{}, z{};
    if ( iss >> type >> x >> y >> z ) {
      // create new something with (x,y,z)
    }
  }
}
thanks it did solved my proplem but now i have to input these data to their corresponding classes
like this in one;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Fish: Animal {
    int x, y;
    bool isfreshwater;
public:
    void move() {
        cout << "can swim" << endl;
    }
};

class Goldfish : Fish {
public:
    friend istream operator >> (istream& lhs, Goldfish rhs);
};
istream operator >> (istream& lhs, Goldfish rhs) {
    Fish* obj = &rhs;
    lhs >>  //incomplete
}

now i dont know how to acces those x and y and change it by overloading ">>" operator

First, you should use class Derived: public Base to do your pollymorphism things.
It allows you to do some implicit-convertions from Derived& to Base&(or from Derived* to Base*).
You might design your goldfist like this:
1
2
3
4
5
6
7
class Goldfish : public Fish {
public:
    friend istream& operator >> (istream& lhs, const Goldfish& rhs);
};
istream& operator >> (istream& lhs, const Goldfish& rhs) {
    lhs >> rhs.x >> rhs.y;  //Or other more complicated things
}

Notice that the '&' after the class name means "reference", aliasing, and without copying.
Last edited on
For istream >> overload, don't use const for the target - otherwise the compiler will complain about trying to amend a const!

For const for the source for the ostream << overload.
Topic archived. No new replies allowed.