C++ 字符串和设置/获取函数

标签 c++ string

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/

相关文章:

c++ - 在 C++ 和 Fortran 代码之间传递复数数组

c++ - 代码块注释掉整个 block

c++ - 解析以扩展 SMTP (ESMTP) 结尾的响应

c - `strcmp` 的返回值的顺序是什么?

sql - 使用给定模式在 Postgres 中生成列

c++ - 如何在 Boosts 的 brent_find_minima() 中传递一些常量?

c++ - 这个插值搜索实现有什么问题?

c - 我想计算字符串 C 程序中每个字母的频率或出现次数

用于搜索子字符串的Python正则表达式

python - 如何对 Python 字符串中的特殊字符进行转义?