Inheritance vs Polymorphism & Vectors (Basics)

  • Thread starter Thread starter AKJ1
  • Start date Start date
  • Tags Tags
    Basics Vectors
Click For Summary
SUMMARY

The discussion focuses on the concepts of inheritance and polymorphism in C++, specifically through the provided code snippet involving the Shape class. The Shape class demonstrates inheritance due to its use of a virtual method, drawPoints(), which is defined in a parent class. Additionally, the comparison between an array (int list[10]) and a std::vector illustrates the differences in memory management and capabilities, with std::vector providing more functionality despite additional overhead.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of object-oriented programming concepts
  • Knowledge of STL (Standard Template Library) and std::vector
  • Familiarity with virtual functions in C++
NEXT STEPS
  • Study C++ inheritance and polymorphism in depth
  • Learn about the Standard Template Library (STL) and its containers
  • Explore memory management differences between arrays and std::vector
  • Investigate best practices for using virtual functions in C++
USEFUL FOR

Self-taught programmers, C++ developers, and students learning object-oriented programming concepts who seek to understand the differences between inheritance and polymorphism, as well as the advantages of using std::vector over traditional arrays.

AKJ1
Messages
43
Reaction score
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
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   Reactions: AKJ1
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K