1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Inheritance vs Polymorphism & Vectors (Basics)

  1. Mar 19, 2016 #1
    1. The problem statement, all variables and given/known data

    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);

    3. The attempt at a solution

    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: Mar 19, 2016
  2. jcsd
  3. Mar 19, 2016 #2

    jedishrfu

    Staff: Mentor

    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
     
  4. Mar 19, 2016 #3
    Fantastic. Thank you for the response and additional discussion links.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Inheritance vs Polymorphism & Vectors (Basics)
  1. Polymorphism Question (Replies: 1)

  2. Basic base (Replies: 4)

  3. Basic Vector Problem (Replies: 2)

Loading...