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.

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.

C++ Prime Numbers

#include <stdio.h>
#include <time.h>

using namespace std;

bool isPrime(long n)
{
	long i, m;
	m = n / 2;
	if(n % 2==0)
		return false;
	else for(i=3; i<m; i+=2)
		if(n % i==0)
			return false;
	return true;
}

int main()
{
	long j;
	clock_t tStart = clock();
	for(j=1;j<1000000;j++)
		if(isPrime(j))
			printf("%ld\n", j);
	printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
	return 0;
}

 

Fractal Tree C++ Logo Output

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>

usingnamespace std;

class ifstree
{
public:
    float minfactor;
    float maxfactor;
    float bend;
    float angle;
    float initlen;
    int fdim;
    ifstree();
    void turnleft(float ang)
    {
        printf("lt %4.2f\n", ang);
    };
    void turnright(float ang)
    {
        printf("rt %4.2f\n", ang);
    };
    void movefw(float value)
    {
        printf("fw %4.2f\n", value);
    };
    void movebw(float value)
    {
        printf("bw %4.2f\n", value);
    };
    float randfactor();
    void draw();
    void draw(float linelen,int depth);
};

ifstree::ifstree()
{
    minfactor =(float)1/3;
    maxfactor =(float)2/3;
    bend =8.0;
    angle =40.0;
    fdim =8;
    srand( time(NULL));
    initlen =100.0;
}

void ifstree::draw()
{
    draw(initlen, fdim);
}

float ifstree::randfactor()
{
    float rnd =(float) rand()/RAND_MAX;
    return(float) rnd;
}

void ifstree::draw(float linelen,int depth)
{
    if(depth>0)
    {
       float minlength = linelen * minfactor;
       float maxlength = linelen * maxfactor;
       float difflength = maxlength - minlength;
       movefw(linelen);
       turnleft(angle+bend);
       draw(minlength+(randfactor()*difflength), depth-1);
       turnright(angle);
       draw(minlength+(randfactor()*difflength), depth-1);
       turnright(angle);
       draw(minlength+(randfactor()*difflength), depth-1);
       turnleft(angle-bend);
       movebw(linelen);
    }
}

int main(int argc,char*argv[])
{
    ifstree T;
    if(argc>1)
    {
        string svalue(argv[1]);
        float fv =(float)atof(svalue.c_str());
        T.initlen = fv;
    }
    T.draw();
    return0;
}

 

ROT13 C++

The following program (written in C++) creates a filter program for converting ascii into ROT13 encoding. A very simple cypher where each character in the alphabet is replaced with the 13th letter from its position. To download a compiled windows compatible binary, click here.

#include <cstring>
#include <cstdio>
#include <cctype>

using namespace std;

char translate(char c)
{
    int cflag = 0;
    char oc = c;
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    int alphalen = strlen(alphabet);
    if(isalpha(c)) {
        if(isupper(c)) {
            cflag = 1;
            c = tolower(c);
        }
        for(int i=0;i<alphalen; ++i) {
            if(c==alphabet[i]) {
                oc = alphabet[(i+13)%alphalen];
            }
        }
        if(cflag==1) {
            oc = toupper(oc);
        }
    }
    return oc;
}

int main() {
    char c;
    while((c=(char)getchar())!=EOF) {
        putchar(translate(c));
    }
    return 0;
}

Hello World C++ with Objects

Ok… this is a “Hello World” program in C++ using objects. It illustrates a number of aspects of object oriented programming in C++ allowing you to get a quickstart into C++ programming. First there is the #include directives which tells C++ that the program uses two libraries, iostream and string (actually they specify the header files that define the libraries to be used). “iostream” provides input/output to streams, in this case we are sending a stream to the console. “string” is a string type library that provide a string datatype that is not native to the base C++, this allows us to manipulate strings in an easy manner.

#include <iostream.h>
#include <string.h>
using namespace std;

class oHello
{
    string hstr;
public:
    oHello();
    ~oHello();
    void print();
};

oHello::oHello()
{
    hstr = "Hello World\n";
}

oHello::~oHello()
{
}

void oHello::print()
{
    cout << hstr;
}

>int main()
{
    oHello myObject;
    myObject.print();
    return 0;
}

Class oHello is defined as having three methods, oHello method is the constructor that is executed when the object is created, ~oHello is the destructor that is executed when the program destroys the object (in this case, when the program terminates), lastly there is print which sends hstr to the output stream. These are declared as public so that they can be accessed from other modules in the program that are not members of the oHello object class. “hstr” is private to the class and only the class methods can access this data. It is of type string, the object string that was defined in the string class library.

The oHello method does one thing, assign hstr the value of “Hello World\n”. “\n” means add a carriage return to the end of the string. Now, when any member methods access hstr it would contain the value “Hello World\n”.

The print method contains the instruction cout << hstr, this calls the hstr object using the << method which directs it to send the value it contains to the cout stream class, which as stated earlier sends the data to the output stream.

Now that all the objects are defined… we can use them in the main() function which is automatically executed when the program is run. First we create an instance of the oHello object, called myObject. Next we call the print method of the myObject object, which prints “Hello World\n” to the output stream (in this case the console). Lastly since main() is a function it must return something before terminating, so we return the value zero.