Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding virtual function not working, header files and c++ files
    text
    copied!<p>We have a parent class called Student. We have a child class: StudentCS.</p> <p>Student.h:</p> <pre><code>#include &lt;iostream.h&gt; #include&lt;string.h&gt; #include&lt;vector.h&gt; #include "Course.h" class Course; class Student { public: Student(); Student(int id, std::string dep, std::string image,int elective); virtual ~Student(); virtual void Study(Course &amp;c) const; // this is the function we have a problem with void setFailed(bool f); [...] }; </code></pre> <p>Student.cpp:</p> <pre><code>#include "Student.h" [...] void Student::Study(Course &amp;c) const { } </code></pre> <p>And we have StudentCS.h:</p> <pre><code>#include "Student.h" class StudentCS : public Student { public: StudentCS(); virtual ~StudentCS(); StudentCS (int id, std::string dep, std::string image,int elective); void Study(Course &amp;c) const; void Print(); }; </code></pre> <p>And StudentCS.cpp:</p> <pre><code>void StudentCS:: Study (Course &amp;c) const{ //25% to not handle the pressure! int r = rand()% 100 + 1; cout &lt;&lt; r &lt;&lt; endl; if (r&lt;25) { cout &lt;&lt; student_id &lt;&lt; " quits course " &lt;&lt; c.getName() &lt;&lt; endl; } } </code></pre> <p>We create student in the main:</p> <pre><code>Student *s; vector &lt;Student&gt; uniStudent; [...] if(dep == "CS") s = new StudentCS(student_id,dep,img,elective_cs); else s = new StudentPG(student_id,dep,img,elective_pg); uniStudent.push_back(*s); </code></pre> <p>Then we call to study, but we get the parent study, and not the child! Please help! </p> <p>The code compiles but when run and called on the uniStudent.Study() it uses the parent and not the child</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload