Я только что узнал о классах в c ++ и запрограммировал код, который работал хорошо, но позже меня попросили использовать конструктор, но я еще не узнал об этом. я должен загрузить это сегодня, и я бы легко это сделал, если бы это требование преподавалось в классе, но, к сожалению, нет.
Я попытался изменить код из того, что я знаю после обращения к примеру с примечанием к лекции но я все еще не уверен, как работает этот конструктор или как я должен его реализовать. пока я только попытался использовать его для лектора, где я передаю значение в конструктор функции с именем SetLecturerName
, но я не уверен, что еще я должен делать. Был бы очень признателен, если бы кто-то мог изучить этот код и сообщить мне, где я ошибся.
Требуется использовать конструктор как для лектора, так и для названия курса. Я подумал о том, чтобы сначала решить одну и управлять одним.
#include <iostream>
#include <string> //program uses c++ standard string class
using namespace std;
class GradeBook{ //gradebook class definition
//PUBLIC-------------------------------------------------------------------------------------------------------
public:
//COURSE---------------------------------------------------------------------------------------------------
void setCourseName(string name){ //function that sets the course name
courseName = name; //store the course name in the object
}
string getCourseName(){ //function that gets the course name
return courseName; //return the object's courseName
}
//LECTURER-------------------------------------------------------------------------------------------------
/*void setLecturerName(string LecName){ //function that sets the course name
lecturerName = LecName; //store the course name in the object
}*/
GradeBook(string Lname){
SetLecturerName(Lname); //calling the constructor function
}
void SetLecturerName(string LecName){ //The constructor function
lecturerName = LecName; //store the course name in the object
}
string getLecturerName(){ //function that gets the course name
return lecturerName; //return the object's courseName
}
//WELCOME MESSAGE------------------------------------------------------------------------------------------
void displayMessage(){ //function that displays a welcome message
cout << "==================================" << endl;
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl << endl; //this statement calls getCourseName to get the name of the course this gradebook represents
cout << "This course is presented by, \n" << getLecturerName() << "." << endl;
cout << "==================================" << endl;
}
//PRIVATE------------------------------------------------------------------------------------------------------
private:
string courseName; //course name for this gradebook
string lecturerName; //Lecturer name for this gradebook
};
int main(){
string nameOfCourse; //string of characters to store the course name
string nameoflecturer; //string of characters to store the lecturer name
GradeBook myGradeBook(nameoflecturer); //create a GradeBook object named myGradeBook
//COURSE---------------------------------------------------------------------------------------------------
cout << "\nPlease enter the course name:" << endl;
getline(cin, nameOfCourse); //read a course name with blanks
myGradeBook.setCourseName(nameOfCourse); //set the course name
//LECTURER-------------------------------------------------------------------------------------------------
cout << "\nPlease enter the Lecturer's name including the title(Ex- Mr.):" << endl;
getline(cin, nameoflecturer);
myGradeBook.setLecturerName(nameoflecturer);
//WELCOME MESSAGE------------------------------------------------------------------------------------------
cout << endl;
myGradeBook.displayMessage(); //display message with new course name
}