Linkable Object C++ Hello World


// thello.hpp
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class THello
{
	string TGreeting;
	unsigned long TRepeatCount;
public:
	THello();
	THello(string UserGreeting);
	THello(unsigned long Count);
	THello(string UserGreeting, unsigned long Count);
	void AssignGreeting(string UserGreeting);
	void AssignRepeat(unsigned long Count);
	void operator<<(string UserGreeting);
	void run();
};

//thello.cpp
#include "thello.hpp"

void THello::AssignGreeting(string UserGreeting)
{
	TGreeting = UserGreeting;
}

void THello::AssignRepeat(unsigned long Count)
{
	if(Count>0) TRepeatCount = Count;
}

THello::THello()
{
	AssignGreeting("Hello World.");
	AssignRepeat(10);
}

THello::THello(string UserGreeting)
{
	AssignGreeting(UserGreeting);
	AssignRepeat(10);
}

THello::THello(unsigned long Count)
{
	AssignGreeting("Hello World.");
	AssignRepeat(Count);
}

THello::THello(string UserGreeting, unsigned long Count)
{
	AssignGreeting(UserGreeting);
	AssignRepeat(Count);
}

void THello::operator<<(string UserGreeting)
{
	AssignGreeting(UserGreeting);
}

void THello::run()
{
	unsigned int i = 1;
	while(i<=TRepeatCount)
	{
		cout << "[" << setfill('0') << setw(5) << i << "] ";
		cout << TGreeting << endl;
		i++;
	}
}

This creates an object that can be linked to a main program containing all hello world services to a parent program.

#include "hellolib\thello.hpp"
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	switch(argc)
	{
		case 1:
		{	THello World;
			World.run();
			break;
		}
		case 2:
		{
			long i = atol(argv[1]);
			THello World( i );
			World.run();
			break;
		}
		default:
		{
			long i = atol(argv[1]);
			string s = argv[2];
			THello World( s , i );
			World.run();
		}
	}
	return 0;
}

A sample program using that object.