2012-03-26 9 views
4

私はC++を学んでいますが、私には質問があります。C++クラスのメソッド

私はNetbeansでRectangle.hとRectangle.cppを作成しました。私は、矩形のlw変数のAreaとPerimeterを出力するメソッドを追加しようとしています。私は、クラス内でメソッドを作成する方法、およびそれらをRectangle.hファイルに組み込む方法を知らない。ここで

は私がやろうとしているものです:

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

誰かがこれを行う方法を説明できますか?私は困惑している。

おかげで、.cppファイルで使用すると、(一般的にプロトタイプとして)メンバ関数エンメンバ変数を書き留めあなたはクラス定義を持っている.hファイル、

ルーカス

+8

それはよく書かれた質問ですが、あなたには、いくつかの教材を読まずにC++のような言語を学ぶことができるようにするつもりはないことに注意してくださいありません。推測とハッキングは将来あなたのプログラミングを傷つけるだけです。私は[このリスト](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)からの初心者の本をお勧めします。あなたの質問に答えて、あなたを守ります。 – GManNickG

+0

+1誰かにコードを渡してもらいたいのではなく、説明を求めるための+1。 –

答えて

7

メソッド本体を宣言します。例:

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++に関する本や本物のチュートリアル、その意志を読むべきであると述べました構文の仕組みを説明してください。オブジェクト指向プログラミング(あなたがそれに精通していない場合)

+0

ありがとう!これは素晴らしい作品です。 – Lucas

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; 
} 
+0

正確にコンストラクタは何ですか? – Lucas

+0

コンストラクタは、クラスオブジェクトがインストゥルメントされたときに呼び出されるクラスのメソッドです。あなたは本当にC++とoopを説明するものを読む必要があります。 – grifos

+0

あなたはそうです。私は良いC + +の本を取得する必要があります。説明してくれてありがとう。 – Lucas

1

ヘッダー(.h)ファイルは、主にインターフェイスの指定に関係しています。そこに関数を実装することはできますが、通常はそうしないでください。代わりに、ヘッダーにクラスを定義し、.cppファイル(.hpp、何でも)で実装します。たとえば、あなたのRectangleクラス:

// 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; 
} 
+0

OK、それを得ました。ありがとう! – Lucas

関連する問題