Introduction
In this tutorial you can learn how to make your own header files for working with geometry in C++. In the header files will be classes for operations.
Background
C++ class development is not so clean (such as C# or VB OOP), but it's very usable for fast program creation and memory-save + fast operations. Before you start reading
this article you must know basics of C++ OOP and you should understand inheritance.
Note
This articles code was written in Visual Studio 2010 and compiled via default compiler.
Using the code
Ok, so we should start.
You should start you favorite IDE and make new project. In the project create new folder which will contains our header files. After that create new header
file "GeometricObject" (extension .h
or .hpp
depending on compiler).
Now, create new file "Square". In this header we will make simple class named Square
for
calculating Area and Circuit data based on side size.
class Square
{
public:
Square(){this->side = 0;};
Square(double sideSize){this->side = sideSize;};
double GetSide() const{return this->side;};
void SetSide(double size) {this->side = size;};
double Area();
double Circuit();
private:
double side;
};
double Square::Area()
{
return this->side * this->side;
}
double Square::Circuit()
{
return this->side * 4;
}
This class is so simple. We're creating constructors, side manipulation functions and function for getting Area and Circuit.
The main (private) side data storage variable is side
. Next, we'll make class named Rectangle whitch is similair and bit more difficult
than Square
class.
class Rectangle
{
public:
Rectangle() {this->width = 0;this->height = 0;}
Rectangle(double Width, double Height){this->height = Height;this->width = Width;}
void SetSides(double Width, double Height){this->height = Height;this->width = Width;}
void GetSides(double *Width, double *Height)const;
double GetWidth() const {return width;}
double GetHeight() const {return height;}
double Area() const;
double Circuit()const;
private:
double width, height;
};
void Rectangle::GetSides(double *Width, double *Height) const
{
*Width = this->GetWidth();
*Height = this->GetHeight();
}
double Rectangle::Area() const
{
return width * height;
}
double Rectangle::Circuit() const
{
return width * 2 + 2 * height;
}
This class also contains basic side manipulation methods. It's usable for calculating simple parametres of normal mathematical/geometrical Square.
In next header file ("Circle") we will make circle class and we will define new constant PI
whitch will be used for calculating
Circuit
and Area
of circle.
#ifndef PI
#define PI 3.142857
#endif
class Circle
{
public:
Circle(){this->r = 0;};
Circle(double r){this->r = r;};
double Getr() const{return this->r;};
void Setr(double r){this->r = r;};
double Area() const;
double Circuit() const;
private:
double r;
};
double Circle::Area() const
{
return PI * (this->r * this->r);
}
double Circle::Circuit() const
{
return 2 * (PI * this->r);
}
Like before, here we have similair functions for getting user important data. We're using pi*r*r for getting Area and 2pi*r for getting Circuit.
In the last header, 'Triangle' we're making basic triangle class based on v
, a
, b
,
c
whitch are important for mathematical formulas.
class Triangle
{
public:
Triangle(){this->v = this->a = this->b = this->c = 0;}
Triangle(double v, double a, double b, double c){this->v = v;this->a = a;this->b = b;this->c = c;}
void Setvabce(double v, double a, double b, double c){this->v = v;this->a = a;this->b = b;this->c = c;}
void Getvabce(double *v, double *a, double *b, double *c)
const {*v = this->v;*a = this->a;*b = this->b;*c = this->c;}
double Area();
double Circuit();
private:
double v, a, b, c;
};
double Triangle::Area()
{
return v * a / 2;
}
double Triangle::Circuit()
{
return a + b + c;
}
Example code
#include <iostream>
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include "Triangle.h"
using namespace std;
int main(int argc, char **argv)
{
double a = 0;
Circle c(10.5);
cout << "Circuit of circle 'c': " << c.Circuit() << endl;
Rectangle r(10, 50);
cout << "Area of rectangle 'r': " << r.Area() << endl;
Square s(5.758);
cout << "Area of square 's': " << s.Area() << endl;
Triangle t(5, 7, 7, 7);
cout << "Circuit of triangle 't': " << t.Circuit() << endl;
cin >> a;
return 0;
}
Points of Interest
As you can see in all codes, we have data stored in private variables like width
, r
, v
.
For accessing these data we're using methods like Getvabc or Setr. This we can done in another languages with new elements like Properties
in C#, whitch can have get and set operations set as methods. This is avaible with little hacks in C++, but it is. There's a lot of free unextended and unpatched
space whitch you can use and for whitch you can make extensions. Extensions and libraries are making programming of bigger applications simpler.
History
There were not any updates of article.