Pure Virtual Functions

I'm having difficulties figuring out how to get a pure virtual function to work correctly. I have an addClass with two unsigned ints as arguments and I need to add/pass them onto credHours & qualPts but I must be missing something because I keep getting these two errors:

"cannot allocate an object of type `Student' because the following virtual functions are abstract: virtual void colMbr::addClass() const"

"cannot allocate an object of type `Student' since type `Student' has abstract virtual functions"

colMbrTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
#include "colMbrs.h"


int main()  //-------------- Driver ---------------//
{
   ColMbr *cMbr[4];     // array of base-class pointers
   int i;               // index to array
   
   // Create some college members (maybe read from a file?)
   cMbr[0] = new Student( 12345, "Steven DiFranco", 15, 33, "AA" );
   cMbr[1] = new Alumni( 98765, "Don Green", 12, 15, 1978, "AAS" ); 
   cMbr[2] = new Alumni( 24680, "Henry Thoreau", 5, 22, 1846, "AA" );
   cMbr[3] = new Student( 13579, "Millenia Best" );

   // display the array
   cout << "All college members:\n";   
   for( i = 0; i < 4; ++i )
   {
      cMbr[i]->display();
      cout << endl;
   }
   
   // test addClass pure virtual function for student
   cMbr[3]->addClass( 3, 12 );
   cMbr[3]->display();
      
   cout << endl;
   system("PAUSE");
   return 0;
}


colMbrs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <iomanip>
#include <string>
#include "date.cpp"
using namespace std;

#ifndef COLMBRS_H
#define COLMBRS_H

class colMbr{
   private:
      const unsigned idNbr;
      string name;
      
   public:
      colMbr(unsigned id = 0, const string& nm)
      :name(nm), idNbr(id){}
      //Pure Virtual Function
      virtual void addClass() const = 0;
      
      virtual void display(void) const{
         cout << name << " (" << idNbr << ")"<< endl;
            }
};

class Student : public colMbr{
   private:
      unsigned credHrs;
      unsigned qualPts;
      string degSought;
   
   public:
      Student(unsigned i, const string& n, 
              unsigned c = 0, unsigned q = 0, 
              const string& d = "Unspecified")
              : colMbr(i, n), credHrs(c), qualPts(q), degSought(d){}
           
      void addClass(unsigned int1, unsigned int2){
         credHrs = int1;
         qualPts = int2;
      }
      
      double getGPA() const{
         if (credHrs == 0){
            return 0;}
         else{
               return (double)qualPts / (double)credHrs;}
      }
            
      void display(void) const{
         colMbr::display();
         cout << "Degree Sought: " << degSought << endl
              << fixed << showpoint << setprecision(2)
              << "Current GPA:   " << getGPA()
              << endl;
      }
};

class Alumni : public colMbr, public Date{
   private:
      Date dateGrad;
      string degree;
   
   public:
      Alumni(unsigned i, const string& n,
             int m, int d, int y, 
             const string& deg = "Unspecified")
             : colMbr(i, n), dateGrad(m, d, y), degree(deg){}
      //Needed to keep class Alumni concrete?
      void addClass(void) const{}       
      
      void display(void) const{
         colMbr::display();
         cout << degree << " "
              << "(" << dateGrad << ")" 
              << endl;
      }
}; 

#endif 


Thanks in advance for any guidance you can provide!
that is because Student does not implement the addClass function. You see, addClass is declared as a function with NO arguments (line 19 of colMbrs.h). but student declares an addClass function using two unsigned ints.
you are overloading the function, but not implementing it, so Student has two functions called addClass.
1
2
void addClass(unsigned int1, unsigned int2);
virtual void addClass() const = 0;


with this, Student is an abstract class
Just a suggestion. In the declaration of your pure virtual, you declare the function as const:
virtual void addClass() const = 0;.
When you derive your student class and attempt to implement this function, you do not declare the function as const:
void addClass(unsigned int1, unsigned int2).

Try making the function prototypes exactly match.

Hope this helps.
class Student is still abstract because it has not overriden the pure virtual void addClass() const function from the colMbr base class - so you cannot create concrete
instances of the Student class like you are attempting in main()
cMbr[0] = new Student( 12345, "Steven DiFranco", 15, 33, "AA" );//Error Student is abstract class
Thanks for the quick replies. Everything seems to be good now. I changed up the addClass function in the base class and also matched up the function prototypes like Xander suggested.

1
2
3
4
5
6
7
8
9
10
11
//Base class pure virtual function declaration
virtual void addClass(unsigned c, unsigned q) = 0;

//Derived Student class function implementation
void addClass(unsigned c, unsigned q){
         credHrs = c;
         qualPts = q;
}

//Derived Alumni class function implementation to make concrete
void addClass(unsigned c, unsigned q){}


Thanks again!
Topic archived. No new replies allowed.