Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab will consist of aggregation, inheritance, and polymorphism. You will use the given Person, Faculty, and Student classes and alter the definitions to add

This lab will consist of aggregation, inheritance, and polymorphism. You will use the given Person, Faculty, and Student classes and alter the definitions to add polymorphic behavior. In addition, you will write an Abstract Base Class called 'CourseInterface' (parent), a derived 'Course' class (child), and two derived-derived classes called 'Lecture' and 'Lab' (grand children). The description of the classes are listed below. Function main will also be given to you with a ClassInfo class. This is 5.cpp (C++)

CourseInterface (Abstract Base Class)

This class will contain pure virtual member functions for the derived class's public interface. In addition to protected member variables: a statically sized cstring for 'title' and 'type', a Faculty object 'lecturer', a PersonStudent pointer 'roster', and an integer for 'count' number of students enrolled, and and integer for 'capacity'.

CourseInterface() - Default constructor: set the first character member variable 'Title' to NULL. Allocate CLASSROOM_CAP Student objects to the Person pointer (Use a defined constant and set CLASSROOM_CAP to 35). Set count and capacity to 0.

CourseInterface(const CourseInterface&) - Copy constructor: copy the static member variables. Also, if the passed object's roster is not NULL, allocate a new Student array to 'roster', traverse and copy passed object's roster array.

void operator=(const CourseInterface&) - Overloaded Assignment Operator: if the address of the passed object is the same, return. Otherwise, if 'roster' is NOT NULL, deallocate the 'roster' array and set to NULL. Copy the static member variables. Also, if the passed object's roster is not NULL, allocate a new Student array to 'roster', traverse and copy passed object's roster array.

~CourseInterface() - Destructor: deallocate the Student array and set the Student pointer to NULL.

void setTitle(const char*) - copy the passed argument to set the 'title' member variable.

void setType(const char*) - copy the passed argument to set the 'type' member variable.

const char* getTitle() const - return the 'title' member variable

const int getCount() const - return 'count'.

const char* getType() const { return 'type'.

void inputCourse() = 0 - Pure virtual member function. To be defined in derived class. This will act as a driver function to input the course information (set title, set Faculty, allocate Student array, set each Student).

void printCourse() = 0 - Pure virtual member function. To be defined in derived class. This will act as a driver function to output the course information (print course title, type, print Faculty object, print class roster).

Course (Derived)

This class will inherit from CourseInterface publically.

Course() - default constructor: invoke parent default constructor.

Course(const Course&) - copy constructor: invoke and pass to parent copy constructor.

void operator=(const Course&) - Overloaded Assignment Operator: call the parent's version of the overloaded operator with passed Course argument.

~Course() - destructor: empty definition.

void inputCourse() - In this function you will prompt the user for the course title and set the title with the corresponding mutator function. Call the Faculty member function 'inputPerson' to set the Faculty object. Prompt the user for a positive number that is less than CLASSROOM_CAP and validate. Traverse the defautly allocated 'roster' array and call the Student member function 'inputPerson' for each Student object within the array.

void printCourse() - With nicely fomatted output, print the course title, type, lecturer, and each student within the 'roster' array.

Lecture (Derived-Derived)

This class will inherit from Course publically

Lecture() - default constructor: invoke parent default constructor and call the mutator function for 'type' and pass it string literal "Lecture".

Lecture(const Lecture&) - copy constructor: invoke and pass to parent copy constructor and call the mutator function for 'type' and pass it string literal "Lecture".

~Lecture() - destructor: empty definition.

void operator=(const Course&) - Overloaded Assignment Operator: call the parent's version of the overloaded operator with passed Lecture object Argument. Also, call the mutator function for 'type' and pass it string literal "Lecture".

Lab (Derived-Derived)

This class will inherit from Course publically, and will contain one Faculty member object 'labInstructor'.

Lab() - default constructor: invoke parent default constructor. Assign the Faculty member object 'labInstructor' to be the Course's 'lecturer' member object by default. Also, call the mutator function for 'type' and pass it string literal "Lab".

Lab(const Lab&) - copy constructor: invoke and pass to parent copy constructor. Assign the Faculty member object 'labInstructor' to be the passed object's member. Also, call the mutator function for 'type' and pass it string literal "Lab".

~Lab() - destructor: empty definition.

void operator=(const Course&) - Overloaded Assignment Operator: call the parent's version of the overloaded operator with passed Lab object Argument. Also, call the mutator function for 'type' and pass it string literal "Lab".

void inputCourse() - overridden function: call the member function 'inputPerson' of the member object 'labInstructor'. Then call the parent's version of inputCourse.

void printCourse() - overridden function: For this version of printCourse, you will print the course title, type, lab instructor, and each student object in the roster array.

void inputLabFaculty() - this function will call the member function 'inputPerson' of the member object 'labInstructor'.

CourseInfo

This class definition is given to you. Aggregation is the instance of a class within another class ("has a" relationship). A CourseInfo object will contain an instance of a Lecture and a Lab object.

 Complete this Code (But please do NOT make any changes to how the code is written, follow it): #include #include #include #include using namespace std; #define MAX 32 class Person { protected: char name[MAX]; public: Person() { name[0] = '\0'; } ~Person() { } void operator=(const Person& src) { strcpy(name, src.name); } void inputPerson() { char n[MAX]; cout << "Enter person's name: "; cin.getline(n, MAX); setName(n); } void printPerson() { cout << "Name: " << getName(); } void setName(const char* n) { strcpy(name, n); } const char* getName() const { return name; } }; class Faculty : public Person { protected: char department[MAX]; public: Faculty() { department[0] = '\0'; } ~Faculty() { } void operator=(const Faculty& src) { Person::operator=(src); strcpy(department, src.department); } void inputPerson() { cout << "Please enter Faculty Information: "; Person::inputPerson(); char d[MAX]; cout << "Enter faculty's department: "; cin.getline(d, MAX); setDepartment(d); } void printPerson() { Person::printPerson(); cout << " Department: " << getDepartment() << endl; } const char* getDepartment() const {return department;} void setDepartment(const char* d) { strcpy(department, d); } }; class Student : public Person { protected: char major[MAX]; public: Student() { major[0] = '\0'; } ~Student() { } void operator=(const Student& src) { Person::operator=(src); strcpy(major, src.major); } void inputPerson() { cout << "Please enter Student Information: "; Person::inputPerson(); char m[MAX]; cout << "Enter student's major: "; cin.getline(m, MAX); setMajor(m); } void printPerson() { Person::printPerson(); cout << "\tMajor: " << getMajor() << endl; } const char* getMajor() const {return major;} void setMajor(const char* m) { strcpy(major, m); } }; #define CLASSROOM_CAP 35 /*********************************/ //Need to define class CourseInterface { public: CourseInterface(); CourseInterface(const CourseInterface&); void operator=(const CourseInterface&); ~CourseInterface(); void setTitle(const char*); void setType(const char*); const char* getTitle() const; const int getCount() const; const char* getType() const; void inputCourse()=0; void printCourse()=0; protected: char title[CLASSROOM_CAP]; char type[CLASSROOM_CAP]; Faculty lecturer; Student* roster; int count; int capacity; }; //Need to define class Course { public: Course(); Course(const Course&); void operator=(cont Course&); ~Course(); void inputCourse(); void printCourse(); }; //Need to define class Lecture { public: Lecture(); Lecture(const Lecture&); ~Lecture(); void operator=(const Course&); }; //Need to define class Lab { public: Faculty labIntructor; Lab(); Lab(const Lab&); ~Lab(); void operator=(const Course&); void inputCourse(); void printCourse(); void inputLabFaculty(); }; /*********************************/ class CourseInfo { protected: Course* lecture; Course* lab; public: CourseInfo(){ lecture = new Lecture; lab = new Lab; } ~CourseInfo() { if(lecture) delete lecture; if(lab) delete lab; lecture=NULL; lab=NULL; } void inputCourseInfo() { cout << " Input Lecture Information "; lecture->inputCourse(); cout << " Input Lab Information "; lab->inputCourse(); } void printCourseInfo() { cout << " --Lecture-- "; lecture->printCourse(); cout << " --Lab-- "; lab->printCourse(); } }; int main() { Course *c= NULL; CourseInfo *ci = NULL; int choice; do{ cout << " "; cout << "1. Input Lecture " << "2. Input Lab " << "3. Print Course " << "4. Input CourseInfo " << "5. Print CourseInfo " << "0. Exit " << "Enter choice: "; cin >> choice; cin.ignore(256,' '); cout << " "; switch(choice) { case 1: if(c) { delete c; c=NULL; } c = new Lecture; c->inputCourse(); break; case 2: if(c) { delete c; c=NULL; } c = new Lab; c->inputCourse(); break; case 3: if(c) { c->printCourse(); }else { cout << "Input a course first "; } break; case 4: if(ci){ delete ci; c=NULL; } ci = new CourseInfo; ci->inputCourseInfo(); break; case 5: if(ci){ ci->printCourseInfo(); } else { cout << "Input a full course first "; } break; case 0: cout << "Good Bye "; if(c) delete c; if(ci) delete ci; c=NULL; ci=NULL; break; default: cout << "Invalid option "; } }while(choice!=0); cout << endl; return(0); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

socialist egalitarianism which resulted in wage levelling;

Answered: 1 week ago

Question

soyuznye (all-Union, controlling enterprises directly from Moscow);

Answered: 1 week ago