hi everyone....if anyone has time...
can anyone help me with virtual functions...
I have class student, Graduate, Undergraduate. There's a printInfo() in each class, and the one in student is made pure virtual. I had to create a vector of students that had a combination of Graduates and Undergraduates in main. Then I had to pass the vector to a function. In that function theres a loop that goes through each students and prints their attributes. I had to call printinfo in the loop to show how virtual functions work.
this is what i thought it might be... but it doesn't work.. any ideas?
int main()
{
const int N = 2;
student st[N];
Graduate gr; // Initialize yourself
Undergraduate un; // Initialize yourself
st[0] = gr;
st[i] = un;
for(int i = 0; i < N; i++)
st[i].printInfo();
return 0;
}
Here are the classes that go along with it...
***STUDENT.H*** *
class student
{
protected:
string name;
int age;
long studentId;
public:
student();
student(string n, int a, long id){name=n;age= a; studentId=id;};
virtual void printInfo()=0;
};
*****GRADUATE.H ****
class Graduate: public student
{
protected:
string AdvisorName;
string thesisTitle;
public:
Graduate();
Graduate(string Advisor, string title, string n, int a,
long id){AdvisorName =Advisor; thesisTitle=tit le; name=n; age =a;
studentId=id;};
virtual void printInfo(){cou t << "Student ID: " << studentId <<"Name: " << name << endl << "Age: " << age << endl
<<"Advisor Name: " << AdvisorName << endl << "Thesis Title: "<< thesisTitle << endl << endl;};
};
*****UNDERGRADU ATE.H*****
class Undergraduate: public student
{
protected:
int SchoolYear;
int numofCourses;
public:
Undergraduate() ;
Undergraduate(i nt year, int NoCourses, string n, int a, long id);
virtual void printInfo(){cou t << "Student ID: " << studentId <<"Name: " << name << endl << "Age: " << age << endl
<<"School Year: " << SchoolYear << endl << "Number of Courses: "<< numofCourses << endl
<< endl;};
};
can anyone help me with virtual functions...
I have class student, Graduate, Undergraduate. There's a printInfo() in each class, and the one in student is made pure virtual. I had to create a vector of students that had a combination of Graduates and Undergraduates in main. Then I had to pass the vector to a function. In that function theres a loop that goes through each students and prints their attributes. I had to call printinfo in the loop to show how virtual functions work.
this is what i thought it might be... but it doesn't work.. any ideas?
int main()
{
const int N = 2;
student st[N];
Graduate gr; // Initialize yourself
Undergraduate un; // Initialize yourself
st[0] = gr;
st[i] = un;
for(int i = 0; i < N; i++)
st[i].printInfo();
return 0;
}
Here are the classes that go along with it...
***STUDENT.H*** *
class student
{
protected:
string name;
int age;
long studentId;
public:
student();
student(string n, int a, long id){name=n;age= a; studentId=id;};
virtual void printInfo()=0;
};
*****GRADUATE.H ****
class Graduate: public student
{
protected:
string AdvisorName;
string thesisTitle;
public:
Graduate();
Graduate(string Advisor, string title, string n, int a,
long id){AdvisorName =Advisor; thesisTitle=tit le; name=n; age =a;
studentId=id;};
virtual void printInfo(){cou t << "Student ID: " << studentId <<"Name: " << name << endl << "Age: " << age << endl
<<"Advisor Name: " << AdvisorName << endl << "Thesis Title: "<< thesisTitle << endl << endl;};
};
*****UNDERGRADU ATE.H*****
class Undergraduate: public student
{
protected:
int SchoolYear;
int numofCourses;
public:
Undergraduate() ;
Undergraduate(i nt year, int NoCourses, string n, int a, long id);
virtual void printInfo(){cou t << "Student ID: " << studentId <<"Name: " << name << endl << "Age: " << age << endl
<<"School Year: " << SchoolYear << endl << "Number of Courses: "<< numofCourses << endl
<< endl;};
};
Comment