Constructors always public?

Leave a comment

Learning C++ and OO methodology has been fun, the semester is nearly done, and I had learned whole bunch of interesting thing. One of the base thing that we learned this semester when talking about OOP was function permissions, functions can be public, private and protected, and we all know that a class constructor has to be public, right? Otherwise, how are you going to instantiate the class? You want to see an example of private constructor? Enter a Singleton Pattern:
What is a Singleton – a singleton is a way to create your object where instead of instantiating an object, you get an instance of the object that has been previously created. If the instance does not exist, then the instance will be created, but check out following code:

#include
using namespace std;

class Singleton {
private:
bool CanIHazAnInstance;
static Singleton *single;

//private constructor – could this be????
Singleton()
{
}
public:
//this is a weird function, it will be called BEFORE constructor
static Singleton* getInstance();

void MySuperCoolFunction();
~Singleton()
{
CanIHazAnInstance = false;
}
};

bool Singleton::CanIHazAnInstance = false;
Singleton* Singleton::single = NULL;

Singleton* Singleton::getInstance()
{
//this is where magic happens:
if(!CanIHazAnInstance)
{
//if there is no instance as of yet,
//we will call our private constructor
single = new Singleton();
//and set our instance flag to true, so we know
//that an object has been instantiated.
CanIHazAnInstance = true;
return single;
}
return single;
}
}

void Singleton::MySuperCoolFunction()
{
cout << "SuperCoolFunction is Super Cool!!!" <MySuperCoolFunction();

//Over here, the instance has already been created, so this
//time, we won’t have to call constructor
single2 = Singleton::getInstance();
//Oh, look here is that SuperCoolFunction again!!!
single2->MySuperCoolFunction();

return 0;
}

Now, I know, the example is kind of weak, I mean that SuperCoolFunction just outputs some stuff to the screen, right? So you might ask yourself – where would I possibly use an approach like this? Consider following scenario – you are writing an application of some sort. This application has options, like let’s say default fonts, colors and so on. As we can imagine, we will have to keep those options somewhere in the file and to access those we need to open the file, read it, and close it. Even worse if you have to modify those options at some point. Using a handy Singleton Pattern, we have one instance of options for our application loaded and available to all objects that might require it.

Recursion

1 Comment

Recursion

SVN Branching/Merging

Leave a comment

Very useful video tutorial on how to merge, branch and test things in SVN.

If you need to know how to download tortoise svn or how to checkout there is tons if videos also available on youube.
😉

C++ predefined functions

1 Comment

Just a quick reference of some of predefined functions I learned today.

The return value for abs(value) and fabs(value) is an absolute value int for abs and floating point for fabs. Library header is #include <cstdlib>

For example:
unsigned int positiveInt = abs(-6);

positiveInt will return 6

By the way, a few days ago we learned what signed and unsigned variables are. Forgot to ask if declaring variable “unsigned” prevents users to enter negative values or what is the point of declaring it unsigned? At least should gave some kind of warning at compile time. I will do more research on this later.

Another function I learned today is exit(value);

We are not allow to use break and continue in our code from now on, but we have not been told anything about exit.

Exit(1);

will exit program immediately.

To use exit function you must include
#include <cstdlib>
using namespace std;

I don’t know if this is any use at all for anyone, but I thought it would be nice to know, especially absolute value functions.

Internet Explorer 9 Beta

Leave a comment

Just installed Internet Explorer 9 Beta which available for Download now. And I would say that I am quite impressed with it. Microsoft really did something great this time! Very attractive User Interface and it is considerably faster. Also available in variety of different languages, improvements of graphics etc.

Well, for sure it’s worth a try.

A little of programming history

1 Comment

I found a really interesting and useful for the beginner programmers article
The People Behind the Code: Creators of Famous Programming Languages

After all, without the past we do not have a future 😉

I hope you will find it interesting, entertaining and useful as well.

C++ Magic Formula

1 Comment

Have you ever met people looking for magic formulas in C++?
I have, but I did not understand before what were they really looking for.
Today finally I’ve got an idea. For example, if you want to set 2 digits after the decimal point you can use following magic formula in you code:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

and after that all floating point variables will be displayed in the following format:

2.34
5.01
5.00
etc.

example:

#include <iostream>
using namespace std;
int main( )
{
double pi = 3.1415926;

cout << “pi is ” << pi << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << “pi is ” << pi << endl;
cout.precision(7);
cout << “pi is ” << pi << endl;

return 0;
}

Older Entries