Question
A. Consider the following definition of the class studentType class studentType: public personType { public: void print(); void calculateGPA(); void setID(long id); void setCourses(const string
A. Consider the following definition of the
class studentType
class studentType: public personType
{
public:
void print();
void calculateGPA();
void setID(long id);
void setCourses(const string c[], int noOfC);
void setGrades(const char cG[], int noOfC);void getID();
void getCourses(string c[], int noOfC);
void getGrades(char cG[], int noOfC);
studentType(string fName = "", string lastName = "",long id=0, string c[] = NULL,char cG[] = NULL, int noOfC = 0);
private:
long studentId;
string courses[6];
char coursesGrade[6]int noOfCourses;
}
Rewrite the definition of the class studentType so that the functions print and calculateGPA are virtual functions.
B. Provide Implementation of the class in part A
C. Provide Test program.
PersonType is already implemented.
I have finished everything. but i cant make the tester file that test the virtual function, can you help me please..
I have the following code so far.
//personType.h
#ifndef H_personType
#define H_personType
#include
using namespace std;
class personType
{
public:
void print() const;
//Function to output the first name and last name
//in the form firstName lastName.
void setName(string first, string last);
//Function to set firstName and lastName according
//to the parameters.
//Postcondition: firstName = first; lastName = last
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of the data member firstName
// is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of the data member lastName
// is returned.
personType(string first = "", string last = "");
//constructor
//Sets firstName and lastName according to the parameters.
//The default values of the parameters are empty strings.
//Postcondition: firstName = first; lastName = last
private:
string firstName;
//variable to store the first name
string lastName;
//variable to store the last name
};
#endif
//personType.cpp
#include "personType.h"
#include
using namespace std;
void personType::print() const
{
cout << firstName << " " << lastName << endl;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
//studentType.h
#ifndef H_studentType
#define H_studentType
#include
#include "personType.h"
using namespace std;
class studentType: public personType
{
public:
virtual void print();
void calculateGPA();
void setID(long id);
void setCourses(const string c[], int noOfC);
void setGrades(const char cG[], int noOfC);
void getID();
void getCourses(string c[], int noOfC);
void getGrades(char cG[], int noOfC);
studentType(string fName = "", string lastName = "",long id = 0, string c[] = NULL,char cG[] = NULL, int noOfC = 0);
private:
long studentId;
string courses[6];
char coursesGrade[6];
int noOfCourses;
};
#endif
//studentType.cpp
#include "studentType.h"
#include
using namespace std;
void studentType::print()
{
cout << "Name: " << getFirstName() << " " << getLastName() << endl;
cout << "Student ID: " << studentId << endl;
cout << "No of courses: " << noOfCourses << endl;
cout <
for (int i = 0; i < noOfCourses; i++)
{
cout << courses[i] << ": " << coursesGrade[i] << endl;
}
calculateGPA();
cout<
}
void studentType::calculateGPA()
{
float score = 0;
for (int i = 0; i < noOfCourses; i++)
{
switch (coursesGrade[i])
{
case 'A':
score = score + 4;
break;
case 'B':
score = score + 3;
break;
case 'C':
score = score + 2;
break;
case 'D':
score = score + 1;
break;
case 'F':
score = score + 0;
break;
}
}
score /= noOfCourses;
cout << "GPA is : " << score << endl;
}
void studentType::setID(long id)
{
studentId = id;
}
void studentType::setCourses(const string c[], int noOfC)
{
noOfCourses=noOfC;
for (int i = 0; i < noOfCourses; i++)
courses[i] = c[i];
}
void studentType::setGrades(const char cG[], int noOfC)
{
for (int i = 0; i < noOfC; i++)
coursesGrade[i] = cG[i];
}
void studentType::getID()
{
cout << studentId;
}
void studentType::getCourses(string c[], int noOfC)
{
noOfCourses = noOfC;
for (int i = 0; i < noOfC; i++)
{
courses[i]= c[i];
cout << courses[i]<
}
}
studentType::studentType(string fName , string lastName ,long id , string c[] ,char cG[] , int noOfC )
{
}
void studentType::getGrades(char cG[], int noOfC)
{
noOfCourses = noOfC;
for (int i = 0; i < noOfC; i++)
{
coursesGrade[i]=cG[i];
cout << courses[i] << " : "<< coursesGrade[i];
cout<< endl;
}
}
//this is tester file.. it is just rough work... can you test the virtual function ? by using pointer and dynamic variable.
#include "personType.h"
#include "studentType.h"
#include
using namespace std;
void Print(studentType& a)
{
a.print();
}
int main()
{
studentType s1;
studentType s2;
studentType s3;
s1.setName("tom","king");
s1.setID(2088);
string c[] = {"English 103","Calculus 2","Physics 201","CIS185","CIS280X","CIS292"};
s1.setCourses(c,6);
char grades[] = {'A','A','B','B','C','A'};
s1.setGrades(grades,6);
s1.print();
s2.setName("brain","lara");
s2.setID(1098);
s2.setCourses(c,6);
char grades1[] = {'B','A','C','A','A','A'};
s2.setGrades(grades1,6);
s2.print();
s3.setName("lincon","joy");
s3.setID(3482);
s3.setCourses(c,6);
cout<<"Name: "<< s3.getFirstName()<< "," << s3.getLastName()<< endl;
s3.getGrades(grades1,6);
//s3.getCourses(c,6);
//s3.print();
studentType* p1ptr;
personType p1;
p1ptr= &p1;
p1ptr->setName("joy","it");
p1ptr->print();
personType* p2ptr;
personType* p3ptr;
p2ptr = &s2;
p3ptr = &s3;
p2ptr->print();
p3ptr->print();
return 0;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started