C and C++ Programming Resources

Custom Search

Constructors in C++

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.

When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.

A constructor is declared without a return value, that also excludes void.
Therefore, when implemented, do not return a value:

Constructor Example

class rectangle {              // A simple class
   int height;
   int width;
public:
   rectangle(void);            // with a constuctor,
   ~rectangle(void);           // and a destructor
};

rectangle::rectangle(void)     // constuctor
{
   height = 6;
   width = 6;
}

Default Constructor

A constructor is declared without a return value, that also excludes void. Therefore, when implemented, do not return a value:

//---
#include
using namespace std;
//---

struct TBook
{
public:
    TBook();    // Constructor
};
//---
TBook::TBook()
{
    cout << "I see a book...\n";
}
//---
int main()
{
    TBook B;

    return 0;
}
//---

This would produce:

I see a book…

This book constructor is a programmer created constructor and is empty. You might find it sometimes convenient to create your own constructor because, whether you create an empty constructor or not, this does not negatively impact your program but makes it more lively and allows other parts of the program to conveniently call the object using its constructor. A constructor is easily implemented once you have created one:

There are various categories of bricks used in the construction industry.

For this exercise, we will consider a simple one used in constructing domestic building foundations. It has a total length, here called Length; it also has a height, referred here to as Height; and it has a thickness that will be called Thickness. For resistance and static reasons, our brick will have two holes. Since we are more interested in the amount of cement used to create the brick, we will subtract the volume of the hole from the total volume. The dimensions we use are for simplicity. We will consider that the small wall of the brick has a thickness of 0.25; also, the static wall in the middle length has a thickness of 0.25.

Listing – Brick Unit – Header File: Bricks.h

//---
#ifndef BricksH
#define BricksH

//---
struct TBrick
{
public:
     TBrick(); // Empty Constructor
    void  setDimensions(double l, double h, double t);
    void  Initializer(double l, double h, double w);
    double  CementVolume();
    void  ShowProperties();
private:
    double Length;
    double Height;
    double Thickness;
};
//---
#endif

Brick Unit – Source File: Bricks.cpp

//---
#include
#include
using namespace std;

#include "Bricks.h"

//---

//---
 TBrick::TBrick()
{
    //TODO: Add your source code here
}
//---
void  TBrick::setDimensions(double l, double h, double t)
{
    Length = l;
    Height = h;
    Thickness = t;
}
//---
double  TBrick::CementVolume()
{
    double Enclosure = 0.50; // This includes both walls of the brick itself
    double HoleLength = Length - 0.75; // Including both holes
    double HoleThickness = Thickness - Enclosure;
    double HoleVolume = HoleLength * HoleThickness * Height;
    double TotalVolume = Length * Height * Thickness;
    double ValidVolume = TotalVolume - HoleVolume;

    return ValidVolume;
}
//---
void  TBrick::ShowProperties()
{
    cout << "Foundation Brick Properties";
    cout << setiosflags(ios::fixed) << setprecision(2);
    cout << "\nLength    = " << Length; cout
         << "\nHeight    = " << Height;
    cout << "\nThickness = " << Thickness;
    cout << "\nCement Volume = " << CementVolume() << "\n";
}//---

Main File: Main.cpp

//---
#include
using namespace std;
#include "Bricks.h"
//---

//---
int main()
{
    TBrick Foundation;

    Foundation.setDimensions(4.24, 3.55, 3.45);
    Foundation.ShowProperties();

    return 0;
}
//---

This program would produce:

Foundation Brick Properties
Length    = 4.24
Height    = 3.55
Thickness = 3.45
Cement Volume = 15.38

Press any key to continue...

Pages: [Page - 1] [Page - 2] [Page - 3]

Tags: , , , ,

There are 19 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.


Leave a Reply

You must be logged in to post a comment.