Thursday 29 September 2011

Evolution of Classes Architecture


OOP in C (not C++).
#include <stdio.h>
struct Car
{
   int n; 
   void display() 
   {
      printf("The Car no. is %d .", n);
   } 
};
main()
{
   Car c;
   printf("Enter Car no.: ");
   scanf("%d", &c.n); 
   c.display();
   getchar();
   getchar(); 
}
Although it does not have access specifier, constructor, etc yet Struct instances are objects as the have State(i.e. the value of 'x'), Behavior(i.e. the 'display()' method) and Identity(i.e. the Car struct 'c'). 
But most importantly the experimental code reveals to me, that the idea of Classes in OOPs must have evolved from 'Structs'. Just like Man evolved from Monkeys and Lizards evolved from Dinosaurs, in the same way Classes in OOPs evolved from Structs. 
'Structs' are ancestors of 'Classes'.



Of course, the following concepts are not possible in C:
1) Encapsulation: Hiding of data is not possible because of no access specifier.
So, Encapsulation is gone.
2) Abstraction: Without Encapsulation, Abstraction is not possible.
So, Abstraction is gone.
The only things left with is Inheritance and Polymorphism. They are somewhat or rather very much there in the procedural languages like C because in C struct can be inherited and polymorphism of methods(Static Polymorphism) is also possible.


Conclusion: 'C' was already 50% Object - Oriented.