C++ 类方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9880642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:22:13  来源:igfitidea点击:

C++ class methods

c++classobjectnetbeansmethods

提问by Lucas

I am learning C++ and I have a question.

我正在学习 C++,我有一个问题。

I made a class in Netbeans, which made Rectangle.h and Rectangle.cpp. I am trying to add methods that output the Area and Perimeter of the rectangle's land wvariables. I don't know how to create methods in a class and how to incorporate them in the Rectangle.h file.

我在 Netbeans 中创建了一个类,它创建了 Rectangle.h 和 Rectangle.cpp。我正在尝试添加输出矩形lw变量的面积和周长的方法。我不知道如何在类中创建方法以及如何将它们合并到 Rectangle.h 文件中。

Here's what I'm trying to do:

这是我想要做的:

Rectangle rct;
rct.l = 7;
rct.w = 4;
cout << "Area is " << rct.Area() << endl;
cout << "Perim is " << rct.Perim() << endl;

Can someone explain how to do this? I'm so confused.

有人可以解释如何做到这一点吗?我很困惑。

Thanks,

谢谢,

Lucas

卢卡斯

回答by grifos

In the .h file you have the class definition, where you write down the member variables en member functions (generally as prototype)

在 .h 文件中,您有类定义,您可以在其中写下成员变量和成员函数(通常作为原型)

In the .cpp file you declare the methods body. Example:

在 .cpp 文件中声明方法主体。例子:

rectangle.h:

矩形.h:

class rectangle
{
    public:
    // Variables (btw public member variables are not a good 
    //   practice, you should set them as private and access them 
    //   via accessor methods, that is what encapsulation is)
    double l;
    double w;

    // constructor
    rectangle();
    // Methods
    double area();
    double perim();
};

rectangle.cpp:

矩形.cpp:

#include "rectangle.h" // You include the class description

// Contructor
rectangle::rectangle()
{
   this->l = 0;
   this->w = 0;
}

// Methods
double rectangle::area()
{
   return this->w * this->l;
}

double rectangle::perim()
{
   return 2*this->w + 2*this->l;
}

But like gmannickgsaid you should read a book about c++ or a real tutorial, that will explain you how the syntax works. And Object Oriented Programming (if you are not familiar with it)

但是就像gmannickg说的,你应该阅读一本关于 C++ 的书或一个真正的教程,它们会向你解释语法是如何工作的。和面向对象编程(如果你不熟悉的话)

回答by Mario

Quite easy - this is just an example and one possible implementation. Note that the following adds some additional stuff (like constand a constructor) you don't necessarily need; depending on your useage.

很简单 - 这只是一个例子和一种可能的实现。请注意,以下添加了一些const您不一定需要的其他内容(例如和构造函数);取决于您的用途。

class Rectangle {
    private:
    double l, w;

    // This constructor has optional arguments, meaning you can skip them (which will result in them being set to 0).
    public:
    Rectangle(const double l = 0, const double w = 0);

    double Area(void) const; // the const keyword after the parameter list tells the compiler that this method won't modify the actual object
    double Perim(void) const;
}

Rectangle::Rectangle(const double _l, const double _w) : l(_l), w(_w) { // this is an initializer list - as an alternative, here it would be possible to just assign the values inside the function body
}

double Rectangle::Area(void) const {
    return l * w;
}

double Rectangle::Perim(void) const {
    return l + l + w + w;
}

回答by Ed S.

The header (.h) file is mainly concerned with specifying the interface. While you can implement functions there as well, you typically do not. Instead, you define the class in the header, and then implement it in the .cpp (.hpp, whatever) file. For example, your rectangle class:

头文件 (.h) 主要与指定接口有关。虽然您也可以在那里实现功能,但通常不会。相反,您在头文件中定义类,然后在 .cpp(.hpp,随便什么)文件中实现它。例如,您的矩形类:

// Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle {
public:
    // constructors, just initialize our private members
    Rectangle(int x, int y, int w, int h) 
        : _x(x), _y(y), _w(w), _h(h) { }

    Rectangle() : _x(0), _y(0), _w(0), _h(0) { }

    // implement these in Rectangle.cpp
    int get_x();
    void set_x(int x);

    int get_y();
    void set_y(int y);

    int get_width();
    void set_width(int w);

    int get_height();
    void set_height(int h);

    int Area();
    int Perim();

private:
    int _x, _y, _w, _h;
};

#endif


// Rectangle.cpp
#include "Rectangle.h"
#include <algorithm>

using std::max;

int Rectangle::get_x() {
    return _x;
}

void Rectangle::set_x(int x) {
    _x = x;
}

int Rectangle::get_y() {
    return _y;
}

void Rectangle::set_y(int y) {
    _y = y;
}

int Rectangle::get_width() {
    return _w;
}

void Rectangle::set_width(int w) {
    _w = max(w, 0);
}

int Rectangle::get_height() {
    return _h;
}

void Rectangle::set_height(int h) {
    _h = max(h, 0);
}

int Rectangle::Area() {
    return _w * _h;
}

int Rectangle::Perim() { 
    return _w * 2 + _h * 2;
}