C++ 的新手。可以在类里面使用此作业的一些帮助。 分配要求是:
Create a class called CourseList that includes one instance variables— nameOfCollege (type String). Provide a displayMessage member function to welcome the user to the CourseList program. Provide a set function that sets the name of the college, and a get function that retrieves the name of the college. The set function should have one string parameter for the name of the college. Use the function main() to demonstrate class Course’s capabilities.
为了展示我们对类的理解,我们将创建具有对象和函数的类。最终结果是一个控制台应用程序,用于查看假大学提供的假类(class)。
目前,我的 get 函数没有返回带有我的假大学名称的字符串。这就是我目前正在努力解决的问题。我认为我的问题在于我如何编写函数或如何构建对象。
这是我的代码:
// DJ Homework
// welcome function [done]
// set function w/ college name in parameter [maybe done?]
// get function [not done]
// return string [not done]
// display class list [not done]
#include <iostream>
#include <string> // program uses C++ standard string class
using namespace std;
// CourseList class definition
class CourseList
{
private:
// string here
string nameOfCollege;
public:
void setCollegeName(string nameOfCollege);
string getCollegeName();
void CourseList::mysetCollegeName(string nameOfCollege)
{
nameOfCollege = "Smart Peoples University";
}
string CourseList::mygetCollegeName()
{
return string();
}
// function that displays a welcome message to the CourseList user
void displayMessage(string nameOfCollege) const
{
cout << "Welcome to the CourseList Program\n\n\n" << "To see the classes offered at " << nameOfCollege << ",type something, then press 'Enter'.";
} // end function displayMessage
}; // end class GradeBook
// function main begins program execution
int main()
{
string CollegeName; // string of characters to store the college name
CourseList myWelcome; // create a CourseList object named myWelcome
myWelcome.displayMessage(CollegeName);
} // end main
这就是我的作业,并试图完成上述任务。对我的作业或一般的 C++ 的任何帮助,将不胜感激。
最佳答案
看来你对编程很陌生。 您的代码中存在几个问题,您的方法不明确。以下是修改后的代码。
#include <iostream>
#include <string> // program uses C++ standard string class
using namespace std;
// CourseList class definition
class CourseList
{
private:
// string here
string nameOfCollege;
public:
void mysetCollegeName()
{
nameOfCollege = "Smart Peoples University";
}
// function that displays a welcome message to the CourseList user
void displayMessage() const
{
cout << "Welcome to the CourseList Program\n\n\n"
<< "To see the classes offered at " << nameOfCollege << ", type something, then press 'Enter'.";
} // end function displayMessage
}; // end class GradeBook
// function main begins program execution
int main()
{
string CollegeName; // string of characters to store the college name
CourseList myWelcome; // create a CourseList object named myWelcome
myWelcome.mysetCollegeName();
myWelcome.displayMessage();
} // end main
// * DJ Tonedeaf *
关于C++ 字符串和设置/获取函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35144926/