Introduction To Object -Oriented Programming With C

2y ago
31 Views
4 Downloads
4.19 MB
36 Pages
Last View : 21d ago
Last Download : 3m ago
Upload by : Asher Boatman
Transcription

Introduction to Object -OrientedProgramming with C Olivier MattelaerUCLouvainCP3 & CISM

Programming paradigmParadigm style of computer programming Procedural languages: Declarative programming Describe step by step the procedure that should be followed tosolve a specific problem.The computer is told what the problem is, not how to solve theproblemObject-oriented programming: Data and methods of manipulating data are kept as single unit calledobject A user can access the data via the object’s method The internal working of an object maybe changed without affectingFunctional, Generic, structured,procedural, object orientedany code that uses the objectCECI training: OOP with C 217/10/2017

Why C Tiobe Ranking Extension of C (originally called “C with Classes”)Compiled, high level language, strongly-typed unsafe language, static anddynamic type checking, supports many paradigm, is portableCECI training: OOP with C 317/10/2017

Program of today Basic of C Presentation of concept Code presentation Exercise Introduction to Class in C Presentation of concept Code presentation Exercise (Multi) InheritancePresentation of concept Code presentation Exercise CECI training: OOP with C 417/10/2017

Hello Worldcpp.sh/2ddhttp://www.cpp.sh/2dd line1: Comment line 2: preprocessor directive: also /* */Include a section of standard C code in the codeline 3: empty line: do nothing (but clarity for human reader)line 4: declaration of a function main is a special function which is run automatically starts and stops with the braces (line 5 and 7)Statement. Send character to the output device Note the semi-column at the end of the lineCECI training: OOP with C 517/10/2017

Compile the codeC C 11Hmem/linuxHmem/linuxRun Onceg -o EXECNAME input.cppmodule load GCC/4.9.3-2.25g -std c 11 —o EXECNAME input.cppMacMacclang -std c 11 -stdlib libc \—o EXECNAME input.cppg -o EXECNAME input.cppNote some C 11 tps://ideone.com/Select C 14 (bottom left)Select C (bottom left)http://www.cpp.sh/2ddCECI training: OOP with C http://www.cpp.sh/2dd617/10/2017

Basic of C : variablesVariable portion of memory storing a value C is strongly typedNeed to know the type of variable The type determines the size of the house http://cpp.sh/8ylC 11C 11http://cpp.sh/7d4CECI training: OOP with C 717/10/2017

Basic of C : pointerPointer position in memory of the variable Due to deference pointer also have typed: Those are the type of the variable suffixby a starDeference:CECI training: OOP with C 817/10/2017

Basic of C : functionsFunction group of statements- that is given a name,- which can be called from some point of the programPassing Parameters by VariableCECI training: OOP with C Passing Parameters by referencecpp.sh/2lp9http://cpp.sh/9b217/10/2017

Basic of C : ArrayArray sequential memory space of the same typecpp.sh/6fzbC 11 Note the syntax to receivearray in a function! Array behaves like pointer!cpp.sh/7aotCECI training: OOP with C 1017/10/2017

Exercise I Check that you can compile the Hello Worldexample Define a function that take 3 float and return theaverage For an array of integer and a given value.Return the pointer where this value is. Use this pointer to get the value of the next twoentry of the array Example {1,2,3,4,5} and val 3 - should return 4/5 Have Fun Useful resources: http://www.cplusplus.com/reference http://www.cplusplus.com/doc/tutorial/CECI training: OOP with C 1117/10/2017

Solutionpart I : cpp.sh/6ar2xpart II: cpp.sh/3wr4CECI training: OOP with C 1217/10/2017

Classesclasses data structure with functionsdata structure group of data elements groupedtogether under a single name We can define a class “Car” Defines the structure Which property available: attribute model, colour, has autodrive,nb doorWhich function can be applied. change battery, add fuel, Class is a new type like “int/float” Car mytesla; “mytesla” is an instance of the class CARCECI training: OOP with C 1317/10/2017

Visibility of attribute/functionprivateprotectedpublicOnly accessible from otherinstance of the same classAccessible from otherinstance of the same classAccessible from friendsAccessible from friendsDEFAULTCECI training: OOP with C Accessible from instance ofthe derived/child class14Accessible fromeverywhere where theobject is visibleREAD and WRITE!17/10/2017

First Examplehttp://cpp.sh/8acCECI training: OOP with C 15 width/height are private private attribute ensure thatno one mess up thosevariables.A public function allows toset those values!17/10/2017

Code StructureCECI training: OOP with C 1617/10/2017

Constructorconstructor function called after the object is createdcpp.sh/8lr The name of the constructoris the name of the functionitself!Shortcut for setting attributeCECI training: OOP with C 1717/10/2017

OverloadingOverloading more than one function with the same name The name of two functions CAN be the same ifthe number of argument or the type of argumentare different. Any function can beoverloaded. You can overload basicoperation between objectlike addition: CECI training: OOP with C 18Operator 17/10/2017

OverloadingOverloading more than one function with the same namecpp.sh/27lCECI training: OOP with C 1917/10/2017

Special membersSpecial members member functions implicitly defined Default constructor: Destructor CLASSNAME: CECI training: OOP with C Present only if no other constructor exists!Perform cleanup (remove dynamical allocatedmemory) when the object is deleted/out of scopeCopy Constructor: Called when you call that class (by value) in afunction. Perform shallow copy of all attribute2017/10/2017

cpp.sh/3zgctCECI training: OOP with C Example2117/10/2017

Exercise IICreate a class for three dimensional vectorDefine function to get/set each componentDefine a function returning the norm(squared) ofthe vector Define the scalar product between two vector: x[0]**2 x[1]**2 x[2]**2x[0]*y[0] x[1]*y[1] x[2]*y[2]Define a Class parallelogramCan be initialised by two vector Set a function to compute the associated area CECI training: OOP with C 2217/10/2017

Solutioncpp.sh/6vgu2cCECI training: OOP with C 2317/10/2017

Solutioncpp.sh/7dpvgCECI training: OOP with C 2417/10/2017

InheritanceInheritance new classes which retain characteristics of the base class. The idea is the heritage. What a parent can do,their child can do it too.cpp.sh/72itcMotherhello()Inherit fromChild1Inherit fromChild2hello()Can still call hello()CECI training: OOP with C 2517/10/2017

InheritanceInheritance new classes which retain characteristics of the base class. The idea is the heritage. What a parent can do,their child can do it too.cpp.sh/72itcCECI training: OOP with C “public” tells the maximum level of visibility ofthe attribute coming from the base class Private argument are not passed to the child(but they still exits!) Constructor/Destructor are not passed to thechild Assignment operator (operator ) are notpassed to the child2617/10/2017

Inherit fromInherit fromChildset age()print()Can still call hello()Can access to age (protected)CECI training: OOP with C 2717/10/2017

Multi-inheritancecpp.sh/8vevMotherFatherhello()Age (priv)set age()get age()Inherit fromInherit fromChildprint()Can call hello()Can not call age (since private)But can call the public routine offather which set/get the agevariableCECI training: OOP with C 2817/10/2017

Exercise III Define a class Four-Vector which inherit from your class 3vector Define the norm like in special relativity Define a class ParticleInfo x*x x[0]x[0] -x[1]x[1]-x[2]x[2]-x[3]x[3]Has some attribute (mass/width)Define a class Particle which inherit from both class Define a function which computes the difference between themass square and the norm squared.CECI training: OOP with C 2917/10/2017

Solutioncpp.sh/2jenCECI training: OOP with C 3017/10/2017

Diamond Diagramcpp.sh/4inojAncestorYeartell something()Inherit fromMotherInherit fromFatherhello()Inherit fromAgeInherit fromChildCECI training: OOP with C 3117/10/2017

Diamond Diagramcpp.sh/4inoj CECI training: OOP with C 32Two copy of the Ancestor class test.Mother::year test.Father::year You can use virtual inheritance tohave a single copy Consider as bad design in C 17/10/2017

TemplateTemplate define functions class with generic type Repeat yourself is bad but often you have to havethe exact same definition but for different type Template is the solutioncpp.sh/4jqCECI training: OOP with C 3317/10/2017

Polymorphisma pointer to a derived class is type-compatible with a pointer to its base classcpp.sh/3tzCECI training: OOP with C 34 We can use a pointer of the classCPolygon (CPolygon*) withobject from his derived class Note that from pointer you canaccess attribute/member functionwith - Carefully which function youaccess with polymorphism17/10/2017

Exercise IV Update your four-vector class to include Scalar Multiplication via Template MethodTest polymorphism on your classCECI training: OOP with C 3517/10/2017

Conclusion Oriented ObjectAre a nice way to separate the inner work from theway the object are called Inheritance allows you to build/expand without theneed to restart from scratch Private argument help you to sand box yourself You need to play with itCECI training: OOP with C 3617/10/2017

Object-oriented programming: Data and methods of manipulating data are kept as single unit called object A user can access the data via the object’s method The internal working of an object maybe changed without affecting any code that uses the object Functional, G

Related Documents:

method dispatch in different object-oriented programming languages. We also include an appendix on object-oriented programming languages, in which we consider the distinction between object-based and object-oriented programming languages and the evolution and notation and process of object-oriented analysis and design, start with Chapters 5 and 6;

object-oriented programming language is based on a kind of old object-oriented programming language. For example, though C language is an object-oriented programming language, it still retains the pointer which is complex but has strong function. But C# improved this problem. C# is a kind of pure object-oriented language.

It stands for Object Oriented Programming. Object‐Oriented Programming ﴾223﴿ uses a different set of programming languages than old procedural programming languages ﴾& 3DVFDO, etc.﴿. Everything in 223 is grouped as self sustainable "REMHFWV". Hence, you gain reusability by means of four main object‐oriented programming concepts.

The principle of object oriented programming is to combine both data and the associated functions into a single unit called a class. An object in programming means data. Data is therefore predominant in object oriented programming. The concept will become clear with examples. However, in the object oriented paradigm, accessibility of

Object Oriented Programming 7 Purpose of the CoursePurpose of the Course To introduce several programming paradigms including Object-Oriented Programming, Generic Programming, Design Patterns To show how to use these programming schemes with the C programming language to build “good” programs.

Object Class: Independent Protection Layer Object: Safety Instrumented Function SIF-101 Compressor S/D Object: SIF-129 Tower feed S/D Event Data Diagnostics Bypasses Failures Incidences Activations Object Oriented - Functional Safety Object: PSV-134 Tower Object: LT-101 Object Class: Device Object: XS-145 Object: XV-137 Object: PSV-134 Object .

1. From structured programming to object-oriented programming 1 2. Towards Object-oriented Programming 7 3. Phenomena and Concepts 13 4. Towards Object-oriented Programs 19 5. The C# Language and System 23 6. C# in relation to C 25 7. C# in relation to Java 53 8. C# in relation to Visual Basic 57 9. C# Tools and IDEs 59 10.

Abstract—OOP (Object Oriented Programming) is an object-oriented programming method. The purpose of OOP is to facilitate the development of the program by following the models that have existed in everyday life. Object-oriented programming techniques are becoming very popular today in the process of creating multi-operating system applications.