Click here to Skip to main content
Click here to Skip to main content

Making C++ Geometry headers

, 2 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Article about making C++ header files for simple 2D geometry objects.


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. 


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. 


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. 

// Square.h
class Square
        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();
        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.

// Rectangle.h

class Rectangle
        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;
        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. 

// Circle.h

#ifndef PI
    #define PI 3.142857
class Circle
        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;
        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.  

// Triangle.h
class Triangle
        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();
        double v, a, b, c;
double Triangle::Area()
    return v * a / 2;
double Triangle::Circuit()
    return a + b + c;

Example code  

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


There were not any updates of article. 


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


About the Author


United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 1 PinmemberPaul Conrad18-Jun-12 8:11 
QuestionThese should be template based PinmemberHarrison H5-Apr-12 13:40 
Generalsuggestion Pinmemberliu mingzan3-Apr-12 22:51 
QuestionInheritance PinmemberRick York2-Apr-12 12:33 
AnswerRe: Inheritance Pinmembermluri2-Apr-12 20:11 
GeneralRe: Inheritance PinmemberRick York4-Apr-12 10:44 
AnswerRe: Inheritance PinmemberFrewCen3-Apr-12 4:29 
AnswerRe: Inheritance PinmemberPJ Arends3-Apr-12 20:47 
GeneralRe: Inheritance PinmemberFrewCen4-Apr-12 1:07 
GeneralRe: Inheritance PinmemberPJ Arends4-Apr-12 6:19 
QuestionTriange v? Pinmemberrdw2-Apr-12 10:53 
AnswerRe: Triange v? PinmemberFrewCen3-Apr-12 4:28 
GeneralRe: Triange v? Pinmemberrdw17-Apr-12 1:52 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 2 Apr 2012
Article Copyright 2012 by FrewCen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid