我应该如何写课程? C ++ [英] How should I write classes? C++

查看:180
本文介绍了我应该如何写课程? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿..我真的不会得到他们。我读了一个关于C ++中的类的教程,我不会得到一些东西:
在我看过的每个例子和教程中,函数都不会写在类中!例如,为什么写这样的类:

Hey.. I don't really get them. I read a tutorial about classes in C++, and I don't get a few things: In every example and tutorial that I've seen, functions are never written inside the class! For example, why write a class like this:

#include <iostream>

using namespace std;

class test
{
    private:
        int x, y;
    public:
        test (int, int);
        int tester () {return x + y; }
};

test::test (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    test atest (3, 2);
    test atest2 (2, 6);

    cout << "test1: " << atest.tester() << endl;
    cout << "test2: " << atest2.tester() << endl;

    return 0;
}

或像这样:

#include <iostream>

using namespace std;

class test
{
    private:
        int x, y;
    public:
        void set_values (int,int);
        int testfunc () {return x + y; }
};

void test::set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    test tester;

    tester.set_values (3, 2);

    cout << "test1: " << tester.testfunc() << endl;

    return 0;
}

,而不是像这样:

#include <iostream>

using namespace std;

class test
{
    public:
        int tester (int x, int y) { return x + y; }
};

int main()
{
    test atest;

    cout << atest.tester(3, 2) << endl;

    return 0;
}



老实说,我只是不能得到它!

Honestly, I just don't get it!

为什么我需要私有成员?

Why do I need private members??

我应该何时使用析构函数,如何使用析构函数?

When and how should I use destructors?

我应该如何写我的课?

我很困惑这里,我真的需要一个人为我清理的东西...感谢

I'm very confused here and I really need somebody to clear up things for me... thanks

推荐答案

示例合理地分离出设置和检索值,因为它们之间有更多的内容, 。如果没有,你是完全正确的,你应该结合他们,甚至消除类,有利于一个自由的功能。

The example plausibly separates out the setting and retrieving of values because there's more in-between those than you see in the given main function. If there wasn't, you're completely right that you should combine them, and even eliminate the class in favor of a free function.

例如,代码可以看起来像:

For example, the code could look something like:

void example(Test obj) {
  if (something) {
    process(obj.tester());
  }
}

int main() {
  int x, y;  // Imagine these are assigned to user input.
  Test obj (x, y);
  example(obj);  // Obj.tester may or may not be used.  If the x + y was not
  // separated, then you couldn't "maybe use" it.
  return 0;
}




为什么我需要私人会员? p>

Why do I need private members?

您没有。他们是更多的文档比任何其他。可访问性(public / protected / private)是由编译器检查的文档,但是。这个检查有用的主要是为了封装值,但封装是多得多,更多的
,而不只是标记为非公开。例如,如果(从公共方法)返回对非公开数据成员的引用,则已将该成员的实现绑定到类的公共接口。

You don't. They're more documentation than anything else. Accessibility (public/protected/private) is documentation which is checked by the compiler, however. This check is helpful mostly in order to encapsulate values, but encapsulation is much, much more than just marking something as non-public. For example, if you (from a public method) return a reference to a non-public data member, you've tied the implementation of that member to the class' public interface.


何时和如何使用析构函数?

When and how should I use destructors?

了解三维规则。写入它们,就像它们是一个名为〜的类方法加上类名,没有参数,并省略一个返回类型(甚至void),类似于ctors。

When you need special destruction logic. Read about the Rule of Three. Write them as if they were a class method named "~" plus the class name, taking no parameters and omit a return type (even void), similarly as for ctors.

我应该如何一般写我的课?

How should I generally write my classes?

班上。事实上,实现类中的所有方法开始使用,然后将方法移出,因为有需要。当你学习语言的重要基础时,这很难拧紧,对于你最初写的类的类型来说更方便。使用ctor初始化器,当它们在ctor主体中作为可执行的分配。

You don't have to define the methods outside of the class. In fact, implement all methods inside the class to start out with, then move the methods out as there is a need. This is much harder to screw up while you learn the important basics of the language, and much more convenient for the types of classes you'll write initially. Use ctor initializers when they are just as feasible as assignment within the ctor body.

struct Test {
  Test(int x, int y)  // Use the same names rather than inventing 'a' and 'b'.
  : _x (x), _y (y)
  {}

  int tester() const { return _x + _y; }
  // Move outside the class when needed, if at all -- and it won't be needed
  // for a function like this.
  // Because this doesn't modify anything, it's suitable to be const, which
  // means it can be called on a const Test object.

  int _x, _y;
  // Technically public, but with a "non-public name".  Mark private as you
  // wish, or as the design settles down.
};

注意我使用struct来声明这个类而不是class。结果是一样的,除了我跳过public对任何基类(没有在这里,但基地更常常是公共的,而不是),默认成员可访问性也是公共:这导致更短和稍微更多清除代码。

Note I've used "struct" to declare this class instead of "class". The result is identical, except I get to skip "public" on any base classes (there are none here, but bases are more often public than not) and the default member accessibility is "public" too: this results in shorter and slightly more clear code.

这篇关于我应该如何写课程? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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