title: C++ general inheritance misunderstanding
body: <p>I am working on the heritage and polymorphism mechanism in C++, and I have an insolvable problem from my point of view.<br>
  Actually I'm guessing, I miss something very simple but I can't figure what it is.<br>
  Can someone please help me?</p>
  
  <p>Here is the problem:</p>
  
  <p>I have a base class named <code>Article</code> and a derived class named <code>ArticleEnSolde</code> and a third class <code>Caddy</code> who manage a table of <code>Article.Article</code> contains only a <code>string</code> and an <code>int</code> and <code>ArticleEnSolde</code> have a discount system based on percentage.</p>
  
  <p><code>Caddy</code> only contains the <code>Article</code> table and have a method called <code>prixTotal()</code> which returns the sum of the article price.<br>
  But I can't figure how but all the Object are considered as <code>Article</code> instead of being considered as ArticleEnSolde`.<br>
  And the total sum is incorrect.</p>
  
  <p>Here is Article.hpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #pragma once
  
  using namespace std;
  
  class Article{
  private:
      string nom;
      float prix;
  
  public:
      Article(string = "vide",float = -44.6);
      void set_nom(string);
      void set_prix(float);
      string get_nom() const;
      virtual float getPrix() const;
      friend ostream&amp; operator&lt;&lt;(ostream&amp;,Article&amp;);
      virtual ~Article();
  };
  </code></pre>
  
  <p>Here is Article.cpp</p>
  
  <pre><code>#include "Article.hpp"
  
  Article::Article(string n,float p):nom(n),prix(p){}
  
  void Article::set_nom(string s){
      this-&gt;nom = s;
  }
  
  void Article::set_prix(float p){
      this-&gt;prix = p;
  }
  
  string Article::get_nom() const{
      return this-&gt;nom;
  }
  
  float Article::getPrix() const{
      return this-&gt;prix;
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; o ,Article&amp; a){
      o &lt;&lt; "nom article : " &lt;&lt; a.get_nom() &lt;&lt; " prix : " &lt;&lt; a.getPrix();
      return o;
  }
  
  Article::~Article(){
      cout &lt;&lt; "appel du destructeur d'article " &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Here is ArticleEnSolde.hpp</p>
  
  <pre><code>#include "Article.hpp"
  #pragma once
  
  class ArticleEnSolde : public Article{
  
  private:
      float remise;
  
  public:
      ArticleEnSolde(string,float,float);
      float get_remise() const;
      void set_remise(float);
      float getPrix();
      friend ostream&amp; operator&lt;&lt;(ostream&amp;,ArticleEnSolde&amp;);
  };
  </code></pre>
  
  <p>Here is ArticleEnSolde.cpp</p>
  
  <pre><code>#include "ArticleEnSolde.hpp"
  
  
  ArticleEnSolde::ArticleEnSolde(string s,float f,float i):Article(s,f),remise(i){}
  
  float ArticleEnSolde::get_remise() const{
      return this-&gt;remise;
  }
  
  void ArticleEnSolde::set_remise(float r){
      this-&gt;remise = r;
  }
  
  float ArticleEnSolde::getPrix(){
      cout &lt;&lt; "ArticleEnSolde " &lt;&lt; endl;
      Article * p = this;
      return p-&gt;getPrix() * (1 - this-&gt;get_remise());
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; o ,ArticleEnSolde&amp; a){
      Article * p = &amp;a;
      o &lt;&lt;"nom ArticleEnSolde " &lt;&lt; p-&gt;get_nom() &lt;&lt; " prix : " &lt;&lt; p-&gt;getPrix() &lt;&lt; " remise : " &lt;&lt; a.get_remise() &lt;&lt; " prix apres reduction " &lt;&lt; a.getPrix();
      return o;
  }
  </code></pre>
  
  <p>Here is Caddy.hpp</p>
  
  <pre><code>#include "Article.hpp"
  #include "ArticleEnSolde.hpp"
  
  class Caddy{
  private:
      Article ** tableau;
      int index;
      int taille_max;
  
  public:
      Caddy(int);
      Article** get_tab();
      int get_index() const;
      void set_index(int);
      int get_taille_max() const;
      Article* get_article(int);
      void add_article(Article&amp;);
      float prixTotal();
      friend ostream&amp; operator&lt;&lt;(ostream&amp;,Caddy&amp;);
  //  ~Caddy();
  };
  </code></pre>
  
  <p>Here is Caddy.cpp</p>
  
  <pre><code>#include "Caddy.hpp"
  
  Caddy::Caddy(int i):taille_max(i),index(0){
      tableau = new  Article *[i];
  }
  
  Article** Caddy::get_tab(){
      return this-&gt;tableau;
  }
  
  int Caddy::get_index() const{
      return this-&gt;index;
  }
  
  void Caddy::set_index(int a){
      this-&gt;index = a;
  }
  
  int Caddy::get_taille_max() const{
      return this-&gt;taille_max;
  }
  
  Article* Caddy::get_article(int a){
      return this-&gt;tableau[a];
  }   
  
  void Caddy::add_article(Article&amp; a){
      if(this-&gt;index &lt;this-&gt;taille_max){
          cout &lt;&lt; "inferieur" &lt;&lt; endl;
          this-&gt;tableau[this-&gt;index++] = &amp;a;
          cout &lt;&lt; "ajout reussi" &lt;&lt; endl;
          cout &lt;&lt; this-&gt;index &lt;&lt; endl;
      }
      else{
          cout &lt;&lt; "ajout refuse " &lt;&lt; endl;
      }
  }   
  
  float Caddy::prixTotal(){
      float somme = 0;
      for(int i =0;i&lt;this-&gt;get_index();i++){
          cout &lt;&lt; this-&gt;get_tab()[i]-&gt;getPrix() &lt;&lt; endl;
      }
      return somme;
  }   
  
  ostream&amp; operator&lt;&lt;(ostream&amp; o,Caddy&amp; c){
  
      for(int i = 0 ;i&lt;c.get_index();i++){
          o &lt;&lt; "Case " &lt;&lt; i &lt;&lt;" " &lt;&lt;  c.get_tab()[i] &lt;&lt; endl; 
      }
      return o;
  }
  
  /*
      Caddy::~Caddy(){
          cout &lt;&lt; "appel du destructeur " &lt;&lt; endl;
          for(int i = 0 ; i &lt; this-&gt;get_index();i++){
              this-&gt;get_tab()[i].~Article();
          }
      }    
  */
  </code></pre>
  
  <p>And finally here is the main code:</p>
  
  <pre><code>#include "Article.hpp"
  #include "ArticleEnSolde.hpp"
  #include "Caddy.hpp"
  
  int main(){
      Article a("a",12);
      ArticleEnSolde b("b",15,0.2);
      cout &lt;&lt; a &lt;&lt; endl;
      cout &lt;&lt; b &lt;&lt; endl;
      Caddy c(2);
      c.add_article(a);
      c.add_article(b);
      cout &lt;&lt; c &lt;&lt; endl;
      cout &lt;&lt; "prix total : " &lt;&lt; c.prixTotal() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>I want to have at the end "prix total 24" instead of 27 please someone can help me ?</p>
  
  <p>Thanks</p>