Class help ?

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

#include <iostream>
#include <vector>
using namespace std;

class Animal
{

	int type;

public:
	Animal()
	{
	}

	 char *identify(){return "IM A ANIMAL";}

};


class Dog :public Animal
{
	int nbarks;


public:
	Dog()
	{

	}

	char *idenify(){return "IM A DOG";}


};




class Cat:public Animal
{
	int nmeows;


public:
	Cat()
	{

	}

	char *idenify(){return "IM A CAT";}


};







int main()
{

	Cat cat;
	Dog dog;

	vector<Animal> animals;
	animals.push_back(cat);
	animals.push_back(dog);

	cout<<animals.at(0).identify()<<endl;
	cout<<animals.at(1).identify()<<endl;

cin.get()
return 1;
}


If anyone could help me with this tricky problem ill be grateful.

Basically i want to be able to place dervived objects Cat,Dog into a vector of Animal type and when i call the identify func get "IM A CAT" or "IM A DOG" etc.At the moment it just calls the base identify func which i dont want but at the same time i don't want a separate vector for cats and separate vector for dogs etc.

Any pointers,thanks

Look in to inheritance. If you were to change your base class into a pure virtual class, it would work fairly well for you. Basically, you just need to:

1) Get rid of the pointers to your members. They have their uses, but not in this case.
2) Change identify in class Animal to a virtual member.
3) Check your spelling. I saw that some of your functions aren't named properly.

http://cplusplus.com/doc/tutorial/polymorphism/
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
#include <iostream>
#include <vector>
using namespace std;

class Animal
{

	int type;

public:
	Animal()
	{
	}

	 //virtual char *identify(){return "IM A ANIMAL";}
	virtual char *identify()=0;
};


class Dog :public Animal
{
	int nbarks;


public:
	Dog()
	{

	}

	 char *identify(){return "IM A DOG";}


};




class Cat:public Animal
{
	int nmeows;


public:
	Cat()
	{

	}

	  char *identify(){return "IM A CAT";}


};







int main()
{

	Cat cat;
	Dog dog;

	vector<Animal> animals;
	animals.push_back(cat);
	animals.push_back(dog);

	cout<<animals.at(0).identify()<<endl;
	cout<<animals.at(1).identify()<<endl;

cin.get()
return 1;
}



I fixed the spelling and made the animal identify member func pure virtual however now im getting the following error C2259: 'Animal' : cannot instantiate abstract class.
Dump the pointers and get rid of the constructors.
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
class Animal{
public:
    virtual void identify()=0;
};

class Dog : public Animal{
public:
    void identify(){cout << "I'm a dog!" << endl;}
};

class Cat : public Animal{
public:
    void identify(){cout << "I'm a cat!" << endl;}
};

int main(){
    Cat cat = new Cat;
    Dog dog = new Dog;
    vector<Animal> animals;

    animals.push_back(cat);
    animals.push_back(dog);
    cout << animals.at(0).identify() << endl;
    cout << animals.at(1).identify() << endl;

    cin.ignore();
    delete cat;
    delete dog;
    return 0;
When you set a method in a clas to = 0; you are signifying that is now an abstract base class. Setting a method to = 0; defines it as a pure virtual method. (note- it must be a method and it must be vitrual to be declared = 0;)

When a class contains any number of pure virtual methods it cannot be instantiated. But it can be inherited. The catch is that any class that inherits it MUST define any pure virtual functions. So by making a base class and abstract base class you are DEMANDING that all classes that inherit it have a minimum interface because that must redefine those pure virtual methods in order for them to be instantiated.


Thx all works now!
Topic archived. No new replies allowed.