Like Tree1Likes
  • 1 Post By c99tutorial

Very basic OOP programming in C

This is a discussion on Very basic OOP programming in C within the C Programming forums, part of the General Programming Boards category; Hi. I have just recentely learned on function pointers. So now i know how to create a function and assign ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    61

    Very basic OOP programming in C

    Hi. I have just recentely learned on function pointers.
    So now i know how to create a function and assign it a pointer.
    So far so good.

    But can someone give me a simple example on how I can create a struct and have a function that return a value that is inside that struct ?
    And how can I create a constructor function in order to instantiate all the variables inside a struct?

    just to explain myself better, let's say I have a struct person:

    Code:
    struct Person{
        int age;
        int(*getAge)(int);
    };
    typedef struct Person Person;
    And a function that get's an int and returns it:

    Code:
    int returnInt(int x){
       return x;
    }
    now i can create a new Person:

    Code:
    Person p;
    p.age = 30;
    But how can i link the getAge pointer to the returnInt() function, so that it will be returning the age inside the Person ?
    And how can I create some sort of constructor that will automatically initialize all the variables inside a Person struct?

    (I can't do something like this:
    Code:
    struct Person{
       int age = 30;  // THIS WILL NOT COMPILE
    ........
    };
    Thx.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,051
    Quote Originally Posted by patishi View Post
    But how can i link the getAge pointer to the returnInt() function, so that it will be returning the age inside the Person ?
    And how can I create some sort of constructor that will automatically initialize all the variables inside a Person struct?
    You can't make construction automatic, but you can make a constructor function and make sure you call it. If the struct is opaque, then you can also be reasonably sure that the user will have to use your constructor (similar to fopen and fclose operate on FILE *).

    If the object is not-opaque then of course there is no true information hiding, but for most purposes in C this is probably adequate. If not consider C++.

    Person.h example
    Code:
    #pragma once
    #define PERSON_STRMAX (1000)
    
    typedef struct Person Person;
    struct Person;
    
    Person Person_construct();
    Person *Person_print(Person *p);
    Person *Person_set_name(Person *p, const char *name_str);
    
    struct Person {
        int age;
        char name[PERSON_STRMAX];
        Person *(*print)(Person *);
        Person *(*set_name)(Person *, const char *name_str);
    };
    
    static inline Person
    Person_default() {
        return (Person) {
            0,
            "",
            Person_print,
            Person_set_name
        };
    }
    Person.c example
    Code:
    #include "config.h"
    #include "classes/Person.h"
    
    Person
    Person_construct()
    {
        Person p = Person_default();
        return p;
    }
    
    Person *
    Person_print(Person *p)
    {
        printf("Name: %s\n"
               "Age: %d\n", p->name, p->age);
        return p;
    }
    
    Person *
    Person_set_name(Person *p, const char *name_str)
    {
        strncpy(p->name, name_str, PERSON_STRMAX);
        return p;
    }
    If you want to use it notice you have to pass a pointer to the object for each method call. Also, assigning function pointers is not strictly required. You could just use Person_set_name or Person_print and so on. In fact many C libraries like SDL and APR use this kind of convention.

    Code:
    Person p = Person_construct();
    p.set_name(&p, "Bob");
    p.age = 42;
    p.print(&p);

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,166
    You can't.

    To call a member function, it is necessary to have two items of information: the action to be performed (i.e. the body of the function) and a reference (or pointer) to the object being acted on. The two bits of information have to be available to the function body.

    C++ and other OO languages achieve what you have in mind by passing additional information (the "this" pointer, "self" reference, or something similar - depending on language) when calling the function. In a compiled language, the compiler takes care of implicitly passing that information with every member function call, so the programmer doesn't have to. In an interpreted language, the interpreter takes care of that. Etc.

    The thing is, in C, there is no implicit mechanism (i.e. one the that compiler does for you) for such things. If you want to achieve a similar effect, you have to fall back on explicitly passing that information. In other words, the *programmer* explicitly passes the required information every time a member function is called.

    You can't have your cake and eat it to. In OO languages, it happens implicitly because the compiler (or interpreter, or whatever) takes care of the machinery. You can't expect it to happen by magic when using a language which doesn't require the compiler to support such machinery.
    Last edited by grumpy; 12-20-2013 at 02:10 PM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C programming 3
    By vivekgupta in forum C Programming
    Replies: 10
    Last Post: 06-13-2012, 11:22 AM
  2. Basic C programming -1
    By vivekgupta in forum C Programming
    Replies: 29
    Last Post: 06-11-2012, 03:26 PM
  3. Basic c programming help
    By benrogers in forum C Programming
    Replies: 12
    Last Post: 01-24-2011, 04:01 PM
  4. Very basic programming help please
    By xrated in forum C++ Programming
    Replies: 8
    Last Post: 08-14-2006, 03:02 PM
  5. Basic Programming
    By plexy1 in forum C Programming
    Replies: 2
    Last Post: 09-05-2001, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21