你能帮我解决OOP问题吗? [英] Could you help me with OOP problems?

查看:81
本文介绍了你能帮我解决OOP问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一整天都在尝试使用C ++中的OOP编写代码。我不能这样做。我想问你应该从什么开始制作我的代码目标。

我的程序是从文件中读取,其中包含一行中的一个数字。最多有20个号码。我读了数据并将其打印出来。稍后我检查数字是否大于1并将更大的数字写入文件。





我的代码如下:



Hello, all day long I am trying somehow to write my code using OOP in c++. And I can't do it. I want to ask you from what I should start to make my code Objective.
My program is reading from file which contains one number in one line. There is max of 20 numbers. I read the data and print it out. Later I check if number is greater that 1 and write the greater numbers to file.


My code looks like:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main() {

ifstream myfile("stu.txt");
const size_t SIZE = 30;
int value[SIZE];
string line[SIZE]; // creates SIZE empty strings
size_t i=0;
while(!myfile.eof() && i < SIZE) {
  getline(myfile,line[i]); // read the next line into the next string
  ++i;
}
size_t numLines = i;
for (i=0; i < numLines; ++i) {
	value[i] = atoi(line[i].c_str());
  cout << i << ". " << value[i]<<'\n'; // no need to test for empty lines any more
}
for (i=0; i < numLines; ++i) {
	if(value[i] > 1)
	{
	cout << value[i]<<'\n';
	}
	}
	{
	ofstream myfile;
    myfile.open("file.txt");
    for (i=0; i < numLines; ++i) {
	if(value[i] > 1)
	{
	myfile << value[i]<<'\n';
	}
    }
    myfile.close();
}
}





例如我想要一个Objective类:





For Example I want to have a Objective class:

class NICE{
private:
public:
NICE();
void read();
void sort();
void writeSort():
};





我尝试了什么:





What I have tried:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;
class UZD {
private:
public:
    UZD();
    size_t i;
    size_t numLines;
    int value;
    void read();
    void sort();
    void printSorted();
};

UZD::UZD()
{
}

void UZD::read()
{
    ifstream myfile("stu.txt");
    const size_t SIZE = 30;
    int value[SIZE];
    string line[SIZE]; // creates SIZE empty strings
    while (!myfile.eof() && i < SIZE) {
        getline(myfile, line[i]); // read the next line into the next string
        ++i;
    }
    for (i = 0; i < numLines; ++i) {
        value[i] = atoi(line[i].c_str());
        cout << i << ". " << value[i] << '\n';

    }
}

void UZD::sort()
{
    for (i = 0; i < numLines; ++i) {
        if (value[i] > 1) {
            cout << value[i] << '\n';
        }
    }
}

void UZD::printSorted()
{
    numLines = i;
    ofstream myfile;
    myfile.open("file.txt");
    for (i = 0; i < numLines; ++i) {
        if (value[i] > 1) {
            myfile << value[i] << '\n';
        }
    }
    myfile.close();
}

int main()
{
    UZD wow;
    wow.read();
    wow.sort();
    wow.printSorted();
}





但我根本没有传递价值。我非常困惑。也许你可以帮帮我?



提前谢谢。



But I am not passing values at all. I confused very much. Maybe you could help me?

Thanks in advance.

推荐答案

有一个工具可以让你看看你的代码在做什么,它的名字是调试器,它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪个期望与现实相匹配。

当你不明白你的代码正在做或为什么它做它做的事情,答案是调试器



使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。



建议:找一个用C ++学习oop的教程
There is a tool that allow you to see what your code is doing, its name is debugger, it is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.

Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Advice: find a tutorial to learn oop with C++


Tutorialspoint 有很多教程,下面是关于C ++的教程: C ++ Tutorial [ ^ ]
Tutorialspoint has a plethora of tutorials, here is one about C++: C++ Tutorial[^]

这篇关于你能帮我解决OOP问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆