Thread: Object oriented approach in C.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Question Object oriented approach in C.

    Hi all,

    I'm working on object-oriented "approach" in C (using gcc v 3.4.2). I'm using function pointers in structures and static functions. Now I want to experiment on polymorphism. Can anybody plz tell me how I can achieve that? If anybody can throw light upon some other aspect he/she is welcome.

    I know that C and C++ compilers are different. Plz don't post answers like "it can never be achieved". I don't need one-to-one mapping.

    Thanks n regards,

    Arun

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Gotta marvel at the magic of google sometimes....
    http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here's my ten-minute attempt:
    Code:
    #include <stdio.h>
    
    /* Declare the shape object. */
    struct shape;
    
    struct ShapeVtbl
    {
    	void (*Draw)(struct shape*);
    	void (*Eat)(struct shape*);
    };
    
    struct shape
    {
    	const struct ShapeVtbl* vtbl; /* Points to the functions for this object,
    	                                 which may be a circle or square. */
    };
    
    /* Functions which implement a circle object. */
    void CircleDraw(struct shape* circle)
    {
    	printf("Drawing a circle.\n");
    }
    
    void CircleEat(struct shape* circle)
    {
    	printf("Mmm, circles! Doh!\n");
    }
    
    CircleCreate(struct shape* circle)
    {
    	static const struct ShapeVtbl CircleVtbl = { CircleDraw, CircleEat };
    	circle->vtbl = &CircleVtbl;
    }
    
    /* Functions which implement a square object. */
    void SquareDraw(struct shape* square)
    {
    	printf("Drawing a square.\n");
    }
    
    void SquareEat(struct shape* square)
    {
    	printf("This is going to be painful tomorrow!\n");
    }
    
    SquareCreate(struct shape* square)
    {
    	static const struct ShapeVtbl SquareVtbl = { SquareDraw, SquareEat };
    	square->vtbl = &SquareVtbl;
    }
    
    
    int main(void)
    {
    	int i;
    
    	/* Declare objects. */
    	struct shape Shapes[2];
    
    	/* Create objects. */
    	CircleCreate(&Shapes[0]);
    	SquareCreate(&Shapes[1]);
    	
    	for (i = 0; i < 2; i++)
    	{
    		Shapes[i].vtbl->Eat(&Shapes[i]);
    		Shapes[i].vtbl->Draw(&Shapes[i]);
    	}
    
    	getchar();
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    Thanks to both of u. Both the replys were of much help to me.

    And sorry for posting the same query in two boards. I'm new here and was unaware of the regulations of the forum.

    Regards,

    Arun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good way to prepare for an object oriented job test
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-12-2008, 11:58 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. What's a good Object Oriented Strategy for OpenGL/Graphics
    By indigo0086 in forum Game Programming
    Replies: 9
    Last Post: 04-03-2007, 06:27 PM
  4. Object Oriented Presentaion help
    By vanella*Flavor in forum C++ Programming
    Replies: 9
    Last Post: 11-16-2005, 07:55 PM
  5. Object Oriented - Funny story
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-26-2002, 02:21 PM