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.

2 comments:

  1. What do you mean??? Just because of that structure declaration that makes an object you will consider that as 50%??? How do you measure the percentage of an object-oriented programming language? The most important concepts such as overloading, constructors, method overloading, constructor overloading, encapsulation and polymorphism all those are missing and you will say that is already 50%???? Aside from that you don't even mention inheritance, class is nothing if you can't provide encapsulation, class is nothing if you can't provide method overloading, polyorphism, constructors, constructor overloading. I'm sorry but I beg to disagree that C is 50% OOP, that is not true... wake up!

    ReplyDelete
    Replies
    1. Well, C certainly is not Object Oriented. It can never be.
      But certainly just like Man evolved from Monkeys. The concept of Object Orientation evolved from Structured based concepts.
      That was what I contemplated from the above work.

      Delete