C++ class methods
Solution 1
In the .h file you have the class definition, where you write down the member variables en member functions (generally as prototype)
In the .cpp file you declare the methods body. 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 said 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)
Solution 2
Quite easy - this is just an example and one possible implementation. Note that the following adds some additional stuff (like const
and a constructor) you don't necessarily need; depending on your useage.
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;
}
Solution 3
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:
// 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;
}
Lucas
Updated on December 13, 2020Comments
-
Lucas over 3 years
I am learning C++ and I have a question.
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
l
andw
variables. I don't know how to create methods in a class and how to incorporate them in the Rectangle.h file.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
-
Lucas about 12 yearsWhat, exactly, is a constructor?
-
grifos about 12 yearsA constructor is a method from a class that will be called when the class object is instaciated. You really need to read something that explain c++ and oop.
-
Lucas about 12 yearsYou're right. I need to get a good C++ book. Thanks for explaining.