C++ Hello World using OOP


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

class hello
{
	unsigned int limit;
	exec();
	exec(int i);
	string greet;
public:
	hello();
	operator=(unsigned int i);
	run();
	run(int i);
};

hello::exec()
{
	cout << greet << endl;
}

hello::exec(int i)
{
	cout << "[" << setfill('0') << setw(4) << i << "] ";
	exec();
}

hello::hello()
{
	limit=10;
	greet = "Hello World.";
}

hello::operator=(unsigned int i)
{
	if(i>0) limit=i;
}

hello::run()
{
	run(1);
}

hello::run(int i)
{
	if(i<=limit)
	{
		exec(i);
		i++;
		run(i);
	}
}

int main(int argc, char *argv[])
{
	hello world;
	if(argc>1)
	{
		stringstream ss(argv[1]);
		int i;
		ss >> i;
		world = i;
	}
	world.run();
	return 0;
}

This Hello World program, illustrates a number of object oriented programming features of C++; including use of classes, method and operator overloading and recursive functions.