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;
}

C++ Templates

2 Comments

Hey everyone!

Today I learned a really great special functions that I did not know about before – templates.

Basically, all it does lets you to define other functions with arguments, but those arguments can be of any different type. Thus, we do not have to overload functions just because they have to accept different data types. I really like this because I don’t really like to overload functions too much. It’s might not be easy to understand what I mean without an example so here it is:

for example we have function:

void findLargerNumber (int a, int b)
{
if (a == b)
cout << a << ” = ” << b << endl;
else
{
int i = a > b;
if (i)
cout << a << endl;
else
cout << b << endl;
}
}

if our arguments are doubles (or any other variable type) and we will need to perform same calculations we will need to overload findLargerNumber function with arguments for double… and for float and for long long… That could be a real pain!… It is inefficient and takes a lot of time to do… unless we use templates which will allow us to code function only once.

Here is an example:

template <class AnyType>
void findLargerNumber (const AnyType &a, const AnyType & b)
{
if (a == b)
cout << a << ” = ” << b << endl;
else
{
int i = a > b;
if (i)
cout << a << endl;
else
cout << b << endl;
}
}

all we do is: before our function findLargerNumber we add

template <class AnyType>

which called template prefix

and we change argument types to AnyType which will be out type parameter. And that is all.

Here is the program with a little main:

#include <iostream>
using namespace std;

template <class AnyType>

void findLargerNumber (const AnyType &a, const AnyType & b)
{
if (a == b)
cout << a << ” = ” << b << endl;
else
{
int i = a > b;
if (i)
cout << a << endl;
else
cout << b << endl;
}
}

int main()
{
findLargerNumber(3, 8);
findLargerNumber(5, 2);
findLargerNumber(5, 5);
findLargerNumber(3.5, 8.2);
findLargerNumber(13.0, 8.6);
findLargerNumber(‘A’, ‘a’);
return 0;
}

Isn’t cool? And notice that we do not have to write any extra code in main to call template function. So, in summary, it saves a lot of time. It can also be used with classes, but that one should be explained by Fardad in class some time later πŸ˜‰

IRC help

2 Comments

Here is very helpful video on how to download and use mIRC.


It also explains in detail how to register nickname and identify it next time you log-in.

Helped me a lot!