Thread: How to emulate object orientation in C, with multiple files and a Makefile. PLEASE!!

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    How to emulate object orientation in C, with multiple files and a Makefile. PLEASE!!

    I'm very confused on this topic of how to structure my classes so that they all "wire together" and inherit the proper functions and data that I want them to.
    I want to have a Car.c and car.h which are "wired" with body.c/body.h, which in turn is wired with a frame.c/frame.h, wheel.c/wheel.h, and driver.c/driver.h.
    Each part of the car holds some type of data, mostly ints:
    The car has a (total) mass and car_name. The body has a mass and color. The frame has a mass. The wheel(s) have a mass and force. [There will be 4 instances of wheel] The driver has a mass and driver_name.
    Each of these functionalities must come from their respective .c and .h files, and be amalgamated in the car.c and car.h (which should contain all the functionality of its parts).
    Then, in my main test program, I am to make an instance of car and hardcode in its values of: mass (which comes from the total mass of all of its parts, this is where i start to lose it. How will I access its parts' masses in the test program?), color (a character array), current position (an integer), current velocity (an integer), and current acceleration (which comes from the total newton force of the 4 wheels, again this is where I start to get very confused).
    (Then in the test program I am to print out a simulation of this car over a period of 100 seconds, and show its position, velocity, and acceleration at each point (based on the mass, total newton force, and starting position). This part is of course a simple while loop, and is somewhat trivial.)
    I am somewhat confused/lost all around, and I am making a list of questions to ask my teacher today after class, and I have been posting online (to no avail, yet) for help on this program. I have a strong desire to learn this stuff, but it does not come easily for me.
    From what I understand, I will need to allocate memory for all of the data each class holds. Then, using function pointers, as well as #include "X.h" , I will need to somehow link up all of the classes together so that car has access to all of the parts' functions which set, for example, the newton force of each wheel. To be honest, this is about the extent of my knowledge. I have no idea how to actually implement these ideas, let alone the syntax to do so.
    We have learned a slight bit about structs, malloc, sizeof, and pointers, but I have not used them to link functionality or data from separate classes together ever before in my life. (I feel that my teacher is competent, but he took a big difficulty jump on this lab assignment. We really have not even discussed function pointers yet in class for more than 5 minutes. I am both frustrated but highly intrigued to find out how this stuff works. I know object orientation in C is the basis for most modern programming.)
    LUCKILY, my teacher did give us an example of a simple program structure based on 2 .c and .h files. It has a person.c/person.h and employee.c/employee.h, and, of course, employee inherits some of the data and functionality of person (like the name). I can post some of the code, here, if that helps anyone understand what I'm talking about.

    I will be very greatful if anyone here can give me help on this project. I've been working on it for no less than 12 hours thus far (and I haven't even started debugging, yeesh) , and i KNOW it is not that hard of a lab and does not deserve this much time.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    There is some good information on this topic here

    oop - Object-Orientation in C - Stack Overflow

    It's up to you how to set it up to solve your problem. I would imagine something like the following

    Code:
    BODY *bod = Body_new(72.5, "blue");
    FRAME *fra = Frame_new(12.0);
    WHEEL *wh1 = Wheel_new(5.5);
    WHEEL *wh2 = Wheel_new(5.8);
    WHEEL *wh3 = Wheel_new(5.4);
    WHEEL *wh4 = Wheel_new(5.6);
    DRIVER *dri = Driver_new(16.5, "male");
    
    CAR *car = Car_new(bod, fra, wh1, wh2, wh3, wh4, dri);
    
    printf("The mass of the car is %.2f\n", Car_getmass(car));
    
    Car_delete(car);
    // ...
    Each object can go in its own file and has its own set of functions. This way can makes it straightforward to independently test them. It has a constructor and a destructor and accessor methods. If you need to model inheritance you can use the technique described in the linked thread.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I've been working on it for no less than 12 hours thus far (and I haven't even started debugging, yeesh) , and i KNOW it is not that hard of a lab and does not deserve this much time.
    O_o

    Homework deserves exactly as much time as it takes you to understand what lessons the homework is intended to impart.

    *shrug*

    In any event, the various parts of this are unrelated. Solve the parts in chunks.

    And, we don't do homework, so you'll have to show your work.

    So, what have you accomplished in these twelve hours?

    Soma

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    We are not required to have "true" inheritance like you said. I had an epiphany right before reading your post which looked something like that (in my mind). THANK YOU SO MUCH for confirming what I believed to be true as a good plan for this assignment. I'll make a .c and .h file for each of the different components, which contain pointer constructors which only take in the variables which relate to those components specifically. Then in the main program, I'll use my main Car pointer constructor, which takes in all of those component pointers to get the finished product.

    Am I on track?

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    Quote Originally Posted by phantomotap View Post
    O_o

    Homework deserves exactly as much time as it takes you to understand what lessons the homework is intended to impart.

    *shrug*

    In any event, the various parts of this are unrelated. Solve the parts in chunks.

    And, we don't do homework, so you'll have to show your work.

    So, what have you accomplished in these twelve hours?

    Soma
    Soma, I didn't mean to come off as lazy there. Call me crazy but I actually love being thrown in the deep end like this, and pulling all nighters to understand this stuff (or to learn a song on guitar, build a website, or whatever it may be). Not that this is due soon, or anything. Computer science has always been one of my main hobbies, and I hope i can turn it into a career. I will put in as much time as it takes to do this lab. All I meant was that I think most of the other kids in my class will finish this lab within a few hours; this stuff does NOT come easily for me.

    What have I accomplished in this 12 hours? I have most of car.c and car.h written, and I actually think they are close to being correct. I also actually understand what I am doing now, rather than following my teachers example source codes. I know, probably not as much as you might think, but the other .c and .h files will be very similar to the one I already have written (actually simpler). And I have a solid plan in mind for my main test program, which I think is pretty easy anyway.

    Back to coding!
    Last edited by dnainstant; 02-13-2013 at 05:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Question - Object Orientation
    By Freem in forum C++ Programming
    Replies: 2
    Last Post: 08-22-2011, 09:17 AM
  2. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  3. Setting an object to predefined angle / orientation
    By sapdev in forum Game Programming
    Replies: 16
    Last Post: 05-19-2009, 07:40 AM
  4. Conditional compilation for object files in Makefile?
    By jutirain in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2007, 06:23 AM
  5. how to debug multiple files /w using makefile
    By reakinator in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2007, 11:41 AM