C ++ Class Methods

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

I made a class in Netbeans that made Rectangle.h and Rectangle.cpp. I am trying to add methods that infer the area and perimeter of the rectangles of l and w variables. I do not know how to create methods in the class and how to include them in the Rectangle.h file.

Here is what I am 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

+4
source share
3 answers

In the .h file, you have a class definition where you write member variables en-member functions (usually a prototype)

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

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

But, like gmannickg , you should read a book about C ++ or a real tutorial that will explain to you how the syntax works. And object oriented programming (if you are not familiar with it)

+7
source

Pretty simple - this is just an example and one possible implementation. Note that the following adds some additional things (like const and constructor) that you don't necessarily need; depending on your use.

 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; } 
+3
source

The header file (.h) is mainly about specifying an interface. Although you can also perform functions there, you usually do not. Instead, you define the class in the header, and then implement it in the .cpp file (.hpp, whatever). For example, your rectangle class:

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

Source: https://habr.com/ru/post/1403554/


All Articles