Skip to content

Instantly share code, notes, and snippets.

@CodingDino
Created April 18, 2024 21:16
Show Gist options
  • Save CodingDino/d403a25293c3dd1b69ff21db0fa2a442 to your computer and use it in GitHub Desktop.
Save CodingDino/d403a25293c3dd1b69ff21db0fa2a442 to your computer and use it in GitHub Desktop.
OOP Classes and Cohesion Example - Dogs - Part 5 - Classes and Vectors
// When we add Dogs to this kennel, they are cloned! (copied in to the vector)
vector<Dog> cloningKennel;
cloningKennel.push_back(myDog1);
cloningKennel.push_back(myDog2);
cloningKennel.push_back(myDog3);
// Loop through the vector just like we learned before
for (int i = 0; i < cloningKennel.size(); ++i)
{
// can use the subscript operator [] just as normal to get the specific item in the vector,
// and use . after it to do something with that item!
cloningKennel[i].Print();
}
// This kennel only stores the ADDRESS to the Dog, it does not copy it!
vector<Dog*> normalKennel;
normalKennel.push_back(&myDog1);
normalKennel.push_back(&myDog2);
normalKennel.push_back(&myDog3);
// Loop through the vector just like we learned before
for (int i = 0; i < normalKennel.size(); ++i)
{
// can use the subscript operator [] just as normal to get the specific item in the vector,
// and use -> after it to do something with that item, since it's a pointer so we use ->
normalKennel[i]->Print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment