In: Computer Science
Current Assignment:
**It is important to note that we are learning about Composition right now.**
Now that we have the CourseType class (included courseType code below instructions) we want to create another "collection" class to hold more than one. This way we can add one class to the StudentType and have a full list of courses. There are many (better) ways we could structure this collection, but for now we're going to use a static array:
Submit: Commented test, implementation and header files (.cpp and .h) for new CourseListType.
My courseType header file:
#ifndef COURSETYPE_H_INCLUDED
#define COURSETYPE_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class courseType {
public:
courseType(int, string);
int getCourseID();
string getCourseName();
void setCourseID(int);
void setCourseName(string);
void printCourse();
private:
int courseID;
string courseName;
};
#endif // COURSETYPE_H_INCLUDED
My courseType Implementation file:
#include "courseType.h"
#include <string>
using namespace std;
courseType::courseType(int id, string name) {
this-> courseID = id;
this-> courseName = name;
}
int courseType::getCourseID() {
return courseID;
}
string courseType::getCourseName() {
return courseName;
}
void courseType::setCourseID(int id) {
courseID = id;
}
void courseType::setCourseName (string name) {
courseName = name;
}
void courseType::printCourse() {
cout << "Course ID: " << getCourseID() <<
endl;
cout << "Course Name: " << getCourseName() <<
endl;
}
courseType test program
#include "courseType.h"
#include <string>
#include <vector>
using namespace std;
int main() {
courseType c1(101, "English");
courseType c2(102, "Mathematics");
courseType c3(103, "Oceanography");
vector <courseType> courses;
courses.push_back(c1);
courses.push_back(c2);
courses.push_back(c3);
for(int i = 0; i < courses.size(); i++) {
cout << "======================================" <<
endl;
cout << "\t COURSE #" << (i+1) << " DETAILS "
<< endl;
cout << "======================================" <<
endl;
courses.at(i).printCourse();
cout << endl;
}
return 0;
}
// courseType.h
#ifndef COURSETYPE_H_INCLUDED
#define COURSETYPE_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class courseType {
public:
// Add a default constructor in the courseType class so that static
array of courses can be created(as static array will call default
constructor of courseType for all its entries)
courseType();
courseType(int, string);
int getCourseID();
string getCourseName();
void setCourseID(int);
void setCourseName(string);
void printCourse();
private:
int courseID;
string courseName;
};
#endif // COURSETYPE_H_INCLUDED
// end of courseType.h
//courseType.cpp
#include "courseType.h"
// default constructor that sets courseID and courseName to 0
and empty string respectively
courseType::courseType()
{
courseID = 0;
courseName = "";
}
courseType::courseType(int id, string name) {
this-> courseID = id;
this-> courseName = name;
}
int courseType::getCourseID() {
return courseID;
}
string courseType::getCourseName() {
return courseName;
}
void courseType::setCourseID(int id) {
courseID = id;
}
void courseType::setCourseName (string name) {
courseName = name;
}
void courseType::printCourse() {
cout << "Course ID: " << getCourseID() <<
endl;
cout << "Course Name: " << getCourseName() <<
endl;
}
// end of courseType.cpp
// courseListType.h
#ifndef COURSELISTTYPE_H_INCLUDED
#define COURSELISTTYPE_H_INCLUDED
#include "courseType.h"
class courseListType
{
public:
courseListType();
void addCourse(courseType course);
void printCourseList();
private:
int numCourses; // current number of courses in the list
courseType courses[50]; // static array of maximum 50 courses
};
#endif // COURSELISTTYPE_H_INCLUDED
//end of courseListType.h
// courseListType.cpp
#include "courseListType.h"
// default constructor that creates an empty list
courseListType::courseListType()
{
numCourses = 0;
}
// function to add course to list
void courseListType:: addCourse(courseType course)
{
if(numCourses < 50) // array is not full, add course at the end
of list
{
courses[numCourses] = course;
numCourses++;
}
}
// function to display the courses in the list
void courseListType:: printCourseList()
{
if(numCourses == 0) // empty list
cout<<"There are no courses in the list"<<endl;
else
{
for(int i=0;i<numCourses;i++){
courses[i].printCourse();
cout<<endl;
}
}
}
// end of courseListType.cpp
// courseListTypeTest.cpp : C++ program to test the
courseListType class
#include <iostream>
#include <string>
#include "courseListType.h"
int main()
{
courseListType list;
list.addCourse(courseType(101, "English"));
list.addCourse(courseType(102, "Mathematics"));
list.addCourse(courseType(103, "Oceanography"));
list.printCourseList();
return 0;
}
// end of courseListTypeTest.cpp
Output: