C ++字符串和set / get函数

时间:2016-02-02 04:14:21

标签: c++ string

C ++的新手。可以在课程上使用这个作业的一些帮助。 作业要求是:

  

创建一个名为CourseList的类,其中包含一个实例变量 -   nameOfCollege(类型String)。提供displayMessage成员函数   欢迎用户加入CourseList计划。提供一组功能   设置学院的名称,以及检索名称的get函数   高校。 set函数应该有一个字符串参数   学院的名字。使用函数main()来演示类课程   能力。

为了演示我们对类的理解,我们要创建带有对象和带函数的类。最终结果是一个控制台应用程序,用于查看假学院提供的假类。

目前,我的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 ++的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您似乎对编程非常陌生。 您的代码中存在多个问题,并且您的方法不明确。以下是修改后的代码。

#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 *  

答案 1 :(得分:1)

此代码中存在许多问题。

  1. 你有两对不同名称的get和set函数;只保留一个
  2. 由于您是在同一个类中定义函数,因此在函数名称之前不需要CourseList::
  3. 你的get函数返回一个空字符串,这是没用的; return nameOfCollege;在这里会更有意义
  4. 在main()函数中,您没有初始化CollegeName因此displayMessage()不会显示任何学院名称