OOP Keywords

Some keywords related to Object Oriented Programming are explained here.

public vs protected vs private

  • public: Any class can refer
  • protected: Can be inherited
  • private: Cannot be inherited

The default is private!

internal

If internal is defined in a dll; any exe which uses the dll can’t access it.

Example:

protected internal

static

  • Static methods can be used without creating an object.
  • Static methods must be called using class name, not using object name!

const vs readonly

  • For the readonly keyword, the latest value is known by the runtime.
  • For the const keyword, the value must be known by compile time.

overload

  • Same name, different parameters.
  • Overloading is an example of polymorphism.

ref vs in vs out

  • ref is used to state that the parameter passed may be modified by the method.
  • in is used to state that the parameter passed cannot be modified by the method.
  • out is used to state that the parameter passed must be modified by the method.

ref must have default value, out doesn’t need to have default value!

get vs set

In order to access a private value;

  • Define a public method
  • Define get and set
class Person
{
  private string name; // field
  public string Name   // property
  {
    get { return name; }
    set { name = value; }
  }
}

class Program
{
  static void Main(string[] args)
  {
    Person myObj = new Person();
    myObj.Name = "Liam";
    Console.WriteLine(myObj.Name);
  }
}

this

The this keyword refers to the current object (instance) of the class and is also used as a modifier of the first parameter of an extension method.

Creator Method

  • The creator method must be public.
  • The creator method doesn’t return value.

Destructor Method

  • ~ is used

C++ example:

class Test
{
    public:
        Test()
        {
            count++;
            cout<<"\n No. of Object created:\t"<<count;
        }
         
        ~Test()
        {
            cout<<"\n No. of Object destroyed:\t"<<count;
            --count;
        }
};

Inheritance related keywords

  • If you define a virtual method and inherit from it, then you can change it using override.
  • An abstract class can’t create objects.
  • A sealed modifier prevents other classes from inheriting from it.
  • In Java all of methods are virtual by default. And methods in derived classes override methods from base. In C# they are not.

base

The base keyword is used to access members of the base class from within a derived class.

Delphi: Screen Scraping

function GetFocusedText(Wnd: HWND): string;
var
  str: string;
  Len: Integer;
begin
  str := '';
  if Wnd <> 0 then
  begin
    Len := SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0);
    if Len > 0 then
    begin
      SetLength(str, Len);
      Len := SendMessage(Wnd, WM_GETTEXT, Len+1, LPARAM(PChar(str)));
      SetLength(str, Len);
    end;
  end;
  Result := str;
end;

Very good screen scraping article for C++

Related Software: Screen Reader

Quake III Fast Inverse Square Root

John Carmack’s Quake 3 source code in C:

float Q_rsqrt( float number )
{
	long i;
	float x2, y;
	const float threehalfs = 1.5F;
 
	x2 = number * 0.5F;
	y  = number;
	i  = * ( long * ) &y;                       // evil floating point bit level hacking
	i  = 0x5f3759df - ( i >> 1 );               // what the f**k? 
	y  = * ( float * ) &i;
	y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//	y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
 
	return y;
}

Toon Krijthe’s Delphi (Object-Pascal) version:

function rsqrt(const ANumber: Single): Single;
var
  ResultAsInt: UInt32 absolute Result;
begin
  Result := ANumber;
  ResultAsInt := $5F3759DF - (ResultAsInt shr 1);
  Result := Result * ( 1.5 - (ANumber * 0.5 * Result * Result));     // 1st iteration
//  Result := Result * ( 1.5 - (ANumber * 0.5 * Result * Result));   // 2nd iteration, this can be removed
end;

Why C# coders should shut up about Delphi

Jon L. Aasenden

EDIT: Some 3 years after this satire post was published, a website called “.NetRocks” decided to make a number out of it. The satire clearly went over their heads, and the facts I outline in the post was met with ridicule. I’m a bit disappointed with .NetRocks, because this could have been a great opportunity for them to learn more about modern object-pascal development and it’s ecosystems. They seem genuinely baffled that Delphi and Object-Pascal in general was being used in 2016 (and 2019), and their behavior and response demonstrates the very arrogance and ignorance we have come to expect from “brogrammers” of their type.

I must admit that I’m somewhat shocked that a massive show like .NetRocks is utterly ignorant of the fact that Object-Pascal as a language has millions of users around the world. You have compilers like Oxygene from RemObjects that targets .Net, Java, x86, ARM and WebAssembly…

View original post 4,832 more words

Delphi vs Alternatives

Broad comparison of programming environments (IDE’s) with internal GUI support:

  • Delphi / C++ Builder Community Edition (WINNER)
    • Free (for yearly income under $5000)
    • Small executable file
    • Very fast execution
    • Very fast compiling
    • No external software requirement
    • Good Cross-Platform support
  • Visual Studio
    • Free
    • Small executable file
    • Very fast execution
    • Fast compiling
    • External software requirement (.Net Framework)
    • Very Bad Cross-Platform support (Xamarin sucks)
  • Qt
    • Only free for open source projects
    • Very large executable file
    • Fast execution
    • Slow compiling
    • No external software requirement
    • Very good Cross-Platform support
  • Java
    • Free (with limitations)
    • Small executable file
    • Slow execution
    • Slow compiling
    • External software requirement (Java Virtual Machine)
    • Good Cross-Platform support

How to Install GLUT to Dev C++

GLUT is a platform-independent OpenGL library. We need to follow the steps below in order to use it on Dev C++

  • glut.h – Copy to C:Dev-CppincludeGL
  • glut32.def – Copy to C:Dev-Cpplib
  • glut32.dll – Copy to C:WINDOWSsystem32

 

Let’s try it:

#include <glgl.h>
#include <glglut.h>
void init(void);
void display(void);

int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,200);
glutCreateWindow("My OpenGL Application");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

void init(void)
{
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)250/(GLfloat)250,0.1f,100.0f);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, -10.0f);
glVertex3f(-1.0f,-1.0f, -10.0f);
glVertex3f( 1.0f,-1.0f, -10.0f);
glEnd();
glutSwapBuffers();
}