(code below) Change the base class and derived class definitions to include the
ID: 3853344 • Letter: #
Question
(code below) Change the base class and derived class definitions to include the default constructor and an initializing constructor. The parameterized constructors need to initialize the private members of the respective class.
The derived class constructor needs to accept the initializing values for the base class members as well.
Also, write destructor functions for all base and derived classes. The default constructor and destructor functions don’t have to do anything.
In main func:
Create an object each of classes senior and freshman. While creating the objects, call the
parametrized derived class constructors and pass appropriate values. Call the derived class accessor function to get the value of name, id, gpa and respective
scores.
//Include the needed header files
#include <iostream>
#include <string>
using namespace std;
//Class
class Student
{
//Access specifier
public:
//Mutator
void setVars();
//Accessor
void getVars();
//Access specifier
private:
//Variable for student id
unsigned long int id;
//Variable for student name
string name;
//Variable for student gpa
double gpa;
};
//Definition for Mutator
void Student::setVars()
{
//Variable for student id
unsigned long int id;
//Variable for student name
string name;
//Variable for student gpa
double gpa;
//Prompt for id
cout<<" Enter the value of id : ";
//Read id
cin>>id;
//Assign
this->id= id;
//Prompt for name
cout<<" Enter the value of name : ";
//Read name
cin.ignore();
getline(cin,name);
//Assign
this->name=name;
//Prompt for gpa
cout<<" Enter the value of gpa : ";
//Read gpa
cin>>gpa;
//Assign
this->gpa=gpa;
}
//Definition for Accessor
void Student::getVars()
{
//Display student ID
cout<<" ID : "<<id;
//Display student name
cout<<" Name : "<<name;
//Display student gpa
cout<<" GPA : "<<gpa;
}
//Derived class - Freshman
class Freshman: public Student
{
//Access specifier
private:
//Variable for score
double csce1030;
//Access specifier
public:
//Accessor
void getValue();
//Mutator
void setValue();
};
//Definition for Accessor
void Freshman::getValue()
{
//Display freshman score
cout<<" Score : "<<csce1030;
}
//Definition for Mutator
void Freshman::setValue()
{
//Prompt for freshman score
cout<<" Enter score for freshman: ";
//Read freshman score
cin>>csce1030;
//Assign
this->csce1030=csce1030;
}
//Derived class - Senior
class Senior: public Student
{
//Access specifier
private:
//Variable for score
double csce4530;
//Access specifier
public:
//Accessor
void getValue();
//Mutator
void setValue();
};
//Definition for Accessor
void Senior::getValue()
{
//Display senior score
cout<<" Score : "<<csce4530;
}
//Definition for Mutator
void Senior::setValue()
{
//Prompt for senior score
cout<<" Enter score for senior: ";
//Read senior score
cin>>csce4530;
//Assign
this->csce4530=csce4530;
}
//Driver
int main()
{
//Create instance for freshman
Freshman f1;
//Create instance for senior
Senior s1;
//Details of freshman
cout<<"Freshman"<<endl;
//Function call
f1.setVars();
f1.setValue();
cout<<endl<<"-------------------------------";
f1.getVars();
f1.getValue();
cout<<endl<<"-------------------------------";
//Details of senior
cout<<endl<<" Senior"<<endl;
//Function call
s1.setVars();
s1.setValue();
cout<<endl<<"-------------------------------";
s1.getVars();
s1.getValue();
cout<<endl<<"-------------------------------";
//Pause
cin.get();cin.get();
//Stop
return 0;
}
Explanation / Answer
Below is your code: -
//Include the needed header files
#include <iostream>
#include <string>
using namespace std;
//Class
class Student
{
//Access specifier
public:
//Mutator
void setVars();
//Accessor
void getVars();
//default constructor
Student();
//initializing constructor
Student(unsigned long int id,string name,double gpa);
~Student() {
}
//Access specifier
private:
//Variable for student id
unsigned long int id;
//Variable for student name
string name;
//Variable for student gpa
double gpa;
};
Student::Student() {
}
Student::Student(unsigned long int id,string name,double gpa){
this->id = id;
this->name = name;
this->gpa = gpa;
}
//Definition for Mutator
void Student::setVars()
{
//Variable for student id
unsigned long int id;
//Variable for student name
string name;
//Variable for student gpa
double gpa;
//Prompt for id
cout<<" Enter the value of id : ";
//Read id
cin>>id;
//Assign
this->id= id;
//Prompt for name
cout<<" Enter the value of name : ";
//Read name
cin.ignore();
getline(cin,name);
//Assign
this->name=name;
//Prompt for gpa
cout<<" Enter the value of gpa : ";
//Read gpa
cin>>gpa;
//Assign
this->gpa=gpa;
}
//Definition for Accessor
void Student::getVars()
{
//Display student ID
cout<<" ID : "<<id;
//Display student name
cout<<" Name : "<<name;
//Display student gpa
cout<<" GPA : "<<gpa;
}
//Derived class - Freshman
class Freshman: public Student
{
//Access specifier
private:
//Variable for score
double csce1030;
//Access specifier
public:
//Accessor
void getValue();
//Mutator
void setValue();
//default constructor
Freshman() {
}
//initializing constructor
Freshman(unsigned long int id,string name,double gpa,double csce1030) : Student(id,name,gpa) {
this->csce1030 = csce1030;
}
~Freshman() {
}
};
//Definition for Accessor
void Freshman::getValue()
{
//Display freshman score
cout<<" Score : "<<csce1030;
}
//Definition for Mutator
void Freshman::setValue()
{
//Prompt for freshman score
cout<<" Enter score for freshman: ";
//Read freshman score
cin>>csce1030;
//Assign
this->csce1030=csce1030;
}
//Derived class - Senior
class Senior: public Student
{
//Access specifier
private:
//Variable for score
double csce4530;
//Access specifier
public:
//Accessor
void getValue();
//Mutator
void setValue();
//default constructor
Senior() {
}
//initializing constructor
Senior(unsigned long int id,string name,double gpa,double csce4530) : Student(id,name,gpa) {
this->csce4530 = csce4530;
}
~Senior() {
}
};
//Definition for Accessor
void Senior::getValue()
{
//Display senior score
cout<<" Score : "<<csce4530;
}
//Definition for Mutator
void Senior::setValue()
{
//Prompt for senior score
cout<<" Enter score for senior: ";
//Read senior score
cin>>csce4530;
//Assign
this->csce4530=csce4530;
}
//Driver
int main()
{
//Create instance for freshman
Freshman f1;
//Create instance for senior
Senior s1;
//Constructor calling
Freshman f2(1,"Suresh",7.4,23.4);
Senior s2(2,"Ramesh",8.4,62.4);
//Details of freshman
cout<<"Freshman"<<endl;
//Function call
f1.setVars();
f1.setValue();
cout<<endl<<"-------------------------------";
f1.getVars();
f1.getValue();
cout<<endl<<"-------------------------------";
f2.getVars();
f2.getValue();
cout<<endl<<"-------------------------------";
//Details of senior
cout<<endl<<" Senior"<<endl;
//Function call
s1.setVars();
s1.setValue();
cout<<endl<<"-------------------------------";
s1.getVars();
s1.getValue();
cout<<endl<<"-------------------------------";
s2.getVars();
s2.getValue();
cout<<endl<<"-------------------------------";
//Pause
cin.get();cin.get();
//Stop
return 0;
}
Sample Run: -
Freshman
Enter the value of id : 3
Enter the value of name : Maddy
Enter the value of gpa : 4.6
Enter score for freshman: 23.7
-------------------------------
ID : 3
Name : Maddy
GPA : 4.6
Score : 23.7
-------------------------------
ID : 1
Name : Suresh
GPA : 7.4
Score : 23.4
-------------------------------
Senior
Enter the value of id : 4
Enter the value of name : Paddy
Enter the value of gpa : 4.5
Enter score for senior: 67.8
-------------------------------
ID : 4
Name : Paddy
GPA : 4.5
Score : 67.8
-------------------------------
ID : 2
Name : Ramesh
GPA : 8.4
Score : 62.4
-------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.