Consider the following structure used to keep record of a module:
struct Module
{
string moduleName;
string moduleCode;
string lecturer;
int nrStudents;
}
Turn the Module struct into a class. The class should have member variables for all
the values in the corresponding struct. Make all the member variables private.
Include public member functions for each of the following:
a default constructor that sets the string member variables to blank strings,
and the int member variable to 0;
an overloaded constructor that sets the member variables to specified values;
member functions to set each of the member variables to a value given as an
argument to the function (i.e. mutators);
member functions to retrieve the data from each of the member variables (i.e.
accessors);
Test the class in a program that instantiates an object of class Module. The program should then input values for the object
(obtained from the keyboard), and use the mutators to assign values to the member
variables.
#include <iostream>
#include <string>
using namespace std;
class Module{
string moduleName;
string moduleCode;
string lecturer;
int nrStudents;
public:
Module(){
moduleName = moduleCode = lecturer = "";
nrStudents = 0;
}
Module(string moduleName, string moduleCode, string lecturer, int nrStudents){
this->moduleName = moduleName;
this->moduleCode = moduleCode;
this->lecturer = lecturer;
this->nrStudents = nrStudents;
}
void setModuleName(string moduleName){
this->moduleName = moduleName;
}
void setModuleCode(string moduleCode){
this->moduleCode = moduleCode;
}
void setLecturer(string lecturer){
this->lecturer = lecturer;
}
void setNrStudents(int nrStudents){
this->nrStudents = nrStudents;
}
string getModuleName(){
return moduleName;
}
string getModuleCode(){
return moduleCode;
}
string getLecturer(){
return lecturer;
}
int getNrStudents(){
return nrStudents;
}
};
int main(){
string moduleName;
string moduleCode;
string lecturer;
int nrStudents;
Module module;
cout<<"Input module name: ";
cin>>moduleName;
cout<<"Input module code: ";
cin>>moduleCode;
cout<<"Input lecturer: ";
cin>>lecturer;
cout<<"Input nrStudents: ";
cin>>nrStudents;
module.setModuleName(moduleName);
module.setModuleCode(moduleCode);
module.setLecturer(lecturer);
module.setNrStudents(nrStudents);
cout<<"\nModule Name: "<<module.getModuleName();
cout<<"\nModule Code: "<<module.getModuleCode();
cout<<"\nModule Lecturer: "<<module.getLecturer();
cout<<"\nModule nrStudents: "<<module.getNrStudents();
return 0;
}
Comments
Leave a comment