
Shanil P.
asked 10/15/20Lab for CS112 Data Structures and Algorithms
Answer this and please explain so that it is easy to understand the following questions and the explanation given.
Implement a base class Person. Derive classes Student and Instructor from Person. A person has a name and a birthday. A student has a major, and an instructor has a salary. Write the class definitions, the constructors, and the member functions display for all classes.
1 Expert Answer

Patrick B. answered 10/15/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define NAME_LENGTH (255)
typedef
class Person
{
protected:
char name[NAME_LENGTH];
char birthday[3]; // M-D-Y Ex. 3 4 5 is March 4 2005
public:
Person( char * nameStr, char * bday)
{
memset(name,0,NAME_LENGTH);
memset(birthday,0,3);
strncpy(name,nameStr,NAME_LENGTH);
strncpy(birthday,bday,3);
}
void GetName( char * nameStr) { strncpy(nameStr,name,NAME_LENGTH); }
void GetBirthday( char * bday) { strncpy(bday,birthday,3); }
~Person()
{
memset(name,0,NAME_LENGTH);
memset(birthday,0,3);
}
Person( Person & person)
{
memset(this->name,0,NAME_LENGTH);
memset(this->birthday,0,3);
person.GetName(this->name);
person.GetBirthday(this->birthday);
}
} * TPerson;
#define PERSON_SIZE ( sizeof(Person))
#define STUDENT_MAJOR_LENGTH (55)
typedef
class Student : public Person
{
protected:
char major[STUDENT_MAJOR_LENGTH];
public:
Student( char * strName, char * bday, char * strMajor) : Person(strName,bday)
{
memset(major,0,STUDENT_MAJOR_LENGTH);
strncpy(major,strMajor,STUDENT_MAJOR_LENGTH);
}
void GetMajor( char * strMajor) { strncpy(strMajor,major,STUDENT_MAJOR_LENGTH ); }
void SetMajor( char * strMajor) { strncpy(major,strMajor,STUDENT_MAJOR_LENGTH ); }
~Student()
{
memset(major,0,STUDENT_MAJOR_LENGTH);
}
} * TStudent;
#define STUDENT_SIZE (sizeof(Student))
typedef
class Instructor : public Person
{
protected:
double salary;
public:
Instructor (char * strName, char * bday, double Salary) : Person(strName,bday)
{
salary = Salary;
}
double GetSalary() { return(salary); }
void SetSalary( double Salary) { salary = Salary; }
~Instructor()
{
salary=0;
}
} * TInstructor;
#define INSTRUCTOR_SIZE (sizeof(Instructor))
int main()
{
char dateBuff[3];
char nameBuff[NAME_LENGTH];
char major[STUDENT_MAJOR_LENGTH];
double salary;
Student student((char*)"Shanil P.",(char*)"IDK",(char*)"Unknown");
dateBuff[0]=2; dateBuff[1]=11; dateBuff[2]=70;
Instructor instructor((char*)"Patrick Baldwin",dateBuff,49735.42f);
student.GetName(nameBuff);
cout << " Student Name :>" << nameBuff << "<" << endl;
student.GetBirthday(dateBuff);
cout << "Student Birthday >" << dateBuff << "< " << endl;
student.GetMajor(major);
cout << "Student Major >" << major << "<" << endl;
instructor.GetName(nameBuff);
cout << " Instructor Name :>" << nameBuff << "<" << endl;
instructor.GetBirthday(dateBuff);
for (int iLoop=0; iLoop<3; iLoop++) { cout << (int) dateBuff[iLoop] << " "; }
cout << endl;
cout << " Instructor salarhy " << fixed << setprecision(2) << instructor.GetSalary() << endl;
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to resource section... If there is something you do not understand, ASK QUESTIONS !!!!10/15/20