Answer to Question #218435 in C++ for Nikita

Question #218435

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; member functions to retrieve the data from each of the member variables; Test the class in a program that instantiates an object of class Module. The program should then input values for the object, and use the mutators to assign values to the member variables. Use the accessors to obtain the values of the object's member variables and display those values on the screen.


1
Expert's answer
2021-07-18T01:24:34-0400
#include <iostream>

using namespace std;

class Module{
    private:
    string moduleName; 
    string moduleCode; 
    string lecturer; 
    int nrStudents;
    public:
    Module(){
        moduleCode = "";
        moduleName = "";
        lecturer = "";
        nrStudents = 0;
    }
    Module(string m, string c, string l, int nr){
        moduleCode = c;
        moduleName = m;
        lecturer = l;
        nrStudents = nr;
    }
    void setLecturer(string name){
        lecturer = name;
    }
    void setNrStudents(int s){
        nrStudents = s;
    }
    void setModuleName(string name){
        moduleName = name;
    }
    void setModulecode(string code){
        moduleCode = code;
    }
    string getModuleCode(){
        return moduleCode;
    }
    string getLectureName(){
        return lecturer;
    }
    int getNrStudents(){
        return nrStudents;
    }
    string getModuleName(){
        return moduleName;
    }
};


int main()
{
    string moduleName, moduleCode, lecturer;
    int nrStudents;
    
    cout<<"Input module name: ";
    cin>>moduleName;
    cout<<"Input module code: ";
    cin>>moduleCode;
    cout<<"Input lecturer: ";
    cin>>lecturer;
    cout<<"Input Students: ";
    cin>>nrStudents;
    
    
    Module m;
    
    cout<<"---------Before Assignment Default constructor-----------------"<<endl;
    cout << " ModuleName: " << m.getModuleName()<<" \n " <<"ModuleCode: " << m.getModuleCode() <<" \n " << "LectureName: " << m.getLectureName()<<" \n "<< "NrStudents: "<<m.getNrStudents() <<endl;
    m.setModulecode(moduleCode);
    m.setModuleName(moduleName);
    m.setNrStudents(nrStudents);
    m.setLecturer(lecturer);
    cout<<"---------After Assignment-----------------"<<endl;
    cout << " ModuleName: " << m.getModuleName()<<" \n " <<"ModuleCode: " << m.getModuleCode() <<" \n " << "LectureName: " << m.getLectureName()<<" \n "<< "NrStudents: "<<m.getNrStudents() <<endl;
    
    Module n("Module2", "Code2", "Lecturer2", 5);
    cout<<"\n\n---------Overloaded constructor-----------------"<<endl;
    cout << " ModuleName: " << n.getModuleName()<<" \n " <<"ModuleCode: " << n.getModuleCode() <<" \n " << "LectureName: " << n.getLectureName()<<" \n "<< "NrStudents: "<<n.getNrStudents() <<endl;
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS