Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Visual C++ 10.0

Making C++ Geometry headers

Rate me:
Please Sign up or sign in to vote.
3.53/5 (7 votes)
2 Apr 2012CPOL2 min read 35.7K   265   4   13
Article about making C++ header files for simple 2D geometry objects.

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. 

C++
//
// Square.h
//
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.

C++
//
// Rectangle.h
//

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. 

C++
//
// Circle.h
//

#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.  

C++
//
// Triangle.h
//
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  

C++
//
// main.cpp
//

#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. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Paul Conrad18-Jun-12 8:11
professionalPaul Conrad18-Jun-12 8:11 
QuestionThese should be template based Pin
Harrison H5-Apr-12 13:40
Harrison H5-Apr-12 13:40 
Generalsuggestion Pin
liu mingzan3-Apr-12 22:51
liu mingzan3-Apr-12 22:51 
QuestionInheritance Pin
Rick York2-Apr-12 12:33
mveRick York2-Apr-12 12:33 
AnswerRe: Inheritance Pin
mluri2-Apr-12 20:11
mluri2-Apr-12 20:11 
GeneralRe: Inheritance Pin
Rick York4-Apr-12 10:44
mveRick York4-Apr-12 10:44 
AnswerRe: Inheritance Pin
FrewCen3-Apr-12 4:29
FrewCen3-Apr-12 4:29 
AnswerRe: Inheritance Pin
PJ Arends3-Apr-12 20:47
professionalPJ Arends3-Apr-12 20:47 
GeneralRe: Inheritance Pin
FrewCen4-Apr-12 1:07
FrewCen4-Apr-12 1:07 
GeneralRe: Inheritance Pin
PJ Arends4-Apr-12 6:19
professionalPJ Arends4-Apr-12 6:19 
QuestionTriange v? Pin
rdw2-Apr-12 10:53
rdw2-Apr-12 10:53 
AnswerRe: Triange v? Pin
FrewCen3-Apr-12 4:28
FrewCen3-Apr-12 4:28 
GeneralRe: Triange v? Pin
rdw17-Apr-12 1:52
rdw17-Apr-12 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.