【发布时间】:2020-01-13 09:54:44
【问题描述】:
我刚刚了解了 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
}
【问题讨论】:
-
“构造函数”一词不存在。您确实有一个构造函数,并且您在
GradeBook myGradeBook(nameoflecturer);行中使用它,因此不清楚您在问什么 -
您是否在任何 C++ 学习资料中查找过构造函数?你的问题到底是什么?
GradeBook(string Lname)是一个构造函数,GradeBook myGradeBook(nameoflecturer)给它一个空字符串。您可能想要删除setCourseName和SetLecturerName,或者至少让它们成为private:。 -
"...让我知道我哪里出错了。"为什么你认为代码有问题?你得到编译器错误吗?运行时错误?出乎意料的结果?你的导师对你当前的代码有什么不满?
-
创建类的实例总是调用构造函数。在类中,构造函数是一个不接受参数且没有返回类型的函数。参数列表提供值。例如,如果名为
Class的类有一个构造函数,该构造函数按值接受int,则变量定义Class x(2)将调用该构造函数(如果可访问)并将值2传递给它。跨度> -
我做得很好,上一个问题是要求遵循一个模板,该模板已经展示了如何使用类(不使用构造函数)显示课程名称,并要求添加讲师姓名以同样的方式。但是这个问题要求使用构造函数。给出的模板是完全不同的东西,而且由于没有正确地教给我们,我使用了上一个问题,只是按照我理解的方式添加了一个构造函数。我遵循模板显示的确切方式,但似乎没有显示任何结果
标签: c++ class constructor