C++ C+中的模块化+;

C++ C+中的模块化+;,c++,modularization,C++,Modularization,我目前正在编写一个(非常基础的)程序,这是一个编程教程(我知道,鉴于我的知识,这很讽刺)。我被指示将代码模块化,以便每个单元都位于自己的模块中。我猜这意味着添加标题?我正在和视觉工作室合作,如果这有帮助的话。我在下面加入了我的代码,以帮助我糟糕的解释有意义。谢谢你能提供的任何帮助 #include <iostream> #include <iomanip> #include <string> using namespace std; int Total; i

我目前正在编写一个(非常基础的)程序,这是一个编程教程(我知道,鉴于我的知识,这很讽刺)。我被指示将代码模块化,以便每个单元都位于自己的模块中。我猜这意味着添加标题?我正在和视觉工作室合作,如果这有帮助的话。我在下面加入了我的代码,以帮助我糟糕的解释有意义。谢谢你能提供的任何帮助

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int Total;
int ans;

class Question
{
private:
    string Question_Text;
    string Answer_one;
    string Answer_two;
    string Answer_three;

    int Correct_Answer;
    int Question_Score;

public:
    void setValues(string, string, string, string, int, int);
    void askQuestion();
};

int main()
{
    string username = "";
    char choice=' ';
    char c;
    int x = 4;
    int y = 5;
    int z = x + y;


    //welcome message
    cout << "Hello user, please enter your name:";
    cin >> username;
    cout << "Welcome to the programming tutorial " << username << "." << endl;

    //menu selection
    while(choice != '5')
    {
        cout << "What would you like to do? (Unit 1 - Declaring Variables (1), Unit 2 - Input/ Output (2), Unit 3 - Conditionals (3), Quizzes (4) or Exit (5))";
        cin >> choice;
        if (choice == '1')
        {
            cout << "We will begin with defining variables. The first step to doing this is choosing which datatype your variable is.\n";
            cout << "The following are a few of the common datatypes used in programming.\n";
            cout << "Character ==> char\n";
            cout << "Integer ==> int, long, double\n";
            cout << "Boolean ==> bool\n";
            cout << endl;
            cout << "When declaring a variable, you must put its datatype before the variable name.\n";
            cout << "An example of this would be if we wanted to declare the value of x as 4.\n";
            cout << "We would write this as: \n";
            cout << "int x = 4\n";
            cout << "The program will now use the value 4 for the variable name 'x'\n";
            cout << endl;
            cout << "Now let's assume we assigned the value of 5 to the variable 'y'\n";
            cout << "If we wanted to add x and y and assign the sum to the variable 'z', we would write:\n";
            cout << "int z = x + y\n";
            cout << "Now when we use the variable 'z' in our program, it will perform the calculation given x=4 and y=5 and declare 9 as the value of the variable 'z'.\n";
            cout << "To test our code, we would write: " << endl;
            cout << "cout<<'x + y'<< z << endl; \n";
            cout << "If written correctly, it will display as: \n";
            cout << "x + y = " << z << "." << endl;

        }
        if (choice == '2')
        {
            cout << "Now that we understand the basics of declaring variables, let's discuss displaying, or output of, information to a user.\n";
            cout << "If you wanted to display a welcome message, for example, you would type:\n";
            cout << "cout << 'Welcome';\n";
            cout << "The line of code would start with 'cout' followed by two less than signs and then the message you wish to display in quotes.\n";
            cout << "Using this, you can ask the user for input.\n";
            cout << "Enter c to continue...";
            cin >> c;
            cout << "Let's say we have a program that flips a coin. You may want to ask the user how many times to flip the coin.\n";
            cout << "Assuming we previously declared this amount variable as 'int timesFlipped', we would 'cout' our question and the next line would read:\n";
            cout << "cin>> timesFlipped; \n";
            cout << "This will store the users input for the variable 'timesFlipped'\n";
            cout << "You almost always end a line of code with a semi colon." << endl;
        }
        if (choice == '3')
        {
            cout << "This unit will cover conditional expressions." << endl;
        }
        if (choice == '4')
        {
            string Question_Text;
            string Answer_one;
            string Answer_two;
            string Answer_three;

            int Correct_Answer;
            int Question_Score;
            Question q1;
            Question q2;
            Question q3;


            cout << username << ", you have chosen to take a quiz." << endl << endl;
            int ans, score = 0;
            cout << "Unit One Quiz - Variables " << endl << endl;

            q1.setValues("How would you declare the value of 'x' as 12? ",
                "x=12()",
                "x==12()",
                "x=12;()",
                3,
                1);
            q2.setValues("What do you need to put before a variable when declaring it?",
                "a name()",
                "a value()",
                "a datatype()",
                3,
                1);
            q3.setValues("Which data type would you use for a number that includes a decimal value?",
                "int()",
                "double()",
                "float()",
                2,
                1);

            q1.askQuestion();
            q2.askQuestion();
            q3.askQuestion();

            cout << "Your score out of a possible 3 is " << Total << endl;

        }
        if (choice == 'E')
        {
            cout << "Have a good day!";
            break;
        }
    }


    system("pause");
}

void Question::setValues(string q, string a1, string a2, string a3, int ca, int pa)
{

    Question_Text = q;
    Answer_one = a1;
    Answer_two = a2;
    Answer_three = a3;
    Correct_Answer = ca;
    Question_Score = pa;
}

void Question::askQuestion()
{

    cout << endl;
    cout << Question_Text << endl;
    cout << "1. " << Answer_one << endl;
    cout << "2. " << Answer_two << endl;
    cout << "3. " << Answer_three << endl << endl;
    cout << "Please enter your answer: " << endl;
    cin >> ans;

    if (ans == Correct_Answer)
    {
        cout << "That is correct!" << endl;
        Total = Total + Question_Score;
    }
    else
    {
        cout << "Sorry, that is incorrect" << endl;
        cout << "The correct answer was " << Correct_Answer << endl;
    }
}
#包括
#包括
#包括
使用名称空间std;
整数合计;
INTANS;
课堂提问
{
私人:
字符串问题\文本;
一串答案;
字串答案二;
串答三;
正确答案;
智力测验成绩;
公众:
void setValues(string,string,string,string,int,int);
无效askQuestion();
};
int main()
{
字符串username=“”;
字符选择=“”;
字符c;
int x=4;
int y=5;
intz=x+y;
//欢迎辞
cout>用户名;
库特
我猜这意味着添加标题

差不多就是这个意思

在您的情况下,您可能需要:

  • 创建包含类声明的标题名
    Question.h
  • 创建一个源文件名
    Question.cpp
    并将类定义移到那里,即所有函数,如
    void Question::askQuestion()
  • 创建一个测试文件名
    test.cpp
    以放置
    main
    函数,并记住包含
    Question.h

当您使用Visual Studio时,您可以提前创建一个项目,然后在编译/构建之前添加所有这些文件。

在choice==“4”中,您声明了变量:
字符串问题\u文本;字符串答案\u一;字符串答案\u二;字符串答案\u三;
我认为没有使用这些文件。请执行您需要执行的操作以保留teac她的快乐和顺利通过,但有余地,在好的,可读代码的一边出错。你会发现它比闪电快的代码更重要。作为一个几乎普遍的规则,任何教授C++的教授几乎一无所知,所以他们所说的一切都是毫无疑问的,对任何ADVI都非常怀疑。这是一个例外,但是他们很难做到。你可以尽可能的去获得一个好的分数,但是不要让他们的建议破坏你对C++的理解。太糟糕了,我想试试。为了使情况更糟,他们用Python开始了我们,一个学期,然后去了C++的HaaHead彼得的观点:<代码>如果(选择==‘1’){maging block of code}
可以是
if(choice='1'){do_choice_1();}
,而
do_choice_1()
函数包含巨大的代码块。这将有助于将
函数保持在一个合理的大小,这样人类就有机会在他们的大脑中适应并理解它。我听过和读过很多关于函数应该有多大的指标,但一般的要点是“做一件事并把它做好”你怎么这么肯定教程不是关于C++20模块的?我不是“那么肯定”。无论如何,这是一个建议。非常感谢,这非常有帮助!!不管出于什么原因,@einpoklum似乎开始纠缠我的问题,因为我在这方面是新手,显然他们需要做出明智的评论来让自己觉得有效。不客气。人们有不同的意见,请放心。@artm如果这是他妈的第一个评论,我会同意的德洛尔。