Answered step by step
Verified Expert Solution
Question
1 Approved Answer
My professor ask me to Use this exact code in C++ and make a .cpp file for every class. So that I get a .h,
My professor ask me to Use this exact code in C++ and make a .cpp file for every class. So that I get a .h, .cpp for the three class and a main file that run the three . It has to be in dynamic array. No struct or other format. As is, the code run well but my professor want this changes and I am a little confused.
Text Format Code:
Instructor.h:
//Contents of Instructor.h #ifndef INSTRUCTOR #define INSTRUCTOR #include#include using namespace std; // Instructor class class Instructor { private: string lastName; string firstName; string officeNumber; public: // The default constructor stores empty strings // in the string objects. Instructor(){ set("", "", ""); } // Constructor Instructor(string lname, string fname, string office){ set(lname, fname, office); } // set function void set(string lname, string fname, string office){ lastName = lname; firstName = fname; officeNumber = office; } // print function void print() const{ cout << "Last name: " << lastName << endl; cout << "First name: " << firstName << endl; cout << "Office number: " << officeNumber << endl; } // << operator overloading friend ostream &operator<<(ostream &output, const Instructor &obj){ output << "Lase Name: " << obj.lastName << endl; output << "First Name: " << obj.firstName << endl; output << "Office number: " << obj.officeNumber << endl; return output; } // >> operator overloading friend istream &operator>>(istream &input, Instructor &obj) { cout << "Enter Instructor's Last Name: "; input >> obj.lastName; cout << "Enter Instructor's First Name: "; input >> obj.firstName; cout << "Enter Instructor's Office Number: "; input >> obj.officeNumber; return input; } }; #endif
TextBook.h:
//Contents of TextBook.h #ifndef TEXTBOOK #define TEXTBOOK #include#include using namespace std; // TextBook class class TextBook{ private: string title; string author; string publisher; public: // The default constructor stores empty strings // in the string objects. TextBook(){ set("", "", ""); } // Constructor TextBook(string textTitle, string auth, string pub){ set(textTitle, auth, pub); } // set function void set(string textTitle, string auth, string pub){ title = textTitle; author = auth; publisher = pub; } // print function void print() const{ cout << "Title: " << title << endl; cout << "Author: " << author << endl; cout << "Publisher: " << publisher << endl; } // << operator overloading friend ostream &operator<<(ostream &output, const TextBook &obj){ output << "Title: " << obj.title << endl; output << "Author: " << obj.author << endl; output << "Publisher: " << obj.publisher << endl; return output; } // >> operator overloading friend istream &operator>>(istream &input, TextBook &obj) { cout << "Enter book title: "; input >> obj.title; cout << "Enter book Author: "; input >> obj.author; cout << "Enter book publisher: "; input >> obj.publisher; return input; } }; #endif
Course.h:
//Contents of Course.h #ifndef COURSE #define COURSE #include#include #include "Instructor.h" #include "TextBook.h" using namespace std; class Course{ private: string courseName; // Course name Instructor instructor; // Instructor TextBook textbook; // Textbook public: // default construcotr Course(){ courseName = ""; } // Constructor Course(string course, string instrLastName, string instrFirstName, string instrOffice, string textTitle, string author, string publisher) { // Assign the course name. courseName = course; // Assign the instructor. instructor.set(instrLastName, instrFirstName, instrOffice); // Assign the textbook. textbook.set(textTitle, author, publisher); } // print function void print() const { cout << "Course name: " << courseName << endl << endl; cout << "Instructor Information: "; instructor.print(); cout << " Textbook Information: "; textbook.print(); cout<< endl; } // << operator overloading friend ostream &operator<<(ostream &output, const Course &obj){ output << "Course name: " << obj.courseName << endl; output << "Instructor info: " << endl; // use << operator overloading from Instructor class output << obj.instructor; output << "TextBook info: " << endl; // use << operator overlaoding from TextBook class output << obj.textbook; return output; } // >> operator overloading friend istream &operator>>(istream &input, Course &obj) { cout << "Enter Course Name: "; input >> obj.courseName; // use >> operator overloading from the Instructor class input >> obj.instructor; // use >> opeartor overlaoding from the TextBook class input >> obj.textbook; return input; } }; #endif
main.cpp:
#include "Course.h" int main() { // declare variable int n; // ask user to enter how many course to enter cout << "Enter the number of courses you want to enter: "; cin >> n; // dynamically allocate memory from n Course objects using array // store base address of array to a Course pointer Course *courses = new Course[n]; // aks user to enter course information cout << "Enter courses information: " << endl; for (int i = 0; i < n; i++){ cout << "Course " << i+1 << ":" << endl; cin >> courses[i] ; } // print the information entered cout << endl << "Entered information is: " << endl; for (int i = 0; i < n; i++){ cout << endl << "Course " << i+1 << ":" << endl; cout << courses[i]; } 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