Chuyển tới nội dung
Trang chủ » C++ Vector Of Base Class Update

C++ Vector Of Base Class Update

An Efficient Polymorphic Data Structure - 1.68.0

You are looking for information on the topic “c++ vector of base class”. dongtienvietnam.com will gather information to answer your questions in this article. Please read this article to the end to find the information you want to see. You can also check out some other related articles here: https://dongtienvietnam.com/category/wiki/ vector of derived classes – c++, c++ vector of base class pointers, vector of unique pointers, c++ vector of child classes, c++ cast vector of derived*> to vector of base, unique_ptr, make_unique template

View information about the c++ vector of base class

Information about this question

  • Question: c++ vector of base class
  • Time: Askeded 104 hours ago
  • Modified: Asked 44 minute ago
  • View: 5722

c++ vector of base class

In C++, you can create a vector of base class pointers. Here’s an example:

c
#include <vector> #include <iostream> class Animal { public: virtual void speak() { std::cout << "I am an animal." << std::endl; } }; class Cat : public Animal { public: void speak() override { std::cout << "Meow!" << std::endl; } }; class Dog : public Animal { public: void speak() override { std::cout << "Woof!" << std::endl; } }; int main() { std::vector<Animal*> animals; animals.push_back(new Cat()); animals.push_back(new Dog()); for (Animal* animal : animals) { animal->speak(); } for (Animal* animal : animals) { delete animal; } animals.clear(); return 0; }

In this example, we define a base class Animal, and two derived classes Cat and Dog. We then create a vector animals of pointers to Animal objects. We add a Cat and a Dog object to the vector using push_back().

We then iterate over the vector and call the speak() function on each object. Since speak() is a virtual function, the correct version of the function is called based on the type of the object.

Finally, we delete each object and clear the vector to avoid memory leaks.

Watch videos related to “c++ vector of base class”

[Khóa học lập trình C++ Cơ bản] – Bài 52: Cơ bản về lớp Vector | HowKteam

Images related to c++ vector of base class

Found 33 c++ vector of base class related images.

Virtual Base Class In C++ - Geeksforgeeks
Virtual Base Class In C++ – Geeksforgeeks
An Efficient Polymorphic Data Structure - 1.68.0
An Efficient Polymorphic Data Structure – 1.68.0
Inheritance In C++ And It'S Types [Explained] With Example Program
Inheritance In C++ And It’S Types [Explained] With Example Program
Ctfidf - Bertopic
Ctfidf – Bertopic
An Efficient Polymorphic Data Structure - 1.68.0
An Efficient Polymorphic Data Structure – 1.68.0

vector of derived classes – c++

In C++, a vector can store objects of a derived class, just like it can store objects of a base class or any other data type. Here is an example of how to create a vector of objects of a derived class:

c++
#include <iostream> #include <vector> using namespace std; class Animal { public: virtual void speak() { cout << "I am an animal." << endl; } }; class Dog : public Animal { public: void speak() { cout << "I am a dog." << endl; } }; class Cat : public Animal { public: void speak() { cout << "I am a cat." << endl; } }; int main() { vector<Animal*> animals; animals.push_back(new Dog()); animals.push_back(new Cat()); for (int i = 0; i < animals.size(); i++) { animals[i]->speak(); } for (int i = 0; i < animals.size(); i++) { delete animals[i]; } return 0; }

In this example, we have a base class called Animal and two derived classes called Dog and Cat. We create a vector of Animal pointers, and we add two objects to it: a Dog and a Cat. We then iterate over the vector and call the speak() method for each object. Since speak() is a virtual method in the Animal class, it will be called polymorphically for each derived class.

It’s important to note that when you add objects to a vector like this, you’re actually adding pointers to those objects. So when you’re done with the vector, you need to delete the objects pointed to by those pointers, as shown in the example above.

c++ vector of base class pointers

In C++, a vector of base class pointers can be created using the following syntax:

c
std::vector<BaseClass*> myVector;

This creates an empty vector of pointers to the BaseClass. To add objects to the vector, you can use the push_back method:

scss
BaseClass* obj1 = new DerivedClass1(); BaseClass* obj2 = new DerivedClass2(); myVector.push_back(obj1); myVector.push_back(obj2);

Note that DerivedClass1 and DerivedClass2 are classes that inherit from BaseClass.

To access the objects in the vector, you can use a loop:

css
for (int i = 0; i < myVector.size(); i++) { myVector[i]->method(); }

This will call the method() function for each object in the vector.

It’s important to remember that when using pointers to objects, you must manually manage memory, including deleting objects that you have allocated with new. You can do this using the delete keyword:

css
for (int i = 0; i < myVector.size(); i++) { delete myVector[i]; }

This will delete each object in the vector, including those that were created with new.

You can see some more information related to c++ vector of base class here

Comments

There are a total of 477 comments on this question.

  • 564 comments are great
  • 127 great comments
  • 468 normal comments
  • 151 bad comments
  • 69 very bad comments

So you have finished reading the article on the topic c++ vector of base class. If you found this article useful, please share it with others. Thank you very much.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *