15,968,413 members
Articles / Programming Languages / Visual C++ 10.0

Rate me:
2 Apr 2012CPOL2 min read 36.6K   268   4   13

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

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

 First Prev Next
 These should be template based Harrison H5-Apr-12 13:40 Harrison H 5-Apr-12 13:40
 suggestion liu mingzan3-Apr-12 22:51 liu mingzan 3-Apr-12 22:51
 Inheritance Rick York2-Apr-12 12:33 Rick York 2-Apr-12 12:33
 Re: Inheritance mluri2-Apr-12 20:11 mluri 2-Apr-12 20:11
 Re: Inheritance Rick York4-Apr-12 10:44 Rick York 4-Apr-12 10:44
 Re: Inheritance FrewCen3-Apr-12 4:29 FrewCen 3-Apr-12 4:29
 Re: Inheritance PJ Arends3-Apr-12 20:47 PJ Arends 3-Apr-12 20:47
 Re: Inheritance FrewCen4-Apr-12 1:07 FrewCen 4-Apr-12 1:07
 Re: Inheritance PJ Arends4-Apr-12 6:19 PJ Arends 4-Apr-12 6:19
 Triange v? rdw2-Apr-12 10:53 rdw 2-Apr-12 10:53
 Re: Triange v? FrewCen3-Apr-12 4:28 FrewCen 3-Apr-12 4:28
 Re: Triange v? rdw17-Apr-12 1:52 rdw 17-Apr-12 1:52
 Last Visit: 31-Dec-99 18:00     Last Update: 15-Aug-24 1:47 Refresh 1