Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struggling with C coming from Object Oriented land? [closed]

When I am presented with programming problems, I naturally start breaking them up into logical objects in my head. Who has what responsibility, who owns what, who derives from what, etc.

I am struggling with C. I just don't get how to do things in a Procedural Language.

Can an experienced C programmer help explain how I should think about my programs during design time?

For example, I want to write my own Semaphore class. I would naturally need a Queue data structure for my program, which I would like to write myself as well. If I needed to do this in Java or C#, I could simply whip up a quick Queue class and create a new instance of it in my Semaphore class.

But in C, there aren't objects. So do I have to inline all the behavior of my Queue data structure?

Can someone help me "get it"?

Related: what is the best way to plan and organize development of an application in c

like image 718
KingNestor Avatar asked Mar 23 '09 18:03

KingNestor


People also ask

Why is OOP so hard for me?

As a beginner, OOP is also more difficult to read for several non-code related reasons. First, it's near impossible to understand why a piece of code exists if you're unfamiliar with the domain being modeled with classes. Secondly, OOP is a craft and is inherently opinionated.

Is object-oriented programming dying?

Object-oriented programming has fulfilled many of its promises. Software systems today are longer-lived and more amenable to change and extension than ever. Nevertheless we observe that object orientation is slowly dying, with the introduction of ever more complex and heterogeneous systems.

What is the problem with OOP?

Nondeterminism inherent in OOP programs makes the code unreliable.” As the program executes, its flow can take many, many different paths — thanks to all of those different objects, with new objects sometimes even created on-the-fly.

Is Oops concept difficult?

Students find it very difficult to understand object oriented concepts like classes, constructor invocation, overloaded constructors, friend functions and other object oriented concepts [2]. Students who have been exposed to procedural programming find it a little difficult to move towards object oriented programming.


1 Answers

But in C, there aren't objects. So do I have to inline all the behavior of my Queue data structure?

No.

Do this.

  1. Define your class however you feel comfortable doing OO design.

  2. Write the attributes of your class as a C-language struct.

  3. Put that struct in a header file, along with all of the functions that operate on that struct. Make sure a MyStruct * self is the first argument to all of these "method functions".

  4. Write a C module with all of the bodies of the method functions.

Poor-person's OO in C. It works well. Just be disciplined about putting everything into the struct that you need -- public and private instance variables -- everything.

Generally, avoid trying to have private variables in the first place. You don't have the full power of an OO compiler, so don't bother with low-value features like "private" or "protected".

like image 78
S.Lott Avatar answered Oct 19 '22 00:10

S.Lott