C++ Understanding Constructors

I have successfully gotten the info to print out in the correct order.

However, I cannot figure out how to implement getCount.

Help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <istream>
#include <fstream>

using namespace std;

class Animal
{
	string species;
                string family;
                string phylum;
				string desc;
				int count;
public:
	bool readIn(ifstream&file, const string frame);
	void printInfo() const;
	void setAnimal(string s, string f, string p, string d);
	static int getCount(int count);
	Animal(string s, string f, string p, string d);
	Animal(ifstream& file, const string fname);
	Animal(); //Default Constructor
};

Functions:
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
#include "Animal.h"
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
using namespace std;
Animal::Animal(){
    species="ape";
    family="ape";
    phylum="ape";
    desc="ape";
    count = 1;
}
Animal::Animal(string s, string f, string p, string d){
    species=s;
    family=f;
    phylum=p;
    desc=d;
}
Animal::Animal(ifstream& file, const string fname)
{
    readIn(file, fname);
}
int Animal::getCount(int count)
{	
	    int i;
        i++;
		cout << count << endl;
        return i;
}
bool Animal::readIn(ifstream &myFile, const string fname)
{
        myFile.open(fname);
        if(myFile)
        {
                getline(myFile, species);
                getline(myFile, family);
                getline(myFile, phylum);
                getline(myFile, desc);
                myFile.close();
                return true;
        }
		else
        return false;
}
void Animal::printInfo() const
{
cout << species << endl;
cout << family << endl;
cout << phylum << endl;
cout << desc << endl;
}
void Animal::setAnimal(string s, string f, string p, string d)
{
        species = s, family = f, phylum = p, desc = d;
}
int main()
{
	ifstream myFile;
        Animal a;
        Animal b("homo sapien", "primate", "chordata", "homo erectus");
        Animal c(myFile, "horse.txt");
        a.printInfo();
        b.printInfo();
        c.printInfo();
		//cout << c.count << endl;

		system("PAUSE");
		return 0;
}
Last edited on
You should report error messages. I see for example that you missed header <fstream>.

Or another question what is the meaning of method getCount?! Does it serve to increase its argument?
Last edited on
getCount is just supposed to increase every time a new animal object is made.
But it increases a local variable instead of the data member with the same name.:)

If I understand correctly it would be better to define data member count as static.
Last edited on
How would I go about doing that?
By the way these statements are invalid

1
2
3
        Animal a();
        Animal c();
        a().printInfo();


Animal a(); and Animal c(); are function declarations.

a().printInfo(); means calling of the function operator which was not defined in your class.
Topic archived. No new replies allowed.