Need help with vector

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class CollegeStudent { 
 public: 
 string get_name(); 
 virtual void take_test() = 0; 
 virtual string play_sports() = 0; 
 protected: 
 string name; 
}; 
class TrojanStudent : public CollegeStudent { 
 public: 
 void take_test() { cout << "Got an A."; } 
 string play_sports(){return string("WIN!");} 
}; 
class BruinStudent : public CollegeStudent { 
 public: 
 void take_test() { cout << "Uh..uh..C-."; } 
 string play_sports(){return string("LOSE");} 
}; 
int main() { 
 vector<CollegeStudent *> mylist; 
 mylist.push_back(new TrojanStudent()); 
 mylist.push_back(new BruinStudent());
  .... 


I am not sure why my instructor wrote vector<CollegeStudent *> mylist. COuld anyone explain? Thanks
Can you answer yourself any of these:

What was the topic of the lesson?
What is the purpose of the virtual?
What are inheritance and polymorphism?
What is object slicing?
The topic is about polymorphism. it seems strange to me when the content of the vector points to the address. I didn't use it more often. This is what I understands with my limit knowledge
Vector holds elements of some type. Vector allocates memory for storing those elements. The size per element is fixed.

If you have a CollegeStudent in an element, that is what you have. You cannot assign TrojanStudennt to that space.

If you have a pointer in an element, it does not matter how big object it is pointing to. Additionally, copying pointers is much cheaper than copying complex objects.
Topic archived. No new replies allowed.