How To Code Inheritance and Polymorphism in C

C++ Code
--------------
#include <iostream>
#include <cstdlib>
using namespace std;
class base
{
public:
    base();
    virtual ~base();
    virtual void draw();
    void initialize();
private:
    int value;
};
base::base()
{
    cout << "base\n";
}
base::~base(){
    cout << "~base\n";
}
void base::draw(){
    cout << "base draw\n";
}
void base::initialize()
{
    cout << "initialize\n";
}
class child : public base
{
public:
    child();
    ~child();
    virtual void draw();
private:
    int number;
};
child::child(){
    cout << "child\n";
}
child::~child()
{
    cout << "~child\n";
}
void child::draw()
{
    cout << " child draw\n";
}

int main(int argc, char** argv) {
    cout << "non dynamic\n";
    child c;
    c.initialize();
    c.draw();
    base* b = &c;
    b->initialize();
    b->draw();
   
    cout << "dynamic\n";
    child* p = new child();
    p->initialize();
    p->draw();
    base* pb = (base*)p;
    pb->initialize();
    pb->draw();
   

   
    return 0;
}

Result Run:

non dynamic
base
child
initialize
 child draw
initialize
 child draw
dynamic
base
child
initialize
 child draw
initialize
 child draw
~child
~base

-----------------------------------------

C Code
----------
 #include <stdio.h>
#include <stdlib.h>
typedef struct base
{
    void (*base_)(struct base* this_ptr); //constructor function pointer
    void (*_base) (struct base* this_ptr); //destructor function pointer
    void (*initialize)(struct base* this_ptr);
    void (*draw)(struct base* this_ptr);
   
    int value;
}base;
void base_(base* this_ptr)
{
    printf("base_\n");
}
void _base(base* this_ptr)
{
    printf("_base\n");
}
void base_initialize(base* this_ptr)
{
    printf("base_initialize\n");
}
void base_draw(base* this_ptr)
{
    printf("base_draw\n");
}
void base_init(base* b)
{
   b->value=0;
   b->base_ = base_;
   b->_base = _base;
   b->initialize = base_initialize;
   b->draw = base_draw;
}
typedef struct child
{
  base parent;
 
  int number;
  void (*child_)(struct child* this_ptr);
  void (*_child)(struct child* this_ptr);
  void (*initialize)(struct child* this_ptr);
  void (*draw)(struct child* this_ptr);
}child;
void child_(child* this_ptr)
{
    this_ptr->parent.base_((base*)this_ptr);
   
    printf("child_\n");
}
void _child(child* this_ptr)
{
    printf("_child\n");
    this_ptr->parent._base((base*)this_ptr);
}
void child_draw(child* this_ptr)
{
    printf("child_draw\n");
}
void child_init(child* c)
{
    base_init((base*)c); //initialize the base first
   
    c->child_ = child_;
    c->_child = _child;
    c->draw = child_draw;
    c->initialize = (void (*)(child*))base_initialize; //since we reuse base
    ((base*)c)->draw = (void (*)(base*))child_draw;//since overriden from base
}

int main(int argc, char** argv) {
    printf("non dynamic\n");
    child c; child_init(&c); c.child_(&c);
    c.initialize(&c);
    c.draw(&c);
    base* b = (base*)&c;
    b->initialize(b);
    b->draw(b);
    c._child(&c);
   
    printf("dynamic\n");
    child* p = (child*)malloc(sizeof(child)); child_init(p);
    p->child_(p);
    p->initialize(p);
    p->draw(p);
    base* pb = (base*)p;
    pb->initialize(pb);
    pb->draw(pb);
    p->_child(p);
    free(p);
   
    return (EXIT_SUCCESS);
}

Result Run:

non dynamic
base_
child_
base_initialize
child_draw
base_initialize
child_draw
_child
_base
dynamic
base_
child_
base_initialize
child_draw
base_initialize
child_draw
_child
_base

Comments

  1. i like it. less confusion and straight to the point. i have seen others using virtual tables but seem more complicated and performance wise not so good. good work!

    ReplyDelete

Post a Comment

Popular posts from this blog

Creating tray application and popup context menu in Windows

How to put an image to wxPanel using wxWidgets

How to statically link applications to wxWidgets via Visual Studio 2008 and in Linux