From C function pointers to object-oriented programming · PDF fileFrom C function pointers to...
Transcript of From C function pointers to object-oriented programming · PDF fileFrom C function pointers to...
![Page 1: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/1.jpg)
From C function pointers to object-orientedprogramming
Hayo ThieleckeUniversity of Birmingham
http://www.cs.bham.ac.uk/~hxt
February 2015
![Page 2: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/2.jpg)
Objects and C
I C gives us primitive building blocks
I struct, pointers, functions
I What we do with them is up to us
I How far can we push C?
I How about objects? Or something reasonably close?
I We will assume: virtual functions as fundamental for OO
I Early C++ was a preprocessor for C
I Advanced example of pointers in C
I Some idea of how C++ is implemented
![Page 3: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/3.jpg)
The big picture: building objects in C
C++ C
machine code
build and use vtables manually
C++ compiler C compiler
![Page 4: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/4.jpg)
Simple objects simulated in C
In C++ we can write:
class inCPP {
int x;
public:
int get() { return this->x; }
};
In C we can write:
struct inC {
int y;
int (*cget)(struct inC *thisp);
};
int cf(struct inC *thisp) { return thisp->y; }
![Page 5: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/5.jpg)
Simple objects simulated in C
In C++ we can write:
class inCPP {
int x;
public:
int get() { return this->x; }
};
In C we can write:
struct inC {
int y;
int (*cget)(struct inC *thisp);
};
int cf(struct inC *thisp) { return thisp->y; }
![Page 6: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/6.jpg)
Classes simulated in C
In class-based OO languages (like C++), objects can share theirmember functions in a virtual function table, one per class
struct vtbl {
void (*f1)(); // member functions
int (*f2)();
...
};
struct s {
struct vtbl *vptr; // pointer to shared vtbl
int x; // data members
};
![Page 7: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/7.jpg)
Physical subtyping in C example
struct s1 {
struct s1 *p;
int x;
};
struct s2 {
struct s2 *q;
int y;
struct s2 *q2;
};
Code that works on s1 can also work on s2.In that sense, s2 is a physical subtype of s1.A limited form of polymorphism in C due to structure layout
![Page 8: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/8.jpg)
OO in C: two key pointers
In C++ we write a virtual function call as
left->print();
Simulated in C, this becomes:
thisp->left->vptr->print(thisp->left);
Give each function access to object via “self” or “this” pointer
Call virtual function indirectly through virtual function table
![Page 9: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/9.jpg)
Example class in C++
Canonical example of OO:parse trees for expressionsvirtual functions for processing trees
class Expression
{
public :
virtual int eval() = 0;
virtual void print() = 0;
};
![Page 10: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/10.jpg)
Virtual function table in C: types
structure + pointer + function:
struct vtbl
{
void (* print)();
int (*eval)();
};
Base class has pointer to vtbl:
struct ExpressionOO
{
struct vtbl *vptr;
};
![Page 11: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/11.jpg)
Derived class via physical subtyping
struct Constant
{
struct vtbl *vptr;
int n;
};
In memory:
ExpressionOO: Constant:
vptr vptr
n
Position of vptr is the same.
![Page 12: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/12.jpg)
Virtual member functions populate the vtable
void printConstant(struct Constant *thisp)
{
printf("%d", thisp ->n);
}
int evalConstant(struct Constant *thisp)
{
return thisp ->n;
}
Global variable for vtable, containing function pointers
struct vtbl vtblConstant =
{
&printConstant ,
&evalConstant
};
![Page 13: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/13.jpg)
Constructor
malloc and intialize, including vptr
void *makeConstantOO(int n)
{
struct Constant *p;
p = malloc(sizeof(struct Constant));
if(p == NULL) exit (1);
p->n = n;
p->vptr = &vtblConstant;
return p;
}
![Page 14: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/14.jpg)
Another derived class, for plus
struct Plus
{
struct vtbl *vptr;
struct ExpressionOO *left;
struct ExpressionOO *right;
};
In memory:
ExpressionOO: Plus:
vptr vptr
left
right
![Page 15: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/15.jpg)
Virtual member functions
void printPlus(struct Plus *thisp)
{
thisp ->left ->vptr ->print(thisp ->left);
printf(" + ");
thisp ->right ->vptr ->print(thisp ->right);
}
The eval function:
int evalPlus(struct Plus *thisp)
{
return thisp ->left ->vptr ->eval(thisp ->left)
+ thisp ->right ->vptr ->eval(thisp ->right);
}
![Page 16: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/16.jpg)
Virtual function table for plus
struct vtbl vtblPlus =
{
&printPlus ,
&evalPlus
};
![Page 17: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/17.jpg)
Constructor for plus
void *makePlusOO(struct ExpressionOO *left ,
struct ExpressionOO *right)
{
struct Plus *p;
p = malloc(sizeof(struct Plus));
if(p == NULL) exit (1);
p->vptr = &vtblPlus;
p->left = left;
p->right = right;
return p;
}
![Page 18: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/18.jpg)
Using it
struct ExpressionOO *p1 , *p2 , *p3 , *p4 , *p5 ,
*p6, *p7;
p1 = makeConstantOO (1);
p2 = makeConstantOO (2);
p3 = makeConstantOO (3);
p4 = makeConstantOO (4);
p5 = makePlusOO(p1, p2);
p6 = makePlusOO(p3, p4);
p7 = makePlusOO(p5, p6);
printf("\nTesting print 1 + 2 + 3 + 4\n");
p7->vptr ->print(p7);
![Page 19: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/19.jpg)
OO in C: two key pointers
In C++ we write a virtual function call as
left->print();
Simulated in C, this becomes:
thisp->left->vptr->print(thisp->left);
Give each function access to object via “self” or “this” pointerCall virtual function indirectly through virtual function table
![Page 20: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/20.jpg)
How big are objects in C++
class A {
void fA() { }
int *a;
};
class B {
virtual void fB() {}
int *b;
};
class C {
virtual void fC1() {}
virtual void fC2() {}
int *c;
};
sizeof(A) = 8, sizeof(B) = 16, sizeof(C)= 16 on typical compiler
![Page 21: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/21.jpg)
How big are objects in C++
class A {
void fA() { }
int *a;
};
class B {
virtual void fB() {}
int *b;
};
class C {
virtual void fC1() {}
virtual void fC2() {}
int *c;
};
sizeof(A) = 8, sizeof(B) = 16, sizeof(C)= 16 on typical compiler
![Page 22: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/22.jpg)
Inheritance puzzle
class base {
public:
int x = 1;
virtual int f() { return x + g(); }
virtual int g() { return 10; }
};
class derived : public base {
public:
int x = 100;
virtual int g() { return x; }
};
What is (new derived())->f()
101Functions use indirection via vtable, whereas variables do notFrom the book “Essentials of Programming Languages”, by Wand, Friedman, Haynes, 2nd edition
![Page 23: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/23.jpg)
Inheritance puzzle
class base {
public:
int x = 1;
virtual int f() { return x + g(); }
virtual int g() { return 10; }
};
class derived : public base {
public:
int x = 100;
virtual int g() { return x; }
};
What is (new derived())->f()
101Functions use indirection via vtable, whereas variables do notFrom the book “Essentials of Programming Languages”, by Wand, Friedman, Haynes, 2nd edition
![Page 24: From C function pointers to object-oriented programming · PDF fileFrom C function pointers to object-oriented programming ... I Early C++ was a preprocessor for C ... Inheritance](https://reader031.fdocuments.in/reader031/viewer/2022021818/5aa731e87f8b9aee748bc19f/html5/thumbnails/24.jpg)
Conclusions on C++ → C
I C is simple, powerful and flexible
I pointers
I control over memory
I physical subtyping
I function pointers
I static type checking, up to a point
I C type system is not a straightjacket
I C++ objects can be built on top of C quite easily
I Objects become clearer if you know how they are implemented
I Translations (like compiling) are a fundamental technique inprogramming languages