------------------------------------------------------------------------------
MC logo
Translating A Class
[^] Modular and Class Abstraction
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Translating A Class] [Translating Inheritance] [Dynamic Binding]

A class is really just nice syntax for functions and structs. (And structs are just nice notation for a bunch of variables, after all.) “Nice syntax” is not meant to be derogatory. Good notation helps us think, and classes help programmers for just that reason.

class Fred {
private:
    static int a,b,c;
    int x,y;
public:
    Fred(int a) {
        x = a;
        y = 0;
    }
    void joe(int m) {
        a = x;
        y = m;
    }
};
main() {
    Fred *f = new Fred(10);
    f->joe(20);
}
struct {
    int a,b,c;
} Fred_static_part;
struct Fred_dynamic_part {
    int x,y;
};
void Fred_Fred(struct Fred_dynamic_part *this, int a) {
    this->x = a;
    this->y = 0;
}
void Fred_joe(struct Fred_dynamic_part *this, int m) {
    Fred_static_part.a = this->x;
    this->y = m;
}
main() {
    struct Fred_dynamic_part *f = 
      malloc(sizeof(struct Fred_dynamic_part));
    Fred_Fred(f,10);
    Fred_joe(f,20);
}