Object Oriented Programming in C : An overview

oopsinterviewquestionsObject Oriented Programming (OOP) is a programming concept where the program in question is made up of objects and properties of those objects.

Consider an example about a medical facility where both the doctors and patients are objects. These objects have distinct characteristics and the job of one object cannot be performed by another, unless they are related. In addition, these objects interact with each other just like doctors and patients interact with one another and there is generally an outcome of this interaction.

Before we can elaborate on the concept of OOP in C programming, it’s necessary to understand the concept of objects first. You can then relate objects to structures in C and learn how to use structures in C programming.

Learn more about Object Oriented Programming by taking an online class at Udemy.com

Objects

An object is any real world entity that is represented in a programming language as a structure. Although this description of an object is a little complicated, it will go a long way in understanding what you can achieve by using objects in a programming language.

Let’s assume that you’re building software to keep a track of all the employees in a company with related details such as Name, Address, Phone Number and Employee ID. In this scenario, it would be extremely inconvenient to write a piece of code every time an employee has to be added to the system to explicitly declare attributes for each employee. It would be much more convenient if you had some kind of an employee details template that could be used over and over again whenever you created an employee entity and added it to the system.

You can call this employee an object and you can also create as many objects as you require for the system. Another important thing to note is that all the properties related to a particular object remain inside the object once it is created and the object encapsulates its properties. You can access the properties at free will and even change them, but they are distinguished and specific to the object so that you don’t confuse one object with another.

In the “C” language, you should know that C is not an object-oriented language. C was not designed with the concept of objects in mind, but that should not discourage you from reading further and understanding how to implement the concept of objects in C as well.

Structures

A structure is a tool that can be used to implement the concept of objects in C programming. This would require you to know a little about C programming and may even require you to know how to compile a C program and see the output on the console.

Syntax of a Structure

typedef struct employee

{

char name[50];

char title[50];

char address[100];

intemp_id;

};

As you can see, there are four variables inside the structure called “employee” and it is declared using the keyword “struct.”

This struct definition serves as a template (generally called a class in the object-oriented programming language) for creating other structure variables that contain the actual values you want to assign. C structures give you this flexibility and a lot more, but more on that later.

struct employee Emp1;

The line above simply declares a structures type variable (Emp1) that is similar to objects in OOP language. It really does nothing more than tell the compiler to keep some memory handy for the values that will be assigned to the variables later.

Emp1.name=”Harold”;

Emp1.title=”CEO”;

We just assigned our first values to the variables’ name and title, but notice the syntax and use of the dot(.) operator to denote which object the variables belong to. If you had created another object called Emp2, then you could have assigned the values as shown below:

Emp2.name=”Robbie”;

Emp1.name=”Harold”;

Notice how the dot operator relieves the compiler from the confusion of assigning different values to different objects. It can easily distinguish that the name Robbie is being assigned to the second object and Harold to the first.

Usage

Now that you have a brief overview of objects, it’s time to understand usage. Objects are useless if C does not provide a method to use these objects and associated values in programming. Thankfully, it does and it’s not only a container for variables and values but it’s also a very versatile language that lets you access those values.

printf(Emp1.name);

Let’s examine the above code. This is all you need to know if you want to print the name of the first employee on the screen. The syntax is the same as the one used during assigning the values to the objects. You can similarly use the values of the variables in different ways as needed:

if(Emp1.emp_id>1 && Emp1.emp_id<10)

{

printf(“Employee is a Director!”);

}

The code above checks whether the employee in question is a director or not. This is done by analyzing the value of the “emp_id” property, and you will notice again that it uses the same syntax as before.

Learn Object Oriented Programming from scratch by taking an online class at Udemy.com

Structures=Classes

For those of you who are more comfortable with the usage of classes, you should know that C does not support classes, but it makes up for it with structures. Structures are equivalent to classes but then again, they are vastly different.

The important difference would be the inclusion of functions in classes, but that is not possible in structures. It’s wrong to say that you cannot include functions completely in structures since you can declare them inside structures. However, the concept is quite useless as you cannot define them inside a structure.

On the contrary, classes allow you to have functions and even class definitions within them. This is extremely convenient for programmers. Most programmers prefer to use a completely object-oriented language like Java or C# rather than use a structured language like C or Basic.

Having said that, structured languages are easy to learn and easier to master than other languages and they are closer to the low level implementation of the operating system. By this, we mean that functions like memory management are more flexible and bridge the gap between low-level and high-level languages.

New to Object Oriented Programming? Take up a course at Udemy.com

Conclusion

C, a simple structured programming language, lacks the articulacy of highly sophisticated OOP; however, C is very simple to learn and is very popular even today.   In fact, Kerningham and Ritchie made sure that C would always be the forefather of all programming languages that were to come years down the line.