27
Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff OOC Object-oriented programming in C Nils Mosch ¨ uring PhD Student (LMU) Nils Mosch ¨ uring PhD Student (LMU) , OOC 1

OOC - Object-oriented programming in C · OOC Object-oriented programming in C Nils Moschuring¨ PhD Student (LMU) Nils Moschuring PhD Student (LMU) , OOC¨ 1. Outline Introduction

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

OOCObject-oriented programming in C

Nils MoschuringPhD Student (LMU)

Nils Moschuring PhD Student (LMU) , OOC 1

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

1 Introduction to OO-ProgrammingWhat is it?VisibilityInheritanceLate binding

2 First Steps of OO-Programming in CClasses will be structs & functionsAggregations and AssociationsInheritance & SubtypingEncapsulation

3 Final stuff

Nils Moschuring PhD Student (LMU) , OOC 2

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

What is class-based object oriented programming?

The basic notion of class-based programming is the combination of methodsand data fields into classes. These can be visualized using UML diagrams:

class name

visibility(+/-/#) data fields : type

visibility(+/-/#) methods(arguments : type) : return type

Classes employing other classes as data fields generate a relation:

x1

a0..*

class 1 class 2

This means that

class 1 has a list of 0 or more objects of type class 2 called a

class 2 has exactly 1 object of type class 1 called x

Nils Moschuring PhD Student (LMU) , OOC 4

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

What is visibility?

Provides a mechanisms for the concept of Encapsulation

Allows for strict control of access to declared data fields and methods

Important for team-based software developmentCommonly distinguished types of visibility:+ public, unrestricted access# protected, access by containing class and sub-classes− private, access only by containing class∼ package, access in containing package

Nils Moschuring PhD Student (LMU) , OOC 5

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Example

+president

1+follower

0..*

potus

+ charisma : double# funding : double

+ hold speech() : int# diffame opponent() : void

US citizen

+ name : String+ sex : bool

+ vote() : potus

class potus {public :

double charisma ;i n t hold speech ( ) ;US c i t i zen ∗ f o l l o w e r ;

protected :void di f fame opponent ( ) ;double fund ing ;

} ;

class US c i t i zen {public :

char ∗name ;bool sex ;potus p res iden t ;potus vote ( ) ;

} ;

Nils Moschuring PhD Student (LMU) , OOC 6

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Inheritance

Inheritance is a concept to realize

reuse of code

subtype mechanism

in an intuitive manner.

Nils Moschuring PhD Student (LMU) , OOC 7

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Reuse of Code

+president

1+follower

0..*

human

+ name : String+ sex : bool

+ die() : void

potus

+ charisma : double# funding : double

+ hold speech() : int# diffame opponent() : void

US citizen

+ vote() : potus

The data fields which are shared between related classes occur only once

Shared methods occur only once

Nils Moschuring PhD Student (LMU) , OOC 8

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Subtyping

Useful nomenclature for relations

Has-A:Describes usage as a data-field

Is-A:Describes a class as a subclass

The following holds true:

potus Is-A human

potus Has-A US citizen

US citizen Is-A human

US citizen Has-A potus

Nils Moschuring PhD Student (LMU) , OOC 9

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Subtyping

Is-A relations allow for exchange in function arguments (polymorphism)

i n t marry (human h1 , human h2 ) {L i v e h a p p i l y ( h1 , h2 ) ;i n t n r c h i l d r e n = fuck ing in the bushes ( h1 , h2 ) ;return n r c h i l d r e n ;

}potus mr pres ident ;human ms german ;US c i t i zen mr american ;

Subtyping allows the following function calls:

marry ( mr pres ident , ms german ) ;marry ( mr pres ident , mr american ) ;marry ( ms german , mr american ) ;

And they could all return some children!Nils Moschuring PhD Student (LMU) , OOC 10

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

What is late binding? - binding

‘Binding’ describes the process usually done by the linker, where differentfunctions and their respective calls are bound together. Imagine two files:fileA.c:

void funcA ( i n t i ){p r i n t f ( ”%d\n ” , i ) ;

}

fileB.c:

void funcA ( i n t ) ;i n t main ( ) {

funcA ( 5 ) ;return 1;

}

Nils Moschuring PhD Student (LMU) , OOC 11

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

What is late binding? - binding

Compiling and linking is done with gcc fileA.c fileB.c.In this case, the linker will search all created object files for a function with the correctname and will link this function to the function call in the main routine.This is not late binding. The binding happens during the linking stage.

fileA.c

void funcA1(int);void funcA2(int);

.

.

.

fileB.c

void funcB1(int);void funcB2(int);

int main(){funcA1(5);}

...

Compiler

fileA.o

void funcA1(int);void funcA2(int);

.

.

.

fileB.o

void funcB1(int);void funcB2(int);

int main(){funcA1(5);}

...

a.out

Linker

Nils Moschuring PhD Student (LMU) , OOC 12

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

What is late binding? - late binding

If, at compile time, it’s not certain which function to call, the linker can not link.The resulting program has a program sequence which is not set.main.c:

void funcA ( i n t i ){p r i n t f ( ” In A : %d\n ” , i ) ;

}void funcB ( i n t i ){

p r i n t f ( ” In B : %d\n ” ,5∗ i ) ;}i n t main ( i n t argc , void ∗∗argv ){

void ( ∗ fp ) ( i n t )=NULL ;i f ( argc>1) fp=&funcB ;else fp=&funcA ;( ∗ fp ) ( 5 ) ;return 1;

}

Nils Moschuring PhD Student (LMU) , OOC 13

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

What is late binding? - late binding

Results:./a.out

In A:5

./a.out whatever

In B:25

So depending on runtime information the code takes different paths!Linking is done during runtime.

That’s late binding!

Nils Moschuring PhD Student (LMU) , OOC 14

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Connection to OO-Programming

class human {. . .

v i r t u a l void d ie ( ) { p r i n t f ( ”Someones dead\n ” ) ; }v i r t u a l void s t a t s ( ) { p r i n t f ( ”Someones s t a t s \n ” ) ; }

} ;class potus : public human {. . .

void d ie ( ) { p r i n t f ( ” Pres ident dead\n ” ) ; }/ / No implementat ion o f s t a t s ( )} ;class US c i t i zen : public human {public :. . .

void s t a t s ( ) { p r i n t f ( ”US c i t i z e n s s t a t s \n ” ) ; }/ / No implementat ion o f d ie ( )} ;

Nils Moschuring PhD Student (LMU) , OOC 15

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Connection to OO-Programming

void k i l l (human ∗h1 ) {h1−>d ie ( ) ;

}i n t main ( i n t argc ) {

potus my potus ;US c i t i zen my us c i t i zen ;human my human ;human ∗hp = NULL ;i f ( argc>1) hp = &my potus ;else hp = &my us c i t i zen ;k i l l ( hp ) ;k i l l (& my us c i t i zen ) ;return 0;

}

Results:./a.out

Someones deadSomeones dead

./a.out whatever

Pres idents deadSomeones dead

Nils Moschuring PhD Student (LMU) , OOC 16

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Internals: dynamic type identification and vtables

How does this work?

Dynamical type identification of the pointer, followed by usage of classembedded vtable (virtual function table).

hp

my potus

my us citizen

my human &human::die()

&human::stats()

&potus::die()

&US citizen::stats()

die()

stats()

die()

stats()

die()

stats()

This approach makes execution slightly slower.Nils Moschuring PhD Student (LMU) , OOC 17

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Classes will be structs & functions

A basic approach is to use structs flanked by special functions to implement afirst notion of classes:

struct potus {double charisma ;double fund ing ;

} ;void p o t u s i n i t ( potus ∗ t h i s , double cha , double fund ) ;i n t potus hold speech ( potus ∗ t h i s ) ;void potus d i f fame opponent ( potus ∗ t h i s , human ∗h ) ;

Several key concepts are missing

Encapsulation

Aggregations, Associations

Inheritance

Subtyping

Let’s work on these!Nils Moschuring PhD Student (LMU) , OOC 19

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Reuse of Code

+president

1+follower

0..*

human

+ name : String+ sex : bool

+ die() : void

potus

+ charisma : double# funding : double

+ hold speech() : int# diffame opponent() : void

US citizen

+ vote() : potus

The data fields which are shared between related classes occur only once

Shared methods occur only once

Nils Moschuring PhD Student (LMU) , OOC 20

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Aggregations and Associations

Implementing aggregations and associations is done in a straightforward way

struct US c i t i zen {struct potus ∗ pres iden t ;

} ;struct potus {

double charisma ;double fund ing ;struct US c i t i zen ∗ f o l l o w e r ;struct p r e s i d e n t i a l s u i t s u i t ;

} ;void po tus add fo l l ower ( struct potus ∗ t h i s ,

struct US c i t i zen ∗new f ) ;void U S c i t i z e n s e t p r e s i d e n t ( struct US c i t i zen ∗ t h i s ,

struct potus ∗pres ) ;struct potus ∗ US c i t i zen vo te ( struct US c i t i zen ∗ t h i s ) ;

Nils Moschuring PhD Student (LMU) , OOC 21

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Inheritance & Subtyping

struct human {char ∗name ;bool sex ;

} ;struct US c i t i zen {

struct human human ;struct potus ∗ pres iden t ;

} ;struct potus {

struct human human ;double charisma ;double fund ing ;struct US c i t i zen ∗ f o l l o w e r ;struct p r e s i d e n t i a l s u i t s u i t ;

} ;

This has some very special properties

Nils Moschuring PhD Student (LMU) , OOC 22

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Inheritance & Subtyping

The C standard guarantees that

struct US c i t i zen my c i t i zen ;struct human ∗human = ( struct human∗ ) &my c i t i zen ;

is a valid cast.

What does that mean?

potus

namesex human

charisma

funding

follower

suit

addressspace

&my potus

&my potus.human

&my potus.human.name

Nils Moschuring PhD Student (LMU) , OOC 23

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Inheritance & Subtyping

Hence a function like

void human in i t ( struct human ∗h ) ;

can be called like this:

struct potus my potus ;human in i t ( ( struct human∗ ) &my potus ) ;

This is subtyping!

Unfortunately some key capabilities are still missing.

Inheritance is done via casting or direct usage of the base class data field.

Still missing: Encapsulation

Nils Moschuring PhD Student (LMU) , OOC 24

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

How can achieve encapsulation in C?

Our class should only be accessed via the supplied functions.

This can be realized through different header files.

There will be a public header file, which looks like this:

my structs.h

struct human ;struct potus ;struct US c i t i zen ;void human in i t ( struct human ∗h ) ;struct human ∗human create ( ) ;struct potus ∗ US c i t i zen vo te ( struct US c i t i zen ∗ t h i s ) ;. . .

It declares the available structs and functions to interact with them.

Through inclusion of this header one can not directly change values of thestructs since the compiler does not know their complete form.

Nils Moschuring PhD Student (LMU) , OOC 25

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Encapsulation

The private header file is only to be included by the implementing .c files.

my structs private.h

struct human {char ∗name ;bool sex ;

} ;struct US c i t i zen {

struct human human ;struct potus ∗ pres iden t ;

} ;struct potus {

struct human human ;double charisma ;double fund ing ;struct US c i t i zen ∗ f o l l o w e r ;struct p r e s i d e n t i a l s u i t s u i t ;

} ;. . .

Nils Moschuring PhD Student (LMU) , OOC 26

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Encapsulation

The implementing .c file is then given by

my structs .c

#include ” m y s t r u c t p r i v a t e . h ”void human in i t ( struct human ∗h ) {

/ / do s t u f f}struct human ∗human create ( ) {

struct human ∗new = c a l l o c (1 , sizeof ( ∗new ) ) ;return new ;

}struct potus ∗ US c i t i zen vo te ( struct US c i t i zen ∗ t h i s ) {

return t h i s−>pres iden t ;}

Nils Moschuring PhD Student (LMU) , OOC 27

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Encapsulation

The supplied functionality can then safely used like this:

my other project.c

#include ” my s t ruc t . h ”i n t main{ i n t argc , char ∗∗argv ) {

struct human ∗my human = human create ( ) ;human in i t (my human ) ;. . .

}

Keep in mind that:

No variable of the corresponding type can be instantiated.

Only pointers are available.

Nils Moschuring PhD Student (LMU) , OOC 28

Outline Introduction to OO-Programming First Steps of OO-Programming in C Final stuff

Final stuff

Take-Home-Message

C natively supports more OO capabilities than anticipated.

Nils Moschuring PhD Student (LMU) , OOC 30