gpt4 book ai didi

c++ - 将结构转换为类并使用构造函数、修改器和访问器

转载 作者:行者123 更新时间:2023-11-30 03:49:16 30 4
gpt4 key购买 nike

我有一个问题,我正在努力解决这个问题。我得到了一个struct:

struct Module
{
string moduleName;
string moduleCode;
string lecturer;
string nrStudents;
}

我被要求执行以下操作:

  1. Turn the struct into a class
  2. Make all member variables private
  3. Include public member functions for each of the following
    • a default constructor that sets the string member variables to blank strings and int to 0.
    • an overloaded constructor that sets the string member variables to specific values.
    • member functions to set each of the member variables to a value given as an argument to the function i.e. mutators.
    • member functions to retrieve the data from each of the member variables i.e. accessors.

Test the class in a program that instantiates an object of class Module (i.e. ‘declare’ an object of ‘type’ Module). The program should then input values for the object (obtained from the keyboard), and use the mutators to assign values to the member variables. Use the accessors to obtain the values of the member variables of the object and display those values on the screen. Test your program with the following input:
Input for member variables of Module object:

到目前为止,这是我的代码:

模块名称:编程导论
模块代码:CSS1515
讲师:Mrs Smith
学生人数:250

#include <iostream>
#include <cstdlib>
using namespace std;

class module
{
private:
string moduleNameVar;
string moduleCodeVar;
string lecturerVar;
int nrStudentsVar;

public:
module( );
//initializes moduleName, moduleCode and lecturer to a blank string
//initializes nrStudent to 0
module(string moduleName, string moduleCode, string lecturer, int nrStudents);
//initializes moduleName, moduleCode and lecturer to a string value
//initializes nrStudent to a number of students
void set(string moduleName, string moduleCode, string lecturer, int nrStudents);
//sets the moduleName, moduleCode, lecturer and nrStudents
};

int main()
{
module exam1("Introduction to Programming","COS1515","Mrs Smith", 250);
}
module::module( ) : moduleNameVar(""), moduleCodeVar(""), lecturerVar(""), nrStudentsVar(0)
{
//body intentionally empty
}

module::module(string moduleName, string moduleCode, string lecturer, int nrStudents)
{
module::set();
}

void module::set(string moduleName, string moduleCode, string lecturer, int nrStudents)
{
moduleNameVar = moduleName;
moduleCodeVar = moduleCode;
lecturerVar = lecturer;
nrStudentsVar = nrStudents;
}

现在我想了解的是为什么在我已经定义了一个可以有效设置我的成员变量的构造函数时要求我使用增变器。尽管如此,我正在尝试使用:module::set(); 来实现这一点,但这会在编译器中引发错误:no function for call to module::set()

我在这里错过了什么?我该如何着手创建增变函数和构造函数?

注意:访问函数似乎很简单,因为我只需要使用 get() 函数来访问成员变量?

最佳答案

How do I go about creating the mutator functions as well as the constructor function?

非常简单,您只需添加一个公共(public)成员函数,该函数采用适当类型的参数,并将变量设置为其值。例如,模块名称的修改器应如下所示:

void setModuleName(const string &name){ 
moduleNameVar = name;
}

然后其他类似的功能。

访问器只会从您的类中返回成员变量的值。例如:

std::string getModuleName() const{ return moduleNameVar; }

和其他类似的功能。函数声明末尾的“const”表示使用此函数不会更改您的类中任何内容的值(这不是绝对必要的,但包含是一种很好的做法)。

Nonetheless, I am trying to achieve this using

module::set();

but this is throwing up an error in the compiler "no function for call to 'module::set()'

您尚未定义与您尝试使用的函数匹配的函数。你只定义了

module::set(string moduleName, string moduleCode, string lecturer, int nrStudents) 

它有四个参数,并且这些参数中的任何一个都没有默认值。但是你试图在没有任何参数的情况下使用它 - 编译器无法知道你想要它在这种情况下做什么,所以它会给出错误。

关于c++ - 将结构转换为类并使用构造函数、修改器和访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668864/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com