Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP in C. Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler.

Similar presentations


Presentation on theme: "OOP in C. Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler."— Presentation transcript:

1 OOP in C

2 Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler

3 Object-Oriented Programming Data abstraction Support for –Encapsulation –Data Hiding –Inheritance

4 Objects Consist of –Data - each object has its own data –Operations (methods) A class defines the data and operations for a particular type O-O languages provide support for hiding the data

5 structs in C Provide a mechanism for creating user- defined data types A struct allows you to group multiple data items into a single "data object" Function pointers allow you to include functions in the object's data

6 What doesn't a struct have? Data hiding –Everything in the header file is public this pointer –functions need object as an argument How do all objects of a class share the same methods? Inheritance

7 Methods Putting function pointers into each object means different objects can have different versions of the method Alternate approach is to have a virtual function table (vtable) for each "class" and put a pointer to this table in each object

8 One approach to inheritance #define S1_CLASS \ int x; \ int y; \ int (* get_x)(); \ int (*get_y)(); typedef struct s1 { S1_CLASS } S1; #define S2_CLASS \ S1_CLASS \ int z; \ int (*get_z)(); typedef struct s2 { S2_CLASS } S2;

9 vtables and inheritance vtable for a derived class contains a link to the vtable of the base class For multiple inheritance, have links to vtable of all parents

10 Making generic classes In C++, get generics using templates Use a #define function for the class Use a variable for the type(s) that are generic Calling the function with a particular type will generate code with the desired type in all the appropriate places

11 Some sources David Brumbaugh, C/C++ User's Journal, July 1, 1990 http://ldeniau.home.cern.ch/ldeniau/html /oopc/oopc.htmlhttp://ldeniau.home.cern.ch/ldeniau/html /oopc/oopc.html http://www.research.att.com/~bs/hopl2. pdf http://www.planetpdf.com/codecuts/pdfs /ooc.pdf


Download ppt "OOP in C. Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler."

Similar presentations


Ads by Google