C++类方法

6

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

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

以下是我尝试实现的内容:

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

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

谢谢,

卢卡斯


9
这是一个写得不错的问题,但请注意,你无法在不阅读任何学习材料的情况下学习C++语言;猜测和胡乱编程只会对你未来的编程造成伤害。我建议从这个列表中选择一本入门书籍,它将回答你的问题并帮助你保持正确的学习方向。 - GManNickG
+1 是因为你询问了解释而不是只想让别人给你代码。 - Captain Obvlious
3个回答

10

在.h文件中,您编写类定义,其中包括成员变量和成员函数(通常作为原型)。

在.cpp文件中,您声明方法体。例如:

rectangle.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:

#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;
}

不过像gmannickg所说,你应该阅读一本关于C ++的书籍或真正的教程,这将为您解释语法如何工作。还有面向对象编程(如果您对此不熟悉)


3
相当简单 - 这只是一个示例和一种可能的实现。请注意,以下内容添加了一些额外的东西(例如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;
}

什么是构造函数? - Lucas
构造函数是一个类中的方法,当类对象被实例化时将被调用。你真的需要阅读一些解释C++和OOP的东西。 - grifos
你说得对。我需要找一本好的C++书。谢谢你的解释。 - Lucas

1

头文件(.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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接