Inheritance vs Polymorphism & Vectors (Basics)

In summary, the first code snippet is an example of inheritance, where the Shape class inherits the virtual method drawPoints() from a parent class. The second code snippet shows the difference between an array of 10 integers and a std::vector that manages a list of 10 integers, with the latter providing extra capabilities but also involving extra code.
  • #1
AKJ1
43
0

Homework Statement


[/B]
Hi all, these were two even numbered exercises in my C++ textbook. I am self teaching the language so I am trying to get some of the basics down.

1. Would the following snippet of code best be described as an example of Polymorphism or Inheritance?

class Shape { public: void draw();
protected:
Shape();
virtual void drawPoints() const = 0;
private:
std::vector points;
};

2. What is the difference between

int list[10];
std::vector<int> list(10);

The Attempt at a Solution


[/B]
My answer to the first question is inheritance, but if I am incorrect please let me know, thank you.

The second question I am sort of lost on, the second piece of code creates a vector of 10 ints called list. What does "int list[10]" do?

Thank you!
 
Last edited:
Physics news on Phys.org
  • #2
int list[10];

is an array of 10 integers. The data is stored sequentially in memory in the most efficient manner.

whereas the other is using a class std:vector to manage a list of 10 integers. There is extra code
involved and extra capability provided by using the std:vector scheme.

Here's a discussion on why some programmers prefer the int list[10] approach over the std:vector
approach:

http://lemire.me/blog/2012/06/20/do-not-waste-time-with-stl-vectors/

In the first example, I guess the virtual method means that the Shape class gets drawPoints(0 from a parent class and hence is an example of inheritance.

Here's some discussion on virtual methods:

https://en.wikipedia.org/wiki/Virtual_function
 
  • Like
Likes AKJ1
  • #3
jedishrfu said:
int list[10];

is an array of 10 integers. The data is stored sequentially in memory in the most efficient manner.

whereas the other is using a class std:vector to manage a list of 10 integers. There is extra code
involved and extra capability provided by using the std:vector scheme.

Here's a discussion on why some programmers prefer the int list[10] approach over the std:vector
approach:

http://lemire.me/blog/2012/06/20/do-not-waste-time-with-stl-vectors/

In the first example, I guess the virtual method means that the Shape class gets drawPoints(0 from a parent class and hence is an example of inheritance.

Here's some discussion on virtual methods:

https://en.wikipedia.org/wiki/Virtual_function

Fantastic. Thank you for the response and additional discussion links.
 

Related to Inheritance vs Polymorphism & Vectors (Basics)

What is the difference between inheritance and polymorphism?

Inheritance is a concept in object-oriented programming where a class can inherit properties and methods from a parent class. This allows for code reuse and the creation of a hierarchy of classes. Polymorphism, on the other hand, is the ability for an object to take on different forms or behaviors depending on the context. In other words, polymorphism allows for different objects to have different implementations of the same method.

How do inheritance and polymorphism work together?

Inheritance and polymorphism often go hand in hand. Inheritance allows for the creation of a hierarchy of classes where each child class inherits properties and methods from its parent class. Polymorphism then allows for these inherited methods to be overridden or extended in the child classes, creating different behaviors for each child class.

What are the benefits of using vectors in programming?

Vectors, also known as dynamic arrays, have several benefits in programming. First, they allow for the storage of a variable number of elements, making them more flexible compared to traditional arrays. They also have built-in functions for adding, removing, and accessing elements, making them easier to work with. Additionally, vectors have efficient memory management, automatically resizing when needed to prevent memory leaks.

What is the difference between vectors and arrays?

While both vectors and arrays can store a collection of elements, there are some key differences between them. Arrays have a fixed size, meaning they cannot be resized once they are created. Vectors, on the other hand, have a dynamic size and can grow or shrink as needed. Additionally, arrays are typically faster for accessing elements, while vectors have built-in functions for adding and removing elements.

How can vectors be used in inheritance and polymorphism?

Vectors can be used in inheritance and polymorphism by storing objects of different child classes in the same vector. This allows for polymorphic behavior, as the vector can hold different types of objects that all inherit from a common parent class. This can be useful for creating collections of objects with different behaviors or for implementing a data structure that can hold different types of data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
997
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Back
Top