Program with Virtual Functions crashes when run?

Hi, i've made a program for a college system which allows users to enter in student details and view the grade levels of the student. I have used a base and derived class and have also used virtual functions and pointers in my program to get the grade level. The program compiles sucessfully but when it runs, the output screen crashes?

Heres the code for the base class:

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

class HNDstudent 
{
public:
HNDstudent(string isurname, char iinitial, double iID, int ilevel, 
string iqual_title, int iyear, string igrade)
{

surname=isurname;
initial=iinitial;
ID=iID;
level=ilevel;
qual_title=iqual_title;
year=iyear;
final_grade=igrade;
}

~HNDstudent(){}

virtual void Set_surname(string sur) 
{
cout<<"what is the student's surname?";
cin>>sur; 
surname=sur;
}

virtual void Set_initial(char init)
{
cout<<"what is the student's initial?";
cin>>init;
initial=init;
}

virtual void Set_ID(double an_ID)
{
cout<<"what is the student's ID?";
cin>>an_ID;
ID=an_ID;
}


virtual void Set_level (int a_level)
{
cout<<"what is the qualification level?";
cin>>a_level;
level=a_level;
}

virtual void Set_qual_title (string qual)
{
cout<<"what is the qualification title- ?";
cin>>qual;
qual_title=qual;
}

virtual void Set_year (int a_year)
{
cout<<"what year will the student achieve?";
cin>>a_year;
year=a_year;
}

virtual void Set_grade(string a_grade)
{
cout<<"what is the final grade for the student- Pass or Fail?";
cin>>a_grade;
final_grade=a_grade;
}

string Get_surname() 
{
return surname;
}

char Get_initial()
{
return initial;
}

double Get_ID()
{
return ID;
}

string Get_qual_title()
{
return qual_title;
}

int Get_level()
{
return level;
} 

int Get_year()
{
return year;
}

string Get_grade()
{
return final_grade;
}


private: 
         string surname;
         char initial;
         double ID;
         int level;
         string qual_title;
         int year;
         string final_grade;

};


Heres the code for the derived class

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

class NatDipstudent : public HNDstudent
{
public: 
       
NatDipstudent(string isurname, char iinitial, double iID, int ilevel, 
string iqual_title, int iyear, string igrade)
{
 surname=isurname; 
 initial=iinitial;
 ID=iID;
 level=ilevel;
 qual_title=iqual_title;
 year=iyear;
 final_grade=igrade;
}
          
       
~NatDipstudent(){}

void Set_surname(string sur) 
{
cout<<"what is the student's surname?";
cin>>sur;     
surname=sur;
}

void Set_initial(char init)
{
cout<<"what is the student's initial?";
cin>>init;
initial=init;
}

void Set_ID(double an_ID)
{
cout<<"what is the student's ID?";
cin>>an_ID;
ID=an_ID;
}

void Set_level (int a_level)
{
cout<<"what is the qualification level?";
cin>>a_level;
level=a_level;
 }
void Set_qual_title (string qual)
{
cout<<"what is the qualification title- ?";
cin>>qual;
qual_title=qual;
}

void Set_year (int a_year)
{cout<<"what year will the student achieve?";
cin>>a_year;
year=a_year;
}

void Set_grade(string a_grade)
{
  int total=0;
    string grade;
        
    int units[18]={1,2,3,5,6,7,10,12,13,17,1,19,20,24,25,26,42,43};
    int points[18];
   string title[18]={"C&E", "Computer Systems","IS", 
   "ADbs", "ASs", "SAD","Client Side","Games", "HCI", "Maths", "pSDD", "Web Server Scripting", 
   "ED", "Graphics", "OO", "Animation", "CCAN1", "CCNA2" };
   int i;
   
   for(i=0; i<18; i=i+1)
   {
   cout<<"Enter grade for unit  "<< units[i] <<"  " << title[i]<<"  ";
   cin>>points[i];
   }
   for(i=0; i<18; i=i+1)
   {
   total=points[i]+total;
   }
  
   cout<<endl;
   
   if(total>251)
   {grade="DDD";}
   else if (total>227)
   {grade="DDM";}
   else if (total>203)
   {grade="DMM";}
   else if (total>179)
   {grade="MMM";}
   else if (total>155)
   {grade="PMM";}
   else if (total>131)
   {grade="PPM";}
   else if (total>107)
   {grade="PPP";}
   else if (total<108)
   {grade="fail";}
   
   
   cout<<"Total points accrued is "<<total<<endl;
   cout<<"Your grade is "<<grade<<endl;
   a_grade=grade;
    

final_grade=a_grade;
}
string Get_surname(){return surname;} 
char Get_initial(){return initial;}
double Get_ID(){return ID;}
string Get_qual_title(){return qual_title;}
int Get_level() {return level;}
int Get_year(){return year;}
string Get_grade(){return final_grade;}

private: 
        string surname; 
        char initial;
        double ID;
        int level;
        string qual_title;
        int year;
        string final_grade;
};


And heres the code for main:


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
#include <cstdlib> 
#include <iostream>
#include "hnd.h" 

int main(int argc, char *argv[])
{
string sname, sQT,g; //objects
double sID;
char si;
int sQL,sY;


HNDstudent s2009("x", 'x', 000, 0, "x", 0, "incomplete"); 
HNDstudent *s[2]; 

s[0] = new HNDstudent("x", 'x', 000, 0, "x", 0, "incomplete");
s[1] -> Set_level(10); 

s2009.Set_surname(sname);
s2009.Set_initial(si);
s2009.Set_ID(sID);
s2009.Set_level(sQL);
s2009.Set_qual_title(sQT);
s2009.Set_year(sY);
s2009.Set_grade(g);


cout<<s2009.Get_surname()<<endl; 
cout<<s2009.Get_initial()<<endl;;
cout<<s2009.Get_ID()<<endl;;
cout<<s2009.Get_level()<<endl;;
cout<<s2009.Get_qual_title()<<endl;;
cout<<s2009.Get_year()<<endl;
cout<<s2009.Get_grade()<<endl;

cout << s[1] -> Get_level(); // points to function Get_ID from the HNDstudent class 



system("PAUSE");

return EXIT_SUCCESS;

}



You're initializing s[0] but you actually using the uninitialized s[1] for Set/Get_level() hence the crash
Oh yes of course, that works now thanks.
Topic archived. No new replies allowed.