Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hallo
I am new to c++ and learning this Lists and Iterators from quite a few days.
I am stuck and confused now.
First what I got to know abouts Lists and Iterators were
Lists ist just like a normal list in which you can push your objects or values(strings, int and so on) like we do in stack
Iterators are just to poitn out the stuff we pushed in and we can also know what we pushed up just by dereferecing it.
Now my question is Why some people put a pointer with a datatype sometimes. For eg
std::list<animals*> animal_list; // where animal is my class
std::list<animals*> ::iterator iter; //
We do this to access the pointers to the class animals for eg I tried making this code
C++
#include <stdio.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Animals
{
public:
	void getName(){
		cout << "this is a"<< *it;
	}
};
class Dog : public Animals{
public:
	Dog(string dog){
		cout << "the breed of the dog is"<< dog<< endl ;
	}
};
class Cat : public Animals{
public:
	Cat(string colour){
		cout << "the color of the cst is"<<colour<<endl;
	}
};
class Rabbit : public Animals{
public:
	Rabbit(string colour){
		cout << "the color of the rabbit is"<< colour<<endl;
	}
};
int main()
{
	list<animals*> animals;
	Dog *d = new Dog("labrador");
	Cat *c = new Cat("braun");
	Rabbit *r = new Rabbit("black n white");
	animals.push_back(d);
	animals.push_back(c);
	animals.push_back(r);
	list<animals*>::iterator it;
	for (it = animals.begin(); it != animals.end(); ++it){
		cout << (*it)->getName(); // here I'm gettng error because of << operands. 
	}	
}

In this case I made 3 derived classes and all these are inheriting animals class. My main purpose is to access the function getName() declared in base class.
Now I made three pointers pointing to these classes. And then I pushed them up in the list in order to have the dog, cat and rabbit in my list. I don't know if I'm right or wrong.
I am really screwed up.Its not getting in my head.
Please help and sorry for such a lng description. I just wanted to clearly explain myself
Thanks
P.s : Please don't get confuse after reading it.
Posted
Updated 15-Jul-15 23:46pm
v2

You are calling getName() on your Animals pointer, but that function calls some iterator that is not defined in the class. You should change your classes to something like:
C++
class Animal
{
protected string _animal;
public:
    Animal(string type)
    {
        _animal = type;
    }

};
class Dog : public Animal
{
public:
    string getName()
    {
        return _animal + " is a dog";
    }
};
 
Share this answer
 
Comments
Member 11841297 16-Jul-15 6:47am    
you are declaring getName fucntion in class dog instead of doing it in Base class. And then how would I define it in main class.?
and do I need to declare the function in a1l classes?.
Richard MacCutchan 16-Jul-15 6:59am    
Yes, because each Animal returns a different string for getName() that includes the Animal type. This is just an example of how it can be done, not how it must be done. If you do not declare it in all classes then you need a version in the Animal class that provides the default answer.

You need to spend more time learning the basics about classes before worrying about Lists and iterators.
Possibly you mean something like:
C++
 #include <iostream>
 #include <list>
 #include <string>
 using namespace std;


class Animals
{
public:
  virtual void showName(){
    cout << "this is a animal" << endl;
  }
  virtual ~Anumals(){}
};
class Dog : public Animals{
  string breed;
public:
  Dog(string breed):breed(breed){}
  void showName()
  {
    cout << "the breed of the dog is " << breed << endl ;
  }
};
class Cat : public Animals{
string colour;
public:
  Cat(string colour):colour(colour){}
  void showName()
  {
    cout << "the colour of the cat is " << colour << endl;
  }
};
class Rabbit : public Animals{
string colour;
public:
  Rabbit(string colour):colour(colour){}
  void showName()
  {
    cout << "the colour of the rabbit is "<< colour << endl;
  }
};
int main()
{
  list<Animals*> animals;
  Dog *d = new Dog("labrador");
  Cat *c = new Cat("braun");
  Rabbit *r = new Rabbit("black n white");
  animals.push_back(d);
  animals.push_back(c);
  animals.push_back(r);
  list<Animals*>::iterator it;
  for (it = animals.begin(); it != animals.end(); ++it){
    (*it)->showName(); 
  }
  for (it = animals.begin(); it != animals.end(); ++it)
    delete (*it);
}
 
Share this answer
 
Comments
Member 11841297 16-Jul-15 7:18am    
Sorry but your virtual void showname() in base class i.e Animals is doing nothing. And why you wrote like this Dog(string breed):breed(breed){} for every class?
Sorry once again my question is first of all to is how we use class * in our list as datattype for eg.
std:: list<animals *=""> animal instead of std:: list<animals> animal. Whats this * is doing.
And second how can we show the complete list through iterator?
CPallini 16-Jul-15 7:41am    
Yes it does nothing, because of polymorphism (that's why we are using pointers instead of values).
I initialized a data member in the constructor.
My code already shows the complete list.
Richard MacCutchan 16-Jul-15 8:03am    
Your code is a bit too advanced for OP.
CPallini 16-Jul-15 8:05am    
Yes, it might be. However, the very point of using pointers (pardon the pun) instead of values is for exploiting polymophism.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900