Programming - C++ Structs and classes
A common question from engineers and programming enthusiasts
which always makes me perplexed is:
"Why use C++ when we have better languages already
available?"
And I believe this is caused by their superiority complex in
relation to next generation programming languages such as C#
and Java.
I always explain to people the wonders of middle tier
languages such as C/C++ as they allow you to access lower OS
funcionality aswell as inexpensive but fully featured software
frameworks.
In the POSIX world, we can create bridged kernel and
application space programs which add a degree of real-time
predictability to our software, as well as access kernel
function from within our programs. We can map memory, building
complex pointer maps to all servides running within the kernel.
This is somethign which is just not possible with
Java and C#.
And C++ is an excellent OO programming language, blending
function based programming withandOO which is why I think,
C++ will survive.
A quick look at C++ basic concepts.and
//Include headers for other source files which you will have
to link against
#include"stdafx.h"
#include<stdio.h>
//a struct is created on the heap making it faster
struct _tstring{
char* value;
};
//compile directives
typedef _tstring String;
//the wonderfull C++ class which brings in a whole host of
new functions such as inhertitence, polymorphism as well as
friends and access controls
class StringClass{
public:
StringClass(){}
StringClass(char* str){
this->str = str;
}
char* getValue(){
return this->str;
}
private:
char *str;
};
int_tmain(int argc, _TCHAR* argv[])
{
//struct example
String strings[100];
strings[0].value = "ajmal";
printf(strings[0].value);
//C++ class example
StringClass stringClasses[1] = {StringClass("ajmal")};
printf(stringClasses[0].getValue());
}
You can download the source-code here.